]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFAnalysisTaskCalibTree.cxx
Merge branch 'master' into TPCdev
[u/mrichter/AliRoot.git] / TOF / AliTOFAnalysisTaskCalibTree.cxx
1 #include "AliTOFAnalysisTaskCalibTree.h"
2 #include "AliAnalysisManager.h"
3 #include "AliAODHandler.h"
4 #include "TTree.h"
5 #include "AliCDBManager.h"
6 #include "AliGRPManager.h"
7 #include "AliGRPObject.h"
8 #include "AliPhysicsSelection.h"
9 #include "AliESDtrack.h"
10 #include "AliESDEvent.h"
11 #include "AliESDpid.h"
12 #include "AliESDtrackCuts.h"
13 #include "AliTOFT0v1.h"
14 #include "AliTOFT0maker.h"
15 #include "AliTOFcalib.h"
16
17 ClassImp(AliTOFAnalysisTaskCalibTree)
18   
19 //_______________________________________________________
20   
21 AliTOFAnalysisTaskCalibTree::AliTOFAnalysisTaskCalibTree(const Char_t* name) :
22 AliAnalysisTaskSE(name),
23   fInitFlag(kFALSE),                   
24   fEventSelectionFlag(kFALSE),        
25   fVertexSelectionFlag(kFALSE),       
26   fVertexCut(1000.),              
27   fDiscardPileupEventFlag(kFALSE),   
28   fCalibrateTOFsignal(kFALSE),       
29   fComputeT0TOF(kFALSE),             
30   fUseT0TOF(kFALSE),                 
31   fUseLHCClockPhase(kFALSE),         
32   fPrimaryDCASelectionFlag(kFALSE),  
33   fRunNumber(0),                   
34   fESDEvent(0),             
35   fEventCuts(new AliPhysicsSelection()),    
36   fTrackCuts(new AliESDtrackCuts()),  
37   fESDpid(new AliESDpid()),
38   fStartTime(0),
39   fEndTime(0),
40   fElapsedTime(0),
41   fIsCollisionCandidate(kFALSE),
42   fHasVertex(kFALSE),
43   fVertex(NULL),
44   fGRPManager(new AliGRPManager()),         
45   fGRPObject(NULL),     
46   fSpecificStorageParOffline(), 
47   fSpecificStorageRunParams(),  
48   fTimeResolution(80.),
49   fTOFcalib(new AliTOFcalib()),             
50   fTOFT0maker(new AliTOFT0maker(fESDpid, fTOFcalib)),         
51   fTOFT0v1(new AliTOFT0v1(fESDpid)),
52   ftimestamp(0),
53   fVertexZ(99999),
54   ftimezero(9999999),
55   fnhits(-1),
56   fOutputTree(0x0)             
57
58 {
59   /* 
60    * default constructor 
61    */
62
63   for (Int_t i = 0; i < MAXHITS; i++){
64       fmomentum[i] = 999999;
65       flength[i] = 999999;
66       findex[i] = -1;
67       ftime[i] = 999999;
68       ftot[i] = 999999;
69       ftexp[i] = 999999;
70   }
71
72   DefineOutput(1, TTree::Class());  
73
74 }
75
76 //_______________________________________________________
77
78 AliTOFAnalysisTaskCalibTree::~AliTOFAnalysisTaskCalibTree()
79 {
80   /*
81    * default destructor
82    */
83   delete fEventCuts;
84   delete fTrackCuts;
85   delete fESDpid;
86   delete fTOFcalib;
87   delete fTOFT0maker;
88   delete fTOFT0v1;
89   if (fOutputTree) {
90     delete fOutputTree;
91     fOutputTree = 0x0;
92   }
93
94 }
95
96 //_______________________________________________________
97
98 void
99 AliTOFAnalysisTaskCalibTree::UserCreateOutputObjects()
100 {
101   /*
102    * user create output objects
103    */
104
105   fOutputTree = new TTree("aodTree", "Tree with TOF calib output"); 
106
107   /* setup output tree */
108   fOutputTree->Branch("run", &fRunNumber, "run/I");
109   fOutputTree->Branch("timestamp", &ftimestamp, "timestamp/i");
110   fOutputTree->Branch("timezero", &ftimezero, "timezero/F");
111   fOutputTree->Branch("vertex", &fVertexZ, "vertex/F");
112   fOutputTree->Branch("nhits", &fnhits, "nhits/I");
113   fOutputTree->Branch("momentum", &fmomentum, "momentum[nhits]/F");
114   fOutputTree->Branch("length", &flength, "length[nhits]/F");
115   fOutputTree->Branch("index", &findex, "index[nhits]/I");
116   fOutputTree->Branch("time", &ftime, "time[nhits]/F");
117   fOutputTree->Branch("tot", &ftot, "tot[nhits]/F");
118   fOutputTree->Branch("texp", &ftexp, "texp[nhits]/F");
119
120   PostData(1, fOutputTree);
121
122 }
123
124 //_______________________________________________________
125
126 void AliTOFAnalysisTaskCalibTree::UserExec(Option_t *) {
127   //
128   // user exec
129   //
130
131   // unset fill AOD
132   ((AliAODHandler*)(AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler()))->SetFillAOD(kFALSE);
133
134   //init run
135   if (!InitRun()) return;
136
137   //init event 
138   if (!InitEvent()) return;
139
140   /*** ACCEPTED EVENT ***/
141   
142   // set vertex
143   fVertexZ = fVertex->GetZ();
144
145   // compute T0-TOF using all availeble tracks 
146   fTOFT0v1->DefineT0("all", 0.0, 0.5);
147   Float_t timeZeroTOF = -1000. * fTOFT0v1->GetResult(0);
148   Float_t timeZeroTOF_sigma = 1000. * fTOFT0v1->GetResult(1);
149   //Int_t timeZeroTOF_tracks = fTOFT0v1->GetResult(3);  // not used for the time being
150
151   // check T0-TOF sigma 
152   if (timeZeroTOF_sigma >= 250.) ftimezero = 999999.; 
153   else ftimezero = timeZeroTOF;
154
155   // reset
156   fnhits = 0;
157   
158   // loop over ESD tracks
159   Int_t nTracks = fESDEvent->GetNumberOfTracks();
160   AliESDtrack *track;
161   Int_t index;
162   Double_t momentum, length, time, tot, timei[AliPID::kSPECIES];
163   for (Int_t itrk = 0; itrk < nTracks; itrk++) {
164     // get track
165     track = fESDEvent->GetTrack(itrk);
166     if (!track) continue;
167     // check accept track
168     if (!fTrackCuts->AcceptTrack(track)) continue;
169     // check primary DCA
170     if (fPrimaryDCASelectionFlag && !HasPrimaryDCA(track)) continue;
171     // check TOF measurement
172     if (!HasTOFMeasurement(track)) continue;
173
174     /*** ACCEPTED TRACK WITH TOF MEASUREMENT ***/
175
176     // get track info 
177     momentum = track->P();
178     length = track->GetIntegratedLength();
179     index = track->GetTOFCalChannel();
180     time = track->GetTOFsignal();
181     tot = track->GetTOFsignalToT();
182     track->GetIntegratedTimes(timei);
183
184     // add hit to array (if there is room)
185     if (fnhits > MAXHITS) continue;
186     fmomentum[fnhits] = momentum;
187     flength[fnhits] = length;
188     findex[fnhits] = index;
189     ftime[fnhits] = time;
190     ftot[fnhits] = tot;
191     ftexp[fnhits] = timei[AliPID::kPion];
192     fnhits++;
193
194   } /* end of loop over ESD tracks */
195
196   // check number of hits and set fill output tree
197   if (fnhits > 0)
198     fOutputTree->Fill();
199
200   PostData(1, fOutputTree);
201
202 }
203
204 //_______________________________________________________
205
206 Bool_t AliTOFAnalysisTaskCalibTree::InitRun() {
207
208   //¯
209   // init run
210   //
211   
212   // get ESD event 
213   fESDEvent = dynamic_cast<AliESDEvent *>(InputEvent());
214   if (!fESDEvent) {
215     AliError("cannot get ESD event");
216     return kFALSE;
217   }
218
219   // get run number 
220   Int_t runNb = fESDEvent->GetRunNumber();
221
222   // check run already initialized 
223   if (fInitFlag && fRunNumber == runNb) return kTRUE;
224   fInitFlag = kFALSE;
225
226   // init cdb 
227   AliCDBManager *cdb = AliCDBManager::Instance();
228   cdb->SetDefaultStorage("raw://");
229   if (!fSpecificStorageParOffline.IsNull())
230     cdb->SetSpecificStorage("TOF/Calib/ParOffline", fSpecificStorageParOffline.Data());
231   if (!fSpecificStorageRunParams.IsNull())
232     cdb->SetSpecificStorage("TOF/Calib/RunParams", fSpecificStorageRunParams.Data());
233   cdb->SetRun(runNb);
234   
235   // init TOF calib
236   if (!fTOFcalib->Init()) {
237     AliError("cannot init TOF calib");
238     return kFALSE;
239   }
240
241   // get GRP data
242   if (!fGRPManager->ReadGRPEntry()) {
243     AliError("error while reading \"GRPEntry\" from OCDB");
244     return kFALSE;
245   }
246   fGRPObject = fGRPManager->GetGRPData();
247   if (!fGRPObject) {
248     AliError("cannot get \"GRPData\" from GRP manager");
249     return kFALSE;
250   }
251   fStartTime = fGRPObject->GetTimeStart();
252   fEndTime = fGRPObject->GetTimeEnd();
253   AliInfo(Form("got \"GRPData\": startTime=%d, endTime=%d", fStartTime, fEndTime));
254   
255   AliInfo(Form("initialized for run %d", runNb));
256   fInitFlag = kTRUE;
257   fRunNumber = runNb;
258   return kTRUE;
259 }
260
261 //_______________________________________________________
262
263 Bool_t AliTOFAnalysisTaskCalibTree::InitEvent() {
264
265   //
266   // init event
267   //
268
269   // get ESD event 
270   fESDEvent = dynamic_cast<AliESDEvent *>(InputEvent());
271   if (!fESDEvent) return kFALSE;
272
273   // set event time and elapsed time
274   ftimestamp = fESDEvent->GetTimeStamp();
275   fElapsedTime = fESDEvent->GetTimeStamp() - fStartTime;
276
277   // event selection
278   if (fEventSelectionFlag){
279     fIsCollisionCandidate = fEventCuts->IsCollisionCandidate(fESDEvent);
280     if (!fIsCollisionCandidate) return kFALSE;
281   }
282
283   // vertex selection
284   fVertex = fESDEvent->GetPrimaryVertexTracks();
285   if (fVertex->GetNContributors() < 1) {
286     fVertex = fESDEvent->GetPrimaryVertexSPD();
287     if (fVertex->GetNContributors() < 1) fHasVertex = kFALSE;
288     else fHasVertex = kTRUE;
289   }
290   else fHasVertex = kTRUE;
291   if (fVertexSelectionFlag && (!fHasVertex || TMath::Abs(fVertex->GetZ()) > fVertexCut)) return kFALSE;
292
293   // discard pile-up event is requested
294   if (fDiscardPileupEventFlag) {
295     if (fESDEvent->IsPileupFromSPD()) {
296       printf("PILE-UP event, will be discarded\n");
297       return kFALSE;
298     }
299   }
300
301   // init TOF response 
302   fESDpid->GetTOFResponse().SetTimeResolution(fTimeResolution);
303
304   // init TOF-T0 maker 
305   fTOFT0maker->SetTimeResolution(fTimeResolution);
306   
307   // calibrate ESD if requested
308   if (fCalibrateTOFsignal)
309     fTOFcalib->CalibrateESD(fESDEvent);
310
311   // compute T0-TOF and apply it if requested
312   if (fComputeT0TOF) 
313     fTOFT0maker->ComputeT0TOF(fESDEvent);
314   if (fUseT0TOF) {
315     fTOFT0maker->ApplyT0TOF(fESDEvent);
316     //fESDpid->MakePID(fESDEvent, kFALSE, 0.);
317   }
318
319   // init T0-TOF
320   fTOFT0v1->Init(fESDEvent);
321
322   return kTRUE;
323
324 }
325
326 //_______________________________________________________
327
328 Bool_t AliTOFAnalysisTaskCalibTree::HasTOFMeasurement(AliESDtrack *track) {
329
330   //
331   // has TOF measurement
332   //
333
334   //check TOF status flags 
335   if (!(track->GetStatus() & AliESDtrack::kTOFout) ||
336       !(track->GetStatus() & AliESDtrack::kTIME)) return kFALSE;
337
338   // check integrated length
339   if (track->GetIntegratedLength() < 350.) return kFALSE;
340
341   // TOF measurement ok
342   return kTRUE;
343 }
344
345 //_______________________________________________________
346
347 Bool_t AliTOFAnalysisTaskCalibTree::HasPrimaryDCA(AliESDtrack *track) {
348
349   //
350   // has primary DCA
351   //
352
353   // cut on transverse impact parameter
354   Float_t d0z0[2],covd0z0[3];
355   track->GetImpactParameters(d0z0, covd0z0);
356   Float_t sigma= 0.0050 + 0.0060 / TMath::Power(track->Pt(), 0.9);
357   Float_t d0max = 7. * sigma;
358   //
359   Float_t sigmaZ = 0.0146 + 0.0070 / TMath::Power(track->Pt(), 1.114758);
360   if (track->Pt() > 1.) sigmaZ = 0.0216;
361   Float_t d0maxZ = 5. * sigmaZ;
362   //
363   if(TMath::Abs(d0z0[0]) > d0max || TMath::Abs(d0z0[1]) > d0maxZ) 
364     return kFALSE;
365   
366   /* primary DCA ok */
367   return kTRUE;
368 }