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