]>
Commit | Line | Data |
---|---|---|
da97a08a | 1 | /************************************************************************* * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved * |
5fe09262 | 2 | * * |
3 | * Author: The ALICE Off-line Project. * | |
4 | * Contributors are mentioned in the code where appropriate. * | |
5 | * * | |
6 | * Permission to use, copy, modify and distribute this software and its * | |
7 | * documentation strictly for non-commercial purposes is hereby granted * | |
8 | * without fee, provided that the above copyright notice appears in all * | |
9 | * copies and that both the copyright notice and this permission notice * | |
10 | * appear in the supporting documentation. The authors make no claims * | |
11 | * about the suitability of this software for any purpose. It is * | |
12 | * provided "as is" without express or implied warranty. * | |
13 | **************************************************************************/ | |
14 | ||
15 | /* $Id$ */ | |
16 | //--------------------------------------------------------------------------------- | |
17 | // Class AliMCEventHandler | |
18 | // This class gives access to MC truth during the analysis. | |
19 | // Monte Carlo truth is containe in the kinematics tree (produced particles) and | |
20 | // the tree of reference hits. | |
21 | // | |
22 | // Origin: Andreas Morsch, CERN, andreas.morsch@cern.ch | |
23 | //--------------------------------------------------------------------------------- | |
24 | ||
25 | ||
26 | ||
27 | #include "AliMCEventHandler.h" | |
415d9f5c | 28 | #include "AliMCEvent.h" |
82418625 | 29 | #include "AliPDG.h" |
5fe09262 | 30 | #include "AliTrackReference.h" |
31 | #include "AliHeader.h" | |
32 | #include "AliStack.h" | |
2c36081e | 33 | #include "AliLog.h" |
5fe09262 | 34 | |
35 | #include <TTree.h> | |
36 | #include <TFile.h> | |
37 | #include <TParticle.h> | |
0a05cd41 | 38 | #include <TString.h> |
5fe09262 | 39 | #include <TClonesArray.h> |
40 | #include <TDirectoryFile.h> | |
5fe09262 | 41 | |
42 | ClassImp(AliMCEventHandler) | |
43 | ||
44 | AliMCEventHandler::AliMCEventHandler() : | |
d2f1d9ef | 45 | AliVEventHandler(), |
415d9f5c | 46 | fMCEvent(new AliMCEvent()), |
5fe09262 | 47 | fFileE(0), |
48 | fFileK(0), | |
49 | fFileTR(0), | |
5fe09262 | 50 | fTreeE(0), |
51 | fTreeK(0), | |
52 | fTreeTR(0), | |
5efedd31 | 53 | fDirK(0), |
54 | fDirTR(0), | |
da97a08a | 55 | fParticleSelected(0), |
56 | fLabelMap(0), | |
5fe09262 | 57 | fNEvent(-1), |
58 | fEvent(-1), | |
0a05cd41 | 59 | fPathName(new TString("./")), |
9aea8469 | 60 | fExtension(""), |
61 | fFileNumber(0), | |
969c7896 | 62 | fEventsPerFile(0), |
c8b7b5d3 | 63 | fReadTR(kTRUE), |
64 | fInitOk(kFALSE) | |
5fe09262 | 65 | { |
82418625 | 66 | // |
67 | // Default constructor | |
68 | // | |
69 | // Be sure to add all particles to the PDG database | |
70 | AliPDG::AddParticlesToPdgDataBase(); | |
5fe09262 | 71 | } |
72 | ||
73 | AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) : | |
d2f1d9ef | 74 | AliVEventHandler(name, title), |
415d9f5c | 75 | fMCEvent(new AliMCEvent()), |
5fe09262 | 76 | fFileE(0), |
77 | fFileK(0), | |
78 | fFileTR(0), | |
5fe09262 | 79 | fTreeE(0), |
80 | fTreeK(0), | |
81 | fTreeTR(0), | |
5efedd31 | 82 | fDirK(0), |
83 | fDirTR(0), | |
da97a08a | 84 | fParticleSelected(0), |
85 | fLabelMap(0), | |
5fe09262 | 86 | fNEvent(-1), |
87 | fEvent(-1), | |
0a05cd41 | 88 | fPathName(new TString("./")), |
9aea8469 | 89 | fExtension(""), |
90 | fFileNumber(0), | |
0cd61c1d | 91 | fEventsPerFile(0), |
c8b7b5d3 | 92 | fReadTR(kTRUE), |
93 | fInitOk(kFALSE) | |
5fe09262 | 94 | { |
82418625 | 95 | // |
96 | // Constructor | |
97 | // | |
98 | // Be sure to add all particles to the PDG database | |
99 | AliPDG::AddParticlesToPdgDataBase(); | |
5fe09262 | 100 | } |
101 | AliMCEventHandler::~AliMCEventHandler() | |
102 | { | |
103 | // Destructor | |
415d9f5c | 104 | delete fMCEvent; |
5fe09262 | 105 | delete fFileE; |
106 | delete fFileK; | |
107 | delete fFileTR; | |
108 | } | |
109 | ||
300d5701 | 110 | Bool_t AliMCEventHandler::Init(Option_t* opt) |
5fe09262 | 111 | { |
112 | // Initialize input | |
113 | // | |
6073f8c9 | 114 | if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE; |
115 | // | |
0a05cd41 | 116 | fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data())); |
c8b7b5d3 | 117 | if (!fFileE) { |
118 | AliError(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data())); | |
119 | fInitOk = kFALSE; | |
120 | return kFALSE; | |
121 | } | |
122 | ||
415d9f5c | 123 | // |
124 | // Tree E | |
5fe09262 | 125 | fFileE->GetObject("TE", fTreeE); |
415d9f5c | 126 | // Connect Tree E to the MCEvent |
127 | fMCEvent->ConnectTreeE(fTreeE); | |
5fe09262 | 128 | fNEvent = fTreeE->GetEntries(); |
129 | // | |
130 | // Tree K | |
0a05cd41 | 131 | fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension)); |
c8b7b5d3 | 132 | if (!fFileK) { |
133 | AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName)); | |
134 | fInitOk = kFALSE; | |
135 | return kFALSE; | |
136 | } | |
137 | ||
9aea8469 | 138 | fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs(); |
5fe09262 | 139 | // |
140 | // Tree TR | |
969c7896 | 141 | if (fReadTR) { |
c8b7b5d3 | 142 | fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension)); |
143 | if (!fFileTR) { | |
144 | AliError(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data())); | |
145 | fInitOk = kFALSE; | |
146 | return kFALSE; | |
147 | } | |
969c7896 | 148 | } |
5fe09262 | 149 | // |
150 | // Reset the event number | |
2c36081e | 151 | fEvent = -1; |
152 | fFileNumber = 0; | |
b544c64d | 153 | AliInfo(Form("Number of events in this directory %5d \n", fNEvent)); |
c8b7b5d3 | 154 | fInitOk = kTRUE; |
5fe09262 | 155 | return kTRUE; |
5fe09262 | 156 | } |
157 | ||
9aea8469 | 158 | Bool_t AliMCEventHandler::GetEvent(Int_t iev) |
159 | { | |
160 | // Load the event number iev | |
2c36081e | 161 | // |
162 | // Calculate the file number | |
c8b7b5d3 | 163 | if (!fInitOk) return kFALSE; |
164 | ||
415d9f5c | 165 | Int_t inew = iev / fEventsPerFile; |
9aea8469 | 166 | if (inew != fFileNumber) { |
167 | fFileNumber = inew; | |
168 | if (!OpenFile(fFileNumber)){ | |
169 | return kFALSE; | |
170 | } | |
171 | } | |
2c36081e | 172 | // Folder name |
5fe09262 | 173 | char folder[20]; |
9aea8469 | 174 | sprintf(folder, "Event%d", iev); |
5fe09262 | 175 | // TreeE |
9aea8469 | 176 | fTreeE->GetEntry(iev); |
5fe09262 | 177 | // Tree K |
5efedd31 | 178 | fFileK->GetObject(folder, fDirK); |
179 | if (!fDirK) { | |
2c36081e | 180 | AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev)); |
9aea8469 | 181 | return kFALSE; |
182 | } | |
5efedd31 | 183 | fDirK ->GetObject("TreeK", fTreeK); |
415d9f5c | 184 | // Connect TreeK to MCEvent |
185 | fMCEvent->ConnectTreeK(fTreeK); | |
5fe09262 | 186 | //Tree TR |
07bd4750 | 187 | if (fFileTR) { |
5efedd31 | 188 | // Check which format has been read |
189 | fFileTR->GetObject(folder, fDirTR); | |
190 | fDirTR->GetObject("TreeTR", fTreeTR); | |
415d9f5c | 191 | // |
192 | // Connect TR to MCEvent | |
193 | fMCEvent->ConnectTreeTR(fTreeTR); | |
5fe09262 | 194 | } |
5fe09262 | 195 | // |
5fe09262 | 196 | return kTRUE; |
9aea8469 | 197 | } |
198 | ||
199 | Bool_t AliMCEventHandler::OpenFile(Int_t i) | |
200 | { | |
201 | // Open file i | |
9aea8469 | 202 | if (i > 0) { |
203 | fExtension = Form("%d", i); | |
204 | } else { | |
205 | fExtension = ""; | |
206 | } | |
207 | ||
208 | ||
209 | delete fFileK; | |
0a05cd41 | 210 | fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension)); |
9aea8469 | 211 | if (!fFileK) { |
c8b7b5d3 | 212 | AliError(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data())); |
213 | fInitOk = kFALSE; | |
214 | return kFALSE; | |
9aea8469 | 215 | } |
216 | ||
c8b7b5d3 | 217 | if (fReadTR) { |
218 | delete fFileTR; | |
219 | fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension)); | |
220 | if (!fFileTR) { | |
221 | AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data())); | |
222 | fInitOk = kFALSE; | |
223 | return kFALSE; | |
224 | } | |
9aea8469 | 225 | } |
226 | ||
c8b7b5d3 | 227 | fInitOk = kTRUE; |
228 | return kTRUE; | |
9aea8469 | 229 | } |
230 | ||
ed97dc98 | 231 | Bool_t AliMCEventHandler::BeginEvent(Long64_t entry) |
9aea8469 | 232 | { |
da97a08a | 233 | fParticleSelected.Delete(); |
234 | fLabelMap.Delete(); | |
9aea8469 | 235 | // Read the next event |
ed97dc98 | 236 | if (entry == -1) { |
237 | fEvent++; | |
238 | entry = fEvent; | |
239 | } else { | |
240 | fEvent = entry; | |
241 | } | |
242 | ||
243 | if (entry >= fNEvent) { | |
f6065654 | 244 | AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry,fNEvent)); |
9aea8469 | 245 | return kFALSE; |
246 | } | |
ed97dc98 | 247 | return GetEvent(entry); |
5fe09262 | 248 | } |
249 | ||
da97a08a | 250 | void AliMCEventHandler::SelectParticle(Int_t i){ |
251 | // taking the absolute values here, need to take care | |
252 | // of negative daughter and mother | |
253 | // IDs when setting! | |
254 | if(!IsParticleSelected(TMath::Abs(i)))fParticleSelected.Add(TMath::Abs(i),1); | |
255 | } | |
256 | ||
257 | Bool_t AliMCEventHandler::IsParticleSelected(Int_t i) { | |
258 | // taking the absolute values here, need to take | |
259 | // care with negative daughter and mother | |
260 | // IDs when setting! | |
261 | return (fParticleSelected.GetValue(TMath::Abs(i))==1); | |
262 | } | |
263 | ||
264 | ||
265 | void AliMCEventHandler::CreateLabelMap(){ | |
266 | ||
267 | // | |
268 | // this should be called once all selections where done | |
269 | // | |
270 | ||
271 | fLabelMap.Delete(); | |
272 | if(!fMCEvent){ | |
273 | fParticleSelected.Delete(); | |
274 | return; | |
275 | } | |
276 | ||
277 | VerifySelectedParticles(); | |
278 | AliStack *pStack = fMCEvent->Stack(); | |
279 | ||
280 | Int_t iNew = 0; | |
281 | for(int i = 0;i < pStack->GetNtrack();++i){ | |
282 | if(IsParticleSelected(i)){ | |
283 | fLabelMap.Add(i,iNew); | |
284 | iNew++; | |
285 | } | |
286 | } | |
287 | } | |
288 | ||
289 | Int_t AliMCEventHandler::GetNewLabel(Int_t i) { | |
290 | // Gets the labe from the new created Map | |
291 | // Call CreatLabelMap before | |
292 | // otherwise only 0 returned | |
293 | return fLabelMap.GetValue(TMath::Abs(i)); | |
294 | } | |
295 | ||
296 | void AliMCEventHandler::VerifySelectedParticles(){ | |
297 | ||
298 | // | |
299 | // Make sure that each particle has at least it's predecessors | |
300 | // selected so that we have the complete ancestry tree | |
301 | // Private, should be only called by CreateLabelMap | |
302 | ||
303 | if(!fMCEvent){ | |
304 | fParticleSelected.Delete(); | |
305 | return; | |
306 | } | |
307 | AliStack *pStack = fMCEvent->Stack(); | |
308 | ||
309 | Int_t nprim = pStack->GetNprimary(); | |
310 | ||
311 | for(int i = 0;i < pStack->GetNtrack();++i){ | |
312 | if(i<nprim){ | |
313 | SelectParticle(i);// take all primaries | |
314 | continue; | |
315 | } | |
316 | if(!IsParticleSelected(i))continue; | |
317 | TParticle *part = pStack->Particle(i); | |
318 | Int_t imo = part->GetFirstMother(); | |
319 | while((imo >= nprim)&&!IsParticleSelected(imo)){ | |
320 | // Mother not yet selected | |
321 | SelectParticle(imo); | |
322 | TParticle *mother = pStack->Particle(imo); | |
323 | imo = mother->GetFirstMother(); | |
324 | } | |
325 | // after last step we may have a unselected primary | |
326 | // mother | |
327 | if(imo>=0){ | |
328 | if(!IsParticleSelected(imo)) | |
329 | SelectParticle(imo); | |
330 | } | |
331 | }// loop over all tracks | |
332 | } | |
333 | ||
5fe09262 | 334 | Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs) |
335 | { | |
336 | // Retrieve entry i | |
c8b7b5d3 | 337 | if (!fInitOk) { |
338 | return 0; | |
339 | } else { | |
340 | return (fMCEvent->GetParticleAndTR(i, particle, trefs)); | |
341 | } | |
5fe09262 | 342 | } |
343 | ||
2d8f26f6 | 344 | void AliMCEventHandler::DrawCheck(Int_t i, Int_t search) |
5fe09262 | 345 | { |
346 | // Retrieve entry i and draw momentum vector and hits | |
415d9f5c | 347 | fMCEvent->DrawCheck(i, search); |
5fe09262 | 348 | } |
349 | ||
890126ab | 350 | Bool_t AliMCEventHandler::Notify(const char *path) |
5fe09262 | 351 | { |
53faeca4 | 352 | // Notify about directory change |
353 | // The directory is taken from the 'path' argument | |
354 | // Reconnect trees | |
355 | TString fileName(path); | |
356 | if(fileName.Contains("AliESDs.root")){ | |
357 | fileName.ReplaceAll("AliESDs.root", ""); | |
358 | } | |
c8b5ce3a | 359 | else if(fileName.Contains("AliAOD.root")){ |
360 | fileName.ReplaceAll("AliAOD.root", ""); | |
361 | } | |
53faeca4 | 362 | else if(fileName.Contains("galice.root")){ |
363 | // for running with galice and kinematics alone... | |
364 | fileName.ReplaceAll("galice.root", ""); | |
365 | } | |
366 | ||
367 | *fPathName = fileName; | |
b544c64d | 368 | AliInfo(Form("Notify() Path: %s\n", fPathName->Data())); |
53faeca4 | 369 | |
f6065654 | 370 | ResetIO(); |
371 | InitIO(""); | |
372 | ||
5fe09262 | 373 | return kTRUE; |
374 | } | |
36e82a52 | 375 | |
5fe09262 | 376 | void AliMCEventHandler::ResetIO() |
377 | { | |
5efedd31 | 378 | // Clear header and stack |
aff49450 | 379 | if (fInitOk) fMCEvent->Clean(); |
5efedd31 | 380 | |
36e82a52 | 381 | // Delete Tree E |
415d9f5c | 382 | delete fTreeE; fTreeE = 0; |
36e82a52 | 383 | |
5efedd31 | 384 | // Reset files |
89f249fc | 385 | if (fFileE) {delete fFileE; fFileE = 0;} |
386 | if (fFileK) {delete fFileK; fFileK = 0;} | |
387 | if (fFileTR) {delete fFileTR; fFileTR = 0;} | |
19cfde04 | 388 | fExtension=""; |
c8b7b5d3 | 389 | fInitOk = kFALSE; |
5fe09262 | 390 | } |
391 | ||
392 | ||
393 | Bool_t AliMCEventHandler::FinishEvent() | |
394 | { | |
5efedd31 | 395 | // Clean-up after each event |
396 | delete fDirTR; fDirTR = 0; | |
397 | delete fDirK; fDirK = 0; | |
aff49450 | 398 | if (fInitOk) fMCEvent->FinishEvent(); |
5fe09262 | 399 | return kTRUE; |
400 | } | |
401 | ||
402 | Bool_t AliMCEventHandler::Terminate() | |
403 | { | |
404 | // Dummy | |
405 | return kTRUE; | |
406 | } | |
407 | ||
408 | Bool_t AliMCEventHandler::TerminateIO() | |
409 | { | |
410 | // Dummy | |
411 | return kTRUE; | |
412 | } | |
413 | ||
0a05cd41 | 414 | |
0931e76a | 415 | void AliMCEventHandler::SetInputPath(const char* fname) |
0a05cd41 | 416 | { |
417 | // Set the input path name | |
418 | delete fPathName; | |
419 | fPathName = new TString(fname); | |
420 | } |