]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/TRD/AliTRDonlineTrackletFilter.cxx
Fix coverity issues
[u/mrichter/AliRoot.git] / PWGPP / TRD / AliTRDonlineTrackletFilter.cxx
1 // AliTRDonlineTrackletFilter implements an analysis task which reads
2 // the tracklets from the TRD.Tracklets.root file and makes them
3 // available to other analysis tasks. This allows to access the full
4 // tracklet information without repeating the techncicalities in each
5 // analysis task. The tracklets are made available in an exchange
6 // container on the output slot 1.
7
8 #include "TFile.h"
9 #include "TTree.h"
10 #include "TChain.h"
11 #include "TH1F.h"
12 #include "TH2F.h"
13 #include "TProfile.h"
14 #include "TCanvas.h"
15
16 #include "AliAnalysisManager.h"
17 #include "AliESDEvent.h"
18 #include "AliAODEvent.h"
19 #include "AliMCEvent.h"
20 #include "AliESDInputHandler.h"
21 #include "AliAODHandler.h"
22 #include "AliMCEventHandler.h"
23 #include "AliLog.h"
24 #include "AliESDTrdTrack.h"
25
26 #include "AliTRDtrackletMCM.h"
27 #include "AliTRDtrackletWord.h"
28 #include "AliVParticle.h"
29 #include "AliMCParticle.h" 
30
31 #include "AliTRDonlineTrackletFilter.h"
32
33 ClassImp(AliTRDonlineTrackletFilter)
34
35 AliTRDonlineTrackletFilter::AliTRDonlineTrackletFilter(const char *name) :
36   AliAnalysisTask(name, ""),
37   fESD(0x0),
38   fInputHandler(0x0),
39   fInputEvent(0x0),
40   fOutputAOD(0x0),
41   fMCEvent(0x0),
42   fTrackletsRaw(new TClonesArray("AliTRDtrackletWord")),
43   fTrackletsSim(new TClonesArray("AliTRDtrackletMCM")),
44   fTrackletTree(0x0),
45   fGeo(new AliTRDgeometry),
46   fNevent(0),
47   fPath(""),
48   fTrackletFile(0x0),
49   fNEventsPerFile(0),
50   fEvent(0),
51   fFileNumber(0),
52   fTrackletTreeSim(0x0),
53   fTrackletTreeRaw(0x0)
54 {
55   // ctor
56
57   DefineInput(0, TChain::Class());
58
59   DefineOutput(0, TTree::Class()); 
60   DefineOutput(1, TTree::Class());
61 }
62
63 AliTRDonlineTrackletFilter::~AliTRDonlineTrackletFilter()
64 {
65   // dtor
66
67   delete fTrackletsRaw;
68   delete fTrackletsSim;
69   delete fGeo;
70 }
71
72 void AliTRDonlineTrackletFilter::ConnectInputData(const Option_t */* option */)
73 {
74   // connect all the input handlers to access the data
75
76   fInputHandler = (AliInputEventHandler*) AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler();
77   if (fInputHandler)
78     fInputEvent = fInputHandler->GetEvent();
79
80   AliMCEventHandler *mcH = (AliMCEventHandler*) AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler();
81   if (mcH)
82     fMCEvent = mcH->MCEvent();
83 }
84
85 void AliTRDonlineTrackletFilter::CreateOutputObjects()
86 {
87   // create the output objects used to make the tracklets available to
88   // other tasks
89
90   OpenFile(1); 
91   
92   fTrackletTree = new TTree("TRDtrackletfilter", "on-line tracklets");
93   fTrackletTree->Branch("tracklets_sim", fTrackletsSim);
94   fTrackletTree->Branch("tracklets_raw", fTrackletsRaw);
95
96   PostData(1, fTrackletTree);
97 }
98
99 Bool_t AliTRDonlineTrackletFilter::Notify()
100 {
101   // we use notify to figure out which TRD.Tracklets.root file we have
102   // to use for accessing the tracklets
103
104   TString filename(AliAnalysisManager::GetAnalysisManager()->GetTree()->GetCurrentFile()->GetName());
105
106   AliDebug(1, Form("Now reading from %s", filename.Data()));
107
108   if (filename.Contains("AliAOD.root")) 
109     filename.ReplaceAll("AliAOD.root", "");
110   else if (filename.Contains("NewAliESDs.root"))
111     filename.ReplaceAll("NewAliESDs.root", "");
112   else if (filename.Contains("AliESDs.root"))
113     filename.ReplaceAll("AliESDs.root", "");
114   else if (filename.Contains("galice.root"))
115     filename.ReplaceAll("galice.root", "");
116   else if (filename.BeginsWith("root:"))
117     filename.Append("?ZIP=");
118
119   fPath = filename;
120
121   fTrackletFile = TFile::Open(Form("%sTRD.Tracklets.root", fPath.Data()));
122
123   if (!fTrackletFile) {
124     AliError(Form("No tracklet file in %s", fPath.Data()));
125     return kFALSE;
126   }
127
128   fNEventsPerFile = fTrackletFile->GetNkeys() - fTrackletFile->GetNProcessIDs(); //???
129
130   fEvent = -1;
131   fFileNumber = 0;
132
133   return kTRUE;
134 }
135
136
137 void AliTRDonlineTrackletFilter::Exec(const Option_t * /* option */)
138 {
139   // execute this for each event
140
141   if (!LoadEvent())
142     return;
143
144   // ----- simulated tracklets -----
145   if (fTrackletTreeSim) {
146     AliTRDtrackletMCM *trkl = 0x0;
147
148     TBranch *br = fTrackletTreeSim->GetBranch("mcmtrklbranch");
149     br->SetAddress(&trkl);
150     
151     for (Int_t iTracklet = 0; iTracklet < br->GetEntries(); iTracklet++) {
152       br->GetEntry(iTracklet);
153       new ((*fTrackletsSim)[fTrackletsSim->GetEntriesFast()]) AliTRDtrackletMCM(*trkl);
154     }
155   }
156
157   // ----- raw tracklets -----
158   if (fTrackletTreeRaw) {
159     Int_t hc;
160     TClonesArray *trklArray = 0x0;
161     fTrackletTreeRaw->SetBranchAddress("hc", &hc);
162     fTrackletTreeRaw->SetBranchAddress("trkl", &trklArray);
163     for (Int_t iHCidx = 0; iHCidx < fTrackletTreeRaw->GetEntries(); iHCidx++) {
164       fTrackletTreeRaw->GetEntry(iHCidx);
165       if (trklArray) {
166         for (Int_t iTracklet = 0; iTracklet < trklArray->GetEntries(); iTracklet++) {
167           AliTRDtrackletWord *trklWord = (AliTRDtrackletWord*) ((*trklArray)[iTracklet]);
168           trklWord->SetDetector(hc/2);
169           new ((*fTrackletsRaw)[fTrackletsRaw->GetEntriesFast()]) AliTRDtrackletWord(*trklWord);
170         }
171       }
172     }
173   }
174
175   AliDebug(1, Form("%i tracklets", fTrackletsSim->GetEntriesFast()));
176   fTrackletTree->SetBranchAddress("tracklets_sim", &fTrackletsSim);
177   fTrackletTree->SetBranchAddress("tracklets_raw", &fTrackletsRaw);
178   fTrackletTree->Fill();
179   PostData(1, fTrackletTree);  
180 }
181
182 void AliTRDonlineTrackletFilter::LocalInit()
183 {
184
185 }
186
187 void AliTRDonlineTrackletFilter::Terminate(const Option_t * /* option */)
188 {
189
190 }
191
192 Bool_t AliTRDonlineTrackletFilter::LoadEvent()
193 {
194   // load tracklets for the current event
195
196   // ----- cleaning -----
197   fTrackletsSim->Delete();
198   fTrackletsRaw->Delete();
199
200   // ----- initialization -----
201   if (!fInputEvent) {
202     AliError("No event found!");
203     return kFALSE;
204   }
205   fESD = dynamic_cast<AliESDEvent*> (fInputEvent);
206
207   fEvent++;
208   Int_t inew = fNEventsPerFile > 0 ? fEvent / fNEventsPerFile : 0;
209   if ( inew != fFileNumber) {
210     fFileNumber++;
211
212     delete fTrackletFile;
213     fTrackletFile = TFile::Open(Form("%sTRD.Tracklets%d.root", fPath.Data(), fFileNumber));
214
215     if (!fTrackletFile) {
216       AliError("No tracklet file");
217       return kFALSE;
218     }
219   }
220
221   if (!fTrackletFile) {
222     AliError("no tracklet file");
223     return kFALSE;
224   }
225
226   // tracklets from simulation
227   char treename[30];
228   snprintf(treename, 30, "Event%d/tracklets", fEvent);
229
230   fTrackletTreeSim = (TTree*) fTrackletFile->Get(treename);
231
232   // tracklets from raw
233   char treenameRaw[30];
234   snprintf(treenameRaw, 30, "Event%d/tracklets-raw", fEvent);
235   fTrackletTreeRaw = (TTree*) fTrackletFile->Get(treenameRaw);
236
237   return kTRUE;
238 }
239