FairRoot/PandaRoot
PndMcCloner2.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- PndMcCloner2 source file -----
3 // ----- Created 08/07/13 by S.Spataro -----
4 // -------------------------------------------------------------------------
5 
6 #include "PndMcCloner2.h"
7 
8 #include "PndMCTrack.h"
9 #include "PndPidCandidate.h"
10 
11 #include "FairRootManager.h"
12 #include "FairDetector.h"
13 #include "FairRun.h"
14 #include "FairRuntimeDb.h"
15 
16 #include "TClonesArray.h"
17 
18 #include <iostream>
19 
20 using std::cout;
21 using std::endl;
22 
23 // ----- Default constructor -------------------------------------------
24 PndMcCloner2::PndMcCloner2() : FairTask("Cloner of PndMCTrack"),
25  fInputArray(), fPidChargedArray(), fPidNeutralArray(), fOutputArray(),
26  mapMCIndex(), fCleanMC(kFALSE), fTrackBranchNamePidHypo("")
27 {
28 }
29 // -------------------------------------------------------------------------
30 
31 // ----- Destructor ----------------------------------------------------
33 // -------------------------------------------------------------------------
34 
35 // ----- Public method Init --------------------------------------------
36 InitStatus PndMcCloner2::Init() {
37 
38  cout << "-I- PndMcCloner2::Init: "
39  << "INITIALIZATION *********************" << endl;
40 
41  fPidHypoStr[0] = "Electron";
42  fPidHypoStr[1] = "Muon";
43  fPidHypoStr[2] = "Pion";
44  fPidHypoStr[3] = "Kaon";
45  fPidHypoStr[4] = "Proton";
46 
47  //FairRun* sim = FairRun::Instance(); //[R.K. 01/2017] unused variable?
48  //FairRuntimeDb* rtdb=sim->GetRuntimeDb(); //[R.K. 01/2017] unused variable?
49 
50  // Get RootManager
51  FairRootManager* ioman = FairRootManager::Instance();
52  if ( ! ioman ) {
53  cout << "-E- PndMcCloner2::Init: "
54  << "RootManager not instantiated!" << endl;
55  return kFATAL;
56  }
57 
58  // Get input array
59  fInputArray = (TClonesArray*) ioman->GetObject("MCTrack");
60  if ( ! fInputArray ) {
61  cout << "-E- PndMcCloner2::Init: "
62  << "No MCTrack array!" << endl;
63  return kERROR;
64  }
65  bool anychargedtca = false;
66  bool anyneutraltca = false;
67 
68  for(int i=0;i<5;i++){
69  fPidChargedArray[i] = (TClonesArray*) ioman->GetObject("PidChargedCand"+fPidHypoStr[i]);
70  if ( ! fPidChargedArray[i] ) {
71  cout << "-E- PndMcCloner2::Init: "
72  << "No PidChargedCand"+fPidHypoStr[i]+" array. Continue looking." << endl;
73 // return kERROR;
74  }
75  else {
76  fHypoFlagCharged[i] = true;
77  anychargedtca = true;
78  }
79 
80  fPidNeutralArray[i] = (TClonesArray*) ioman->GetObject("PidNeutralCand"+fPidHypoStr[i]);
81  if ( ! fPidNeutralArray[i] ) {
82  cout << "-E- PndMcCloner2::Init: "
83  << "No PidNeutralCand"+fPidHypoStr[i]+" array. Continue looking." << endl;
84 // return kERROR;
85  }
86  else {
87  fHypoFlagNeutral[i] = true;
88  anyneutraltca = true;
89  }
90  }
91 
92  if (!anychargedtca) {
93  cout << "-E- PndMcCloner2::Init: "
94  << "No PidChargedCand array found!" << endl;
95  return kERROR;
96  }
97  if (!anyneutraltca) {
98  cout << "-E- PndMcCloner2::Init: "
99  << "No PidNeutralCand array found!" << endl;
100  return kERROR;
101  }
102 
103 
104  // Create and register output array
105  fOutputArray = new TClonesArray("PndMCTrack");
106 
107  ioman->Register("MCTrack"+fTrackBranchNamePidHypo,"MC",fOutputArray,kTRUE);
108 
109  cout << "-I- PndMcCloner2: Intialization successfull" << endl;
110 
111  return kSUCCESS;
112 
113 }
114 // -------------------------------------------------------------------------
115 
116 
117 
118 // ----- Public method Exec --------------------------------------------
119 void PndMcCloner2::Exec(Option_t*) {
120 
121  // Reset output array
122  if ( ! fOutputArray ) Fatal("Exec", "No Output Array");
123 
124  fOutputArray->Clear();
125  mapMCIndex.clear();
126 
127  if (!fCleanMC)
128  {
129  CloneMCTrack();
130  }
131  else
132  {
137  }
138 }
139 // -------------------------------------------------------------------------
140 
141 // ----- Protected method FindUsedMcIndices --------------------------------------------
143 {
144  // Copy 1:1 of the MCTrack TClonesArray
145 
146  Int_t nMCTracks = fInputArray->GetEntriesFast();
147  for (Int_t iMC=0; iMC<nMCTracks; iMC++)
148  {
149  PndMCTrack *mctrack = (PndMCTrack*) fInputArray->At(iMC);
150  TClonesArray& clref = *fOutputArray;
151  Int_t size = clref.GetEntriesFast();
152  new(clref[size]) PndMCTrack(*mctrack);
153  } // Loop over MCTracks
154 }
155 
156 // ----- Protected method FindUsedMcIndices --------------------------------------------
158  // Loop over PidChargedCand and PidNeutralCand, find the used MC indices, and fill the map
159  // with this index and all the mother indices
160 
161  Int_t nCands = 0;
162 
163  // First store primary MC tracks
164  nCands = fInputArray->GetEntriesFast();
165  for (Int_t imc=0; imc<nCands; imc++)
166  {
168  if (mctrack->GetMotherID()==-1)
169  {
170  mapMCIndex[imc] = imc;
171  }
172  else break;
173  }
174 
175  //todo:assume all arrays are of same length, use list0 to get ncands for now
176  //todo:assuming also that all entries in the lists have the same ordering
177  for (int ihyp = 0; ihyp < 5;ihyp++){
178  if (fPidChargedArray[ihyp]) nCands = fPidChargedArray[ihyp]->GetEntriesFast();
179  }
180 // nCands = fPidChargedArray[0]->GetEntriesFast();
181  for (Int_t iPid=0; iPid<nCands; iPid++)
182  {
183  Int_t mcIndex = -1;
184  for(int i = 0;i<5;i++){
185  if (!fHypoFlagCharged[i]) continue;
186  PndPidCandidate *pidCand = (PndPidCandidate*) fPidChargedArray[i]->At(iPid);
187  mcIndex = pidCand->GetMcIndex();
188  if (mcIndex !=-1) break; //If any of the pidcandidates are pointing to a track, we exit loop
189  }
190  while (mcIndex!=-1)
191  {
192  PndMCTrack *mctrack = (PndMCTrack*)fInputArray->At(mcIndex);
193  if (mctrack==NULL)
194  {
195  Error("FindUsedMCIndices","PndMCTrack is not existing!");
196  }
197  mapMCIndex[mcIndex] = mcIndex;
198  mcIndex = mctrack->GetMotherID();
199  }
200 
201  } // Loop over PidChargedCand
202 
203  for (int ihyp = 0; ihyp < 5;ihyp++){
204  if (fPidNeutralArray[ihyp]) nCands = fPidNeutralArray[ihyp]->GetEntriesFast();
205  }
206 // nCands = fPidNeutralArray[0]->GetEntriesFast();
207  for (Int_t iPid=0; iPid<nCands; iPid++)
208  {
209 
210  Int_t mcIndex = -1;
211  for(int i=0;i<5;i++){
212  if (!fHypoFlagNeutral[i]) continue;
213  PndPidCandidate *pidCand = (PndPidCandidate*) fPidNeutralArray[i]->At(iPid);
214  mcIndex = pidCand->GetMcIndex();
215  if (mcIndex !=-1) break; //If any of the pidcandidates are pointing to a track, we exit loop
216  }
217  while (mcIndex!=-1)
218  {
219  PndMCTrack *mctrack = (PndMCTrack*)fInputArray->At(mcIndex);
220  if (mctrack==NULL)
221  {
222  Error("FindUsedMCIndices","PndMCTrack is not existing!");
223  }
224  mapMCIndex[mcIndex] = mcIndex;
225  mcIndex = mctrack->GetMotherID();
226  }
227  } // Loop over PidNeutralCand
228 
229 }
230 // -------------------------------------------------------------------------
231 
232 // ----- Protected method FindUsedMcIndices --------------------------------------------
234 {
235  // Copy only the MCTracks which were used, and update the mother indices
236 
237  for (std::map<Int_t,Int_t>::iterator it=mapMCIndex.begin(); it!=mapMCIndex.end(); ++it)
238  {
239  PndMCTrack *mctrack = (PndMCTrack*) fInputArray->At(it->first);
240  TClonesArray& clref = *fOutputArray;
241  Int_t size = clref.GetEntriesFast();
242  new(clref[size]) PndMCTrack(*mctrack);
243  mapMCIndex[it->first] = size;
244  }
245 }
246 // -------------------------------------------------------------------------
247 
248 
249 // ----- Protected method CorrectMotherIndices --------------------------------------------
251 {
252  // Loop over the new MCTrack TClonesArray and update the mother indices
253  Int_t nmc = 0;
254  nmc = fOutputArray->GetEntriesFast();
255  for (Int_t imc=0; imc<nmc; imc++)
256  {
258  Int_t motherID = mctrack->GetMotherID();
259  Int_t secondMotherID = mctrack->GetSecondMotherID();
260  if (motherID!=-1) mctrack->SetMotherID(mapMCIndex[motherID]);
261  if (secondMotherID!=-1) mctrack->SetSecondMotherID(mapMCIndex[secondMotherID]);
262  }
263 }
264 // ----- Protected method CorrectMotherIndices --------------------------------------------
266 {
267  // Loop over Pid Candidates and set the mc indices with the new value
268 
269  Int_t nCands = 0;
270 
271  for(int i=0;i<5;i++){
272  if (!fHypoFlagCharged[i]) continue;
273  nCands = fPidChargedArray[i]->GetEntriesFast();
274  for (Int_t iPid=0; iPid<nCands; iPid++)
275  {
276 
277  PndPidCandidate *pidCand = (PndPidCandidate*) fPidChargedArray[i]->At(iPid);
278  Int_t mcIndex = pidCand->GetMcIndex();
279  pidCand->SetMcIndex(mapMCIndex[mcIndex]);
280  }
281  }
282  for(int i=0;i<5;i++){
283  if (!fHypoFlagNeutral[i]) continue;
284  nCands = fPidNeutralArray[i]->GetEntriesFast();
285  for (Int_t iPid=0; iPid<nCands; iPid++)
286  {
287  PndPidCandidate *pidCand = (PndPidCandidate*) fPidNeutralArray[i]->At(iPid);
288  Int_t mcIndex = pidCand->GetMcIndex();
289  pidCand->SetMcIndex(mapMCIndex[mcIndex]);
290  }
291  }
292 
293 
294 }
PndMCTrack * mctrack
virtual void Exec(Option_t *)
Int_t i
Definition: run_full.C:25
virtual InitStatus Init()
Bool_t fHypoFlagCharged[5]
Definition: PndMcCloner2.h:62
void SetMcIndex(int idx)
Int_t GetMcIndex() const
void CloneMCTrack()
cout<< "blue = Monte Carlo "<< endl;cout<< "red = Helix Hit "<< endl;cout<< "green = Center Of Tubes "<< endl;for(Int_t k=0;k< track->GetEntriesFast();k++){PndSttTrack *stttrack=(PndSttTrack *) track-> At(k)
Definition: checkhelixhit.C:56
TString fTrackBranchNamePidHypo
Flag to check which hypotheses are present.
Definition: PndMcCloner2.h:65
TClonesArray * fOutputArray
Definition: PndMcCloner2.h:55
TClonesArray * fPidNeutralArray[5]
Definition: PndMcCloner2.h:51
Bool_t fHypoFlagNeutral[5]
Flag to check which hypotheses are present.
Definition: PndMcCloner2.h:63
void FindUsedMCIndices()
void CorrectMotherIndices()
Bool_t fCleanMC
Definition: PndMcCloner2.h:59
Int_t GetSecondMotherID() const
Definition: PndMCTrack.h:75
void CloneAndCleanMCTrack()
ClassImp(PndAnaContFact)
TClonesArray * fPidChargedArray[5]
Definition: PndMcCloner2.h:48
void SetSecondMotherID(Int_t id)
Definition: PndMCTrack.h:94
void SetMotherID(Int_t id)
Definition: PndMCTrack.h:93
void CorrectPidIndices()
Int_t GetMotherID() const
Definition: PndMCTrack.h:74
map< Int_t, Int_t > mapMCIndex
Definition: PndMcCloner2.h:57
TString fPidHypoStr[5]
Definition: PndMcCloner2.h:61
TClonesArray * fInputArray
Definition: PndMcCloner2.h:45