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