+ //
+ // Stack selection loop
+ //
+ class SelectorLogic { // need to do recursive back tracking, requires a "nested" function
+ private:
+ Int_t idCount;
+ map<Int_t,Int_t> firstMotherMap;
+ map<Int_t,Int_t> secondMotherMap;
+ map<Int_t,Bool_t> selectedIdMap;
+ map<Int_t,Int_t> newIdMap;
+ void selectMothersToo(Int_t particleId) {
+ Int_t mum1 = firstMotherMap[particleId];
+ if (mum1 > -1 && !selectedIdMap[mum1]) {
+ selectedIdMap[mum1] = true;
+ selectMothersToo(mum1);
+ }
+ Int_t mum2 = secondMotherMap[particleId];
+ if (mum2 > -1 && !selectedIdMap[mum2]) {
+ selectedIdMap[mum2] = true;
+ selectMothersToo(mum2);
+ }
+ }
+ public:
+ SelectorLogic():idCount(0), firstMotherMap(), secondMotherMap(), selectedIdMap(), newIdMap() {}
+ void init() {
+ idCount = 0;
+ }
+ void setData(Int_t id, Int_t mum1, Int_t mum2, Bool_t selected) {
+ idCount++; // we know that this function is called in succession of ids, so counting is fine to determine max id
+ firstMotherMap[id] = mum1;
+ secondMotherMap[id] = mum2;
+ selectedIdMap[id] = selected;
+ }
+ void reselectCuttedMothersAndRemapIDs() {
+ for (Int_t id = 0; id < idCount; ++id) {
+ if (selectedIdMap[id]) {
+ selectMothersToo(id);
+ }
+ }
+ Int_t newId0 = 0;
+ for (Int_t id = 0; id < idCount; id++) {
+ if (selectedIdMap[id]) {
+ newIdMap[id] = newId0; ++newId0;
+ } else {
+ newIdMap[id] = -1;
+ }
+ }
+ }
+ Bool_t isSelected(Int_t id) {
+ return selectedIdMap[id];
+ }
+ Int_t newId(Int_t id) {
+ if (id == -1) return -1;
+ return newIdMap[id];
+ }
+ };
+ SelectorLogic selector;
+ selector.init();
+ for (i = 0; i < nTracks; i++) {
+ TParticle* jparticle = fReader->NextParticle();
+ selector.setData(i,
+ jparticle->GetFirstMother(),
+ jparticle->GetSecondMother(),
+ KinematicSelection(jparticle,0));
+ }
+ selector.reselectCuttedMothersAndRemapIDs();
+ fReader->RewindEvent();