]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliMCEventHandler.cxx
Adding particles to PDG DataBase in c'tor so we always have it when reading kinematics
[u/mrichter/AliRoot.git] / STEER / AliMCEventHandler.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17 //---------------------------------------------------------------------------------
18 //                          Class AliMCEventHandler
19 // This class gives access to MC truth during the analysis.
20 // Monte Carlo truth is containe in the kinematics tree (produced particles) and 
21 // the tree of reference hits.
22 //      
23 // Origin: Andreas Morsch, CERN, andreas.morsch@cern.ch 
24 //---------------------------------------------------------------------------------
25
26
27
28 #include "AliMCEventHandler.h"
29 #include "AliMCEvent.h"
30 #include "AliPDG.h"
31 #include "AliTrackReference.h"
32 #include "AliHeader.h"
33 #include "AliStack.h"
34 #include "AliLog.h"
35
36 #include <TTree.h>
37 #include <TFile.h>
38 #include <TParticle.h>
39 #include <TString.h>
40 #include <TClonesArray.h>
41 #include <TDirectoryFile.h>
42
43 ClassImp(AliMCEventHandler)
44
45 AliMCEventHandler::AliMCEventHandler() :
46     AliVEventHandler(),
47     fMCEvent(new AliMCEvent()),
48     fFileE(0),
49     fFileK(0),
50     fFileTR(0),
51     fTreeE(0),
52     fTreeK(0),
53     fTreeTR(0),
54     fDirK(0),
55     fDirTR(0),
56     fNEvent(-1),
57     fEvent(-1),
58     fPathName(new TString("./")),
59     fExtension(""),
60     fFileNumber(0),
61     fEventsPerFile(0),
62     fReadTR(kTRUE)
63 {
64   //
65   // Default constructor
66   //
67   // Be sure to add all particles to the PDG database
68   AliPDG::AddParticlesToPdgDataBase();
69 }
70
71 AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
72     AliVEventHandler(name, title),
73     fMCEvent(new AliMCEvent()),
74     fFileE(0),
75     fFileK(0),
76     fFileTR(0),
77     fTreeE(0),
78     fTreeK(0),
79     fTreeTR(0),
80     fDirK(0),
81     fDirTR(0),
82     fNEvent(-1),
83     fEvent(-1),
84     fPathName(new TString("./")),
85     fExtension(""),
86     fFileNumber(0),
87     fEventsPerFile(0),
88     fReadTR(kTRUE)
89 {
90   //
91   // Constructor
92   //
93   // Be sure to add all particles to the PDG database
94   AliPDG::AddParticlesToPdgDataBase();
95 }
96 AliMCEventHandler::~AliMCEventHandler()
97
98     // Destructor
99     delete fMCEvent;
100     delete fFileE;
101     delete fFileK;
102     delete fFileTR;
103 }
104
105 Bool_t AliMCEventHandler::Init(Option_t* opt)
106
107     // Initialize input
108     //
109     if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
110     //
111     fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data()));
112     if (!fFileE) AliFatal(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
113
114     //
115     // Tree E
116     fFileE->GetObject("TE", fTreeE);
117     // Connect Tree E to the MCEvent
118     fMCEvent->ConnectTreeE(fTreeE);
119     fNEvent = fTreeE->GetEntries();
120     //
121     // Tree K
122     fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
123     if (!fFileK) AliFatal(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
124     fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
125     //
126     // Tree TR
127     if (fReadTR) {
128       fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
129       if (!fFileTR) AliWarning(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
130     }
131     //
132     // Reset the event number
133     fEvent      = -1;
134     fFileNumber =  0;
135     printf("AliMCEvenHandler::Init() %d\n",__LINE__);
136     AliInfo(Form("AliMCEventHandler:Number of events in this directory %5d \n", fNEvent));
137     return kTRUE;
138 }
139
140 Bool_t AliMCEventHandler::GetEvent(Int_t iev)
141 {
142     // Load the event number iev
143     //
144     // Calculate the file number
145     Int_t inew  = iev / fEventsPerFile;
146     if (inew != fFileNumber) {
147         fFileNumber = inew;
148         if (!OpenFile(fFileNumber)){
149             return kFALSE;
150         }
151     }
152     // Folder name
153     char folder[20];
154     sprintf(folder, "Event%d", iev);
155     // TreeE
156     fTreeE->GetEntry(iev);
157     // Tree K
158     fFileK->GetObject(folder, fDirK);
159     if (!fDirK) {
160         AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
161         return kFALSE;
162     }
163     fDirK ->GetObject("TreeK", fTreeK);
164     // Connect TreeK to MCEvent
165     fMCEvent->ConnectTreeK(fTreeK);
166     //Tree TR 
167     if (fFileTR) {
168         // Check which format has been read
169         fFileTR->GetObject(folder, fDirTR);
170         fDirTR->GetObject("TreeTR", fTreeTR);
171         //
172         // Connect TR to MCEvent
173         fMCEvent->ConnectTreeTR(fTreeTR);
174     }
175     //
176     return kTRUE;
177 }
178
179 Bool_t AliMCEventHandler::OpenFile(Int_t i)
180 {
181     // Open file i
182     Bool_t ok = kTRUE;
183     if (i > 0) {
184         fExtension = Form("%d", i);
185     } else {
186         fExtension = "";
187     }
188     
189     
190     delete fFileK;
191     fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
192     if (!fFileK) {
193         AliFatal(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
194         ok = kFALSE;
195     }
196     
197     delete fFileTR;
198     fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
199     if (!fFileTR) {
200         AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
201         ok = kFALSE;
202     }
203     
204     return ok;
205 }
206
207 Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
208
209     // Read the next event
210     if (entry == -1) {
211         fEvent++;
212         entry = fEvent;
213     } else {
214         fEvent = entry;
215     }
216
217     if (entry >= fNEvent) {
218         AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry,fNEvent));
219         return kFALSE;
220     }
221     return GetEvent(entry);
222 }
223
224 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
225 {
226     // Retrieve entry i
227     return (fMCEvent->GetParticleAndTR(i, particle, trefs));
228 }
229
230 void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
231 {
232     // Retrieve entry i and draw momentum vector and hits
233     fMCEvent->DrawCheck(i, search);
234 }
235
236 Bool_t AliMCEventHandler::Notify(const char *path)
237 {
238   // Notify about directory change
239   // The directory is taken from the 'path' argument
240   // Reconnect trees
241     TString fileName(path);
242     if(fileName.Contains("AliESDs.root")){
243         fileName.ReplaceAll("AliESDs.root", "");
244     }
245     else if(fileName.Contains("AliAOD.root")){
246         fileName.ReplaceAll("AliAOD.root", "");
247     }
248     else if(fileName.Contains("galice.root")){
249         // for running with galice and kinematics alone...
250         fileName.ReplaceAll("galice.root", "");
251     }
252     
253     *fPathName = fileName;
254     printf("AliMCEventHandler::Notify() Path: %s\n", fPathName->Data());
255     
256     ResetIO();
257     InitIO("");
258
259     return kTRUE;
260 }
261
262 void AliMCEventHandler::ResetIO()
263 {
264 //  Clear header and stack
265     fMCEvent->Clean();
266     
267 // Delete Tree E
268     delete fTreeE; fTreeE = 0;
269
270 // Reset files
271     if (fFileE)  {delete fFileE;  fFileE  = 0;}
272     if (fFileK)  {delete fFileK;  fFileK  = 0;}
273     if (fFileTR) {delete fFileTR; fFileTR = 0;}
274     fExtension="";
275 }
276
277                             
278 Bool_t AliMCEventHandler::FinishEvent()
279 {
280     // Clean-up after each event
281     delete fDirTR;  fDirTR = 0;
282     delete fDirK;   fDirK  = 0;    
283     fMCEvent->FinishEvent();
284     return kTRUE;
285 }
286
287 Bool_t AliMCEventHandler::Terminate()
288
289     // Dummy 
290     return kTRUE;
291 }
292
293 Bool_t AliMCEventHandler::TerminateIO()
294
295     // Dummy
296     return kTRUE;
297 }
298     
299
300 void AliMCEventHandler::SetInputPath(const char* fname)
301 {
302     // Set the input path name
303     delete fPathName;
304     fPathName = new TString(fname);
305 }