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