FairRoot/PandaRoot
PndMcCloner.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- PndMcCloner source file -----
3 // ----- Created 08/07/13 by S.Spataro -----
4 // -------------------------------------------------------------------------
5 
6 #include "PndMcCloner.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 PndMcCloner::PndMcCloner() : 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 PndMcCloner::Init() {
37 
38  cout << "-I- PndMcCloner::Init: "
39  << "INITIALIZATION *********************" << endl;
40 
41  //FairRun* sim = FairRun::Instance(); //[R.K. 01/2017] unused variable?
42  //FairRuntimeDb* rtdb=sim->GetRuntimeDb(); //[R.K. 01/2017] unused variable?
43 
44  // Get RootManager
45  FairRootManager* ioman = FairRootManager::Instance();
46  if ( ! ioman ) {
47  cout << "-E- PndMcCloner::Init: "
48  << "RootManager not instantiated!" << endl;
49  return kFATAL;
50  }
51 
52  // Get input array
53  fInputArray = (TClonesArray*) ioman->GetObject("MCTrack");
54  if ( ! fInputArray ) {
55  cout << "-E- PndMcCloner::Init: "
56  << "No MCTrack array!" << endl;
57  return kERROR;
58  }
59 
60  fPidChargedArray = (TClonesArray*) ioman->GetObject("PidChargedCand"+fTrackBranchNamePidHypo);
61  if ( ! fPidChargedArray ) {
62  cout << "-E- PndMcCloner::Init: "
63  << "No PidChargedCand array!" << endl;
64  return kERROR;
65  }
66 
67  fPidNeutralArray = (TClonesArray*) ioman->GetObject("PidNeutralCand"+fTrackBranchNamePidHypo);
68  if ( ! fPidNeutralArray ) {
69  cout << "-E- PndMcCloner::Init: "
70  << "No PidNeutralCand array!" << endl;
71  return kERROR;
72  }
73 
74  // Create and register output array
75  fOutputArray = new TClonesArray("PndMCTrack");
76 
77  ioman->Register("MCTrack","MC",fOutputArray,kTRUE);
78 
79  cout << "-I- PndMcCloner: Intialization successfull" << endl;
80 
81  return kSUCCESS;
82 
83 }
84 // -------------------------------------------------------------------------
85 
86 
87 
88 // ----- Public method Exec --------------------------------------------
89 void PndMcCloner::Exec(Option_t*) {
90 
91  // Reset output array
92  if ( ! fOutputArray ) Fatal("Exec", "No Output Array");
93 
94  fOutputArray->Clear();
95  mapMCIndex.clear();
96 
97  if (!fCleanMC)
98  {
99  CloneMCTrack();
100  }
101  else
102  {
107  }
108 }
109 // -------------------------------------------------------------------------
110 
111 // ----- Protected method FindUsedMcIndices --------------------------------------------
113 {
114  // Copy 1:1 of the MCTrack TClonesArray
115 
116  Int_t nMCTracks = fInputArray->GetEntriesFast();
117  for (Int_t iMC=0; iMC<nMCTracks; iMC++)
118  {
119  PndMCTrack *mctrack = (PndMCTrack*) fInputArray->At(iMC);
120  TClonesArray& clref = *fOutputArray;
121  Int_t size = clref.GetEntriesFast();
122  new(clref[size]) PndMCTrack(*mctrack);
123  } // Loop over MCTracks
124 }
125 
126 // ----- Protected method FindUsedMcIndices --------------------------------------------
128  // Loop over PidChargedCand and PidNeutralCand, find the used MC indices, and fill the map
129  // with this index and all the mother indices
130 
131  Int_t nCands = 0;
132 
133  // First store primary MC tracks
134  nCands = fInputArray->GetEntriesFast();
135  for (Int_t imc=0; imc<nCands; imc++)
136  {
138  if (mctrack->GetMotherID()==-1)
139  {
140  mapMCIndex[imc] = imc;
141  }
142  else break;
143  }
144 
145  nCands = fPidChargedArray->GetEntriesFast();
146  for (Int_t iPid=0; iPid<nCands; iPid++)
147  {
148  PndPidCandidate *pidCand = (PndPidCandidate*) fPidChargedArray->At(iPid);
149  Int_t mcIndex = pidCand->GetMcIndex();
150 
151  while (mcIndex!=-1)
152  {
153  PndMCTrack *mctrack = (PndMCTrack*)fInputArray->At(mcIndex);
154  if (mctrack==NULL)
155  {
156  Error("FindUsedMCIndices","PndMCTrack is not existing!");
157  }
158  mapMCIndex[mcIndex] = mcIndex;
159  mcIndex = mctrack->GetMotherID();
160  }
161  } // Loop over PidChargedCand
162 
163  nCands = fPidNeutralArray->GetEntriesFast();
164  for (Int_t iPid=0; iPid<nCands; iPid++)
165  {
166  PndPidCandidate *pidCand = (PndPidCandidate*) fPidNeutralArray->At(iPid);
167  Int_t mcIndex = pidCand->GetMcIndex();
168 
169  while (mcIndex!=-1)
170  {
171  PndMCTrack *mctrack = (PndMCTrack*)fInputArray->At(mcIndex);
172  if (mctrack==NULL)
173  {
174  Error("FindUsedMCIndices","PndMCTrack is not existing!");
175  }
176  mapMCIndex[mcIndex] = mcIndex;
177  mcIndex = mctrack->GetMotherID();
178  }
179  } // Loop over PidNeutralCand
180 
181 }
182 // -------------------------------------------------------------------------
183 
184 // ----- Protected method FindUsedMcIndices --------------------------------------------
186 {
187  // Copy only the MCTracks which were used, and update the mother indices
188 
189  for (std::map<Int_t,Int_t>::iterator it=mapMCIndex.begin(); it!=mapMCIndex.end(); ++it)
190  {
191  PndMCTrack *mctrack = (PndMCTrack*) fInputArray->At(it->first);
192  TClonesArray& clref = *fOutputArray;
193  Int_t size = clref.GetEntriesFast();
194  new(clref[size]) PndMCTrack(*mctrack);
195  mapMCIndex[it->first] = size;
196  }
197 }
198 // -------------------------------------------------------------------------
199 
200 
201 // ----- Protected method CorrectMotherIndices --------------------------------------------
203 {
204  // Loop over the new MCTrack TClonesArray and update the mother indices
205  Int_t nmc = 0;
206  nmc = fOutputArray->GetEntriesFast();
207  for (Int_t imc=0; imc<nmc; imc++)
208  {
210  Int_t motherID = mctrack->GetMotherID();
211  Int_t secondMotherID = mctrack->GetSecondMotherID();
212  if (motherID!=-1) mctrack->SetMotherID(mapMCIndex[motherID]);
213  if (secondMotherID!=-1) mctrack->SetSecondMotherID(mapMCIndex[secondMotherID]);
214  }
215 }
216 // ----- Protected method CorrectMotherIndices --------------------------------------------
218 {
219  // Loop over Pid Candidates and set the mc indices with the new value
220 
221  Int_t nCands = 0;
222 
223  nCands = fPidChargedArray->GetEntriesFast();
224  for (Int_t iPid=0; iPid<nCands; iPid++)
225  {
226  PndPidCandidate *pidCand = (PndPidCandidate*) fPidChargedArray->At(iPid);
227  Int_t mcIndex = pidCand->GetMcIndex();
228  pidCand->SetMcIndex(mapMCIndex[mcIndex]);
229  }
230 
231  nCands = fPidNeutralArray->GetEntriesFast();
232  for (Int_t iPid=0; iPid<nCands; iPid++)
233  {
234  PndPidCandidate *pidCand = (PndPidCandidate*) fPidNeutralArray->At(iPid);
235  Int_t mcIndex = pidCand->GetMcIndex();
236  pidCand->SetMcIndex(mapMCIndex[mcIndex]);
237  }
238 
239 }
PndMCTrack * mctrack
TClonesArray * fPidNeutralArray
Definition: PndMcCloner.h:51
virtual void Exec(Option_t *)
Definition: PndMcCloner.cxx:89
TString fTrackBranchNamePidHypo
Definition: PndMcCloner.h:61
void CorrectPidIndices()
Bool_t fCleanMC
Definition: PndMcCloner.h:59
void SetMcIndex(int idx)
TClonesArray * fOutputArray
Definition: PndMcCloner.h:55
Int_t GetMcIndex() const
void CloneAndCleanMCTrack()
map< Int_t, Int_t > mapMCIndex
Definition: PndMcCloner.h:57
void FindUsedMCIndices()
Int_t GetSecondMotherID() const
Definition: PndMCTrack.h:75
ClassImp(PndAnaContFact)
void SetSecondMotherID(Int_t id)
Definition: PndMCTrack.h:94
virtual InitStatus Init()
Definition: PndMcCloner.cxx:36
void SetMotherID(Int_t id)
Definition: PndMCTrack.h:93
void CorrectMotherIndices()
Int_t GetMotherID() const
Definition: PndMCTrack.h:74
void CloneMCTrack()
TClonesArray * fPidChargedArray
Definition: PndMcCloner.h:48
TClonesArray * fInputArray
Definition: PndMcCloner.h:45