]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliMCEventHandler.cxx
Added new materials
[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 "AliTrackReference.h"
31 #include "AliHeader.h"
32 #include "AliStack.h"
33 #include "AliLog.h"
34
35 #include <TTree.h>
36 #include <TFile.h>
37 #include <TParticle.h>
38 #include <TString.h>
39 #include <TClonesArray.h>
40 #include <TDirectoryFile.h>
41
42 ClassImp(AliMCEventHandler)
43
44 AliMCEventHandler::AliMCEventHandler() :
45     AliVEventHandler(),
46     fMCEvent(new AliMCEvent()),
47     fFileE(0),
48     fFileK(0),
49     fFileTR(0),
50     fTreeE(0),
51     fTreeK(0),
52     fTreeTR(0),
53     fDirK(0),
54     fDirTR(0),
55     fNEvent(-1),
56     fEvent(-1),
57     fPathName(new TString("./")),
58     fExtension(""),
59     fFileNumber(0),
60     fEventsPerFile(0),
61     fReadTR(kTRUE)
62 {
63     // Default constructor
64 }
65
66 AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
67     AliVEventHandler(name, title),
68     fMCEvent(new AliMCEvent()),
69     fFileE(0),
70     fFileK(0),
71     fFileTR(0),
72     fTreeE(0),
73     fTreeK(0),
74     fTreeTR(0),
75     fDirK(0),
76     fDirTR(0),
77     fNEvent(-1),
78     fEvent(-1),
79     fPathName(new TString("./")),
80     fExtension(""),
81     fFileNumber(0),
82     fEventsPerFile(0),
83     fReadTR(kTRUE)
84 {
85     // Constructor
86 }
87 AliMCEventHandler::~AliMCEventHandler()
88
89     // Destructor
90     delete fMCEvent;
91     delete fFileE;
92     delete fFileK;
93     delete fFileTR;
94 }
95
96 Bool_t AliMCEventHandler::Init(Option_t* opt)
97
98     // Initialize input
99     //
100     if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
101     //
102     fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data()));
103     if (!fFileE) AliFatal(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
104
105     //
106     // Tree E
107     fFileE->GetObject("TE", fTreeE);
108     // Connect Tree E to the MCEvent
109     fMCEvent->ConnectTreeE(fTreeE);
110     fNEvent = fTreeE->GetEntries();
111     //
112     // Tree K
113     fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
114     if (!fFileK) AliFatal(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
115     fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
116     //
117     // Tree TR
118     if (fReadTR) {
119       fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
120       if (!fFileTR) AliWarning(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
121     }
122     //
123     // Reset the event number
124     fEvent      = -1;
125     fFileNumber =  0;
126     printf("AliMCEvenHandler::Init() %d\n",__LINE__);
127     AliInfo(Form("AliMCEventHandler:Number of events in this directory %5d \n", fNEvent));
128     return kTRUE;
129 }
130
131 Bool_t AliMCEventHandler::GetEvent(Int_t iev)
132 {
133     // Load the event number iev
134     //
135     // Calculate the file number
136     Int_t inew  = iev / fEventsPerFile;
137     if (inew != fFileNumber) {
138         fFileNumber = inew;
139         if (!OpenFile(fFileNumber)){
140             return kFALSE;
141         }
142     }
143     // Folder name
144     char folder[20];
145     sprintf(folder, "Event%d", iev);
146     // TreeE
147     fTreeE->GetEntry(iev);
148     // Tree K
149     fFileK->GetObject(folder, fDirK);
150     if (!fDirK) {
151         AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
152         return kFALSE;
153     }
154     fDirK ->GetObject("TreeK", fTreeK);
155     // Connect TreeK to MCEvent
156     fMCEvent->ConnectTreeK(fTreeK);
157     //Tree TR 
158     if (fFileTR) {
159         // Check which format has been read
160         fFileTR->GetObject(folder, fDirTR);
161         fDirTR->GetObject("TreeTR", fTreeTR);
162         //
163         // Connect TR to MCEvent
164         fMCEvent->ConnectTreeTR(fTreeTR);
165     }
166     //
167     return kTRUE;
168 }
169
170 Bool_t AliMCEventHandler::OpenFile(Int_t i)
171 {
172     // Open file i
173     Bool_t ok = kTRUE;
174     if (i > 0) {
175         fExtension = Form("%d", i);
176     } else {
177         fExtension = "";
178     }
179     
180     
181     delete fFileK;
182     fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
183     if (!fFileK) {
184         AliFatal(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
185         ok = kFALSE;
186     }
187     
188     delete fFileTR;
189     fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
190     if (!fFileTR) {
191         AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
192         ok = kFALSE;
193     }
194     
195     return ok;
196 }
197
198 Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
199
200     // Read the next event
201     if (entry == -1) {
202         fEvent++;
203         entry = fEvent;
204     } else {
205         fEvent = entry;
206     }
207
208     if (entry >= fNEvent) {
209         AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry,fNEvent));
210         return kFALSE;
211     }
212     return GetEvent(entry);
213 }
214
215 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
216 {
217     // Retrieve entry i
218     return (fMCEvent->GetParticleAndTR(i, particle, trefs));
219 }
220
221 void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
222 {
223     // Retrieve entry i and draw momentum vector and hits
224     fMCEvent->DrawCheck(i, search);
225 }
226
227 Bool_t AliMCEventHandler::Notify(const char *path)
228 {
229   // Notify about directory change
230   // The directory is taken from the 'path' argument
231   // Reconnect trees
232     TString fileName(path);
233     if(fileName.Contains("AliESDs.root")){
234         fileName.ReplaceAll("AliESDs.root", "");
235     }
236     else if(fileName.Contains("AliAOD.root")){
237         fileName.ReplaceAll("AliAOD.root", "");
238     }
239     else if(fileName.Contains("galice.root")){
240         // for running with galice and kinematics alone...
241         fileName.ReplaceAll("galice.root", "");
242     }
243     
244     *fPathName = fileName;
245     printf("AliMCEventHandler::Notify() Path: %s\n", fPathName->Data());
246     
247     ResetIO();
248     InitIO("");
249
250     return kTRUE;
251 }
252
253 void AliMCEventHandler::ResetIO()
254 {
255 //  Clear header and stack
256     fMCEvent->Clean();
257     
258 // Delete Tree E
259     delete fTreeE; fTreeE = 0;
260
261 // Reset files
262     if (fFileE)  {delete fFileE;  fFileE  = 0;}
263     if (fFileK)  {delete fFileK;  fFileK  = 0;}
264     if (fFileTR) {delete fFileTR; fFileTR = 0;}
265     fExtension="";
266 }
267
268                             
269 Bool_t AliMCEventHandler::FinishEvent()
270 {
271     // Clean-up after each event
272     delete fDirTR;  fDirTR = 0;
273     delete fDirK;   fDirK  = 0;    
274     fMCEvent->FinishEvent();
275     return kTRUE;
276 }
277
278 Bool_t AliMCEventHandler::Terminate()
279
280     // Dummy 
281     return kTRUE;
282 }
283
284 Bool_t AliMCEventHandler::TerminateIO()
285
286     // Dummy
287     return kTRUE;
288 }
289     
290
291 void AliMCEventHandler::SetInputPath(const char* fname)
292 {
293     // Set the input path name
294     delete fPathName;
295     fPathName = new TString(fname);
296 }