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