typo fixed
[u/mrichter/AliRoot.git] / T0 / AliT0CalibAnalysisTask.cxx
1 #include "TChain.h"
2 #include "TTree.h"
3 #include "TList.h"
4 #include "TH1F.h"
5 #include "TH2F.h"
6 #include "TCanvas.h"
7 #include "TMath.h"
8 #include "TObjString.h"
9
10 #include "AliAnalysisTask.h"
11 #include "AliPhysicsSelection.h"
12 #include "AliAnalysisManager.h"
13
14 #include "AliESDEvent.h"
15 #include "AliESDfriend.h"
16 #include "AliESDInputHandler.h"
17 #include "AliESDTZEROfriend.h"
18 #include "AliT0CalibAnalysisTask.h"
19 #include "AliESDVZERO.h"
20 #include "AliMultiplicity.h"
21 #include "AliTriggerAnalysis.h"
22 #include "AliESDpid.h"
23 #include "AliESDtrackCuts.h"
24 #include "AliCentrality.h"
25 #include "AliVEvent.h"
26
27 ClassImp(AliT0CalibAnalysisTask)
28
29 //________________________________________________________________________
30 AliT0CalibAnalysisTask::AliT0CalibAnalysisTask(const char *name) 
31   : AliAnalysisTaskSE(name), fESD(0), fOutputList(0), 
32   fT0OutTree(0x0), fEvent(-99999),   fOrbit(-99999), fBC(-99999),
33   fTrackletSPD(-99999), fClustersSPD(-99999), fNcont(-99999), fNcontTPC(-99999),
34   fVertex(-99999), fVertexcalc(-99999), fVertexSPD(-99999), fVertexTPC(-99999),
35   fMeanAC(-99999), fMeanA(-99999),fMeanC(-99999), fMeanACcalc(-99999), fMeanAcalc(-99999), fMeanCcalc(-99999),
36   fMultV0A(-99999), fMultV0C(-99999),fTimeV0A(-99999),fTimeV0C(-99999), fSumampA(-99999), fSumampC(-99999),
37   ftimestamp(0), fSep2(0),
38   fZDCcut(kFALSE), fT0Trigger(0), fpileup(kFALSE), fTrigger(0x0),
39   fT0_amplitude(0x0), fT0_time(0x0),
40   fcentralityV0M(0), fcentralityZDC(0), fcentralityTRK(0), fcentralityCLA(0),
41   fESDtracks(0),
42     fMultiplicity(-99999),
43   fTriggerinput(0x0), fZDCbg(kFALSE),
44   fTOFtracks(0), fT0tofTrack(0),
45   fESDpid(new AliESDpid())
46 {
47   // Constructor
48
49   // Define input and output slots here
50   // Input slot #0 works with a TChain
51   DefineInput(0, TChain::Class());
52   // Output slot #0 id reserved by the base class for AOD
53   // Output slot #1 writes into a TH1 container
54   DefineOutput(1, TList::Class());
55 }
56
57 //________________________________________________________________________
58 AliT0CalibAnalysisTask::~AliT0CalibAnalysisTask() 
59 {
60 // Destructor
61    if (fT0OutTree) delete fT0OutTree ;
62 }
63
64 //_____________________________________________________________________________
65 Bool_t AliT0CalibAnalysisTask::UserNotify()
66 {
67 //
68 // Calls the mother class Notify()
69 //
70
71 //  AliDebug(AliLog::kDebug,"<-");
72   // AliDebug(AliLog::kDebug,"->");
73
74   return AliAnalysisTaskSE::UserNotify();
75 }
76
77 //________________________________________________________________________
78 void AliT0CalibAnalysisTask::UserCreateOutputObjects()
79 {
80   // Create histograms
81   // Called once
82    fOutputList = new TList();
83   fOutputList->SetOwner(); // Will delete the histos on cleanup
84
85   fT0OutTree=new TTree("t0tree","None here");
86   fT0OutTree->Branch("fNevent", &fEvent);
87   fT0OutTree->Branch("fOrbit", &fOrbit);
88   fT0OutTree->Branch("fBC", &fBC);
89   fT0OutTree->Branch("fNcont", &fNcont, "fNcont/I");
90   fT0OutTree->Branch("fNcontTPC", &fNcontTPC);
91   fT0OutTree->Branch("fVertexTPC", &fVertexTPC);
92   fT0OutTree->Branch("vertexT0", &fVertex, "fVertex/F");
93   fT0OutTree->Branch("vertexSPD", &fVertexSPD, "vertexSPD/F");
94   fT0OutTree->Branch("meanAC", &fMeanAC,"meanAC/F");
95   fT0OutTree->Branch("meanA", &fMeanA,"meanA/F");
96   fT0OutTree->Branch("meanC", &fMeanC,"meanC/F");
97   fT0OutTree->Branch("trackletSPD", &fTrackletSPD,"trackletSPD/I");
98   fT0OutTree->Branch("clustersSPD", &fClustersSPD,"clustersSPD/I");
99   fT0OutTree->Branch("TOFtracks", &fTOFtracks);
100   fT0OutTree->Branch("ESDtracks", &fESDtracks);
101   fT0OutTree->Branch("multV0A", &fMultV0A,"multV0A/F");
102   fT0OutTree->Branch("multV0C", &fMultV0C,"multV0C/F");
103   fT0OutTree->Branch("timeV0A", &fTimeV0A,"timeV0A/F");
104   fT0OutTree->Branch("timeV0C", &fTimeV0C,"timeV0C/F");
105   fT0OutTree->Branch("sumampA", &fSumampA);
106   fT0OutTree->Branch("sumampC", &fSumampC);
107   fT0OutTree->Branch("pileup", &fpileup);
108   fT0OutTree->Branch("trigger", &fTrigger);
109   fT0OutTree->Branch("triggerinput", &fTriggerinput);
110   fT0OutTree->Branch("T0trigger", &fT0Trigger);
111   fT0OutTree->Branch("t0tofTrack",  &fT0tofTrack);
112   fT0OutTree->Branch("ZDCcut", &fZDCcut);
113   fT0OutTree->Branch("ftimestamp", &ftimestamp);
114   fT0OutTree->Branch("centralityV0M", &fcentralityV0M);
115   fT0OutTree->Branch("centralityZDC", &fcentralityZDC);
116   fT0OutTree->Branch("centralityTRK", &fcentralityTRK);
117   fT0OutTree->Branch("centralityCLA", &fcentralityCLA);
118   for ( Int_t i=0; i<24; i++) {
119     fT0OutTree->Branch(Form("amp%i", i+1), &famp[i]);
120     fT0OutTree->Branch(Form("time%i", i+1), &ftime[i]);
121   } 
122   for ( Int_t i=0; i<5; i++) {
123     fT0OutTree->Branch(Form("fOrA%i", i+1), &fOrA[i]);
124     fT0OutTree->Branch(Form("fOrC%i", i+1), &fOrC[i]);
125     fT0OutTree->Branch(Form("fTVDC%i", i+1), &fTVDC[i]);  
126     for ( Int_t ii=0; ii<24; ii++) 
127       fT0OutTree->Branch(Form("fRawTime%i_%i", ii+1, i+1), &fRawTime[ii][i]);
128   }
129   TString pilename[3] = {"T0pileup", "T0background", "T0satellite"};
130   for ( Int_t i=0; i<3; i++) 
131     fT0OutTree->Branch(pilename[i].Data(), &fT0pileup[i]);
132   fT0OutTree->Branch("meanACbest", &fMeanACcalc,"meanACcalc/F");
133   fT0OutTree->Branch("meanAbest", &fMeanAcalc,"meanAcalc/F");
134   fT0OutTree->Branch("meanCbest", &fMeanCcalc,"meanCcalc/F");
135   fT0OutTree->Branch("multEstimator", &fMultiplicity);
136   //ZDC background
137  fT0OutTree->Branch("zbg", &fZDCbg);
138  fESDpid = new AliESDpid();
139
140   fOutputList->Add(fT0OutTree);
141  
142    PostData(1, fOutputList);
143   
144 }
145
146 //________________________________________________________________________
147 void AliT0CalibAnalysisTask::UserExec(Option_t *) 
148 {
149   // Main loop
150   // Called for each event
151    fVertex= fVertexSPD =  fMeanAC = fMeanA =  fMeanC =  fTrackletSPD=-99999;
152   fMeanACcalc = fMeanAcalc =  fMeanCcalc = fNcont = fNcontTPC = -99999;
153   fMultV0A=fMultV0C=fTimeV0A=fTimeV0C=fMultiplicity = -99999;
154   
155   for (Int_t i=0; i<24; i++) {
156     famp[i] = ftime[i] = -9999;
157     for ( Int_t i0=0; i0<5; i0++) {
158       fRawTime[i][i0] = -9999;
159       if(i0==0) { 
160         fTVDC[i0]=-9999;
161         fOrC[i0]=-9999;
162         fOrA[i0]=-9999;
163       }
164     }
165   }
166   Float_t orA , orC, tvdc;
167   orA = orC = tvdc = -9999;
168   fBC = -9999;
169   // Post output data.
170   fESD = dynamic_cast<AliESDEvent*>(InputEvent());
171   if (!fESD) {
172     printf("ERROR: fESD not available\n");
173     return;
174   }
175
176   if(fESD)
177     {
178       //    if(fIsSelected) 
179       {
180         fZDCbg = kFALSE;
181         /*
182         AliTriggerAnalysis *fTriggerAnalysis;
183         Bool_t  fZDCbgA = fTriggerAnalysis->IsOfflineTriggerFired(fESD, AliTriggerAnalysis::kZNABG);
184         Bool_t  fZDCbgC =  fTriggerAnalysis->IsOfflineTriggerFired(fESD, AliTriggerAnalysis::kZNCBG);
185         fZDCbg = (fZDCbgA);
186         cout<<" ZDC back "<<fZDCbgA<<endl;
187         */
188         AliESDVZERO* esdV0 = fESD->GetVZEROData();
189         const AliMultiplicity *mult = fESD->GetMultiplicity();
190         // printf(" VZERO MULT \n");
191         AliESDTZERO* tz= (AliESDTZERO*) fESD->GetESDTZERO();
192
193         const Double32_t *amp, *time, *mean ;
194         const Double32_t *meanbest;
195         fVertex= fVertexSPD =  fMeanAC = fMeanA =  fMeanC =  fTrackletSPD=-99999;
196         fMeanACcalc = fMeanAcalc =  fMeanCcalc = fNcont = fNcontTPC -99999;
197         fMultV0A=fMultV0C=fTimeV0A=fTimeV0C=fMultiplicity = -99999;
198         
199         fSumampA=0;
200         fSumampC=0;
201         fNcont=0;
202         TString triggers = fESD->GetFiredTriggerClasses();
203         fTrigger.SetString(triggers.Data());
204         if ( !triggers.Contains("Z") )    cout<<triggers<<endl;
205         TString inputtriggers =fESD-> GetHeader()->GetFiredTriggerInputs();
206         // cout<<inputtriggers<<endl;
207         fTriggerinput.SetString(inputtriggers.Data());
208         
209         
210         fEvent=fESD->GetEventNumberInFile();
211         fT0Trigger=fESD->GetT0Trig();
212         
213         fESDtracks=fESD->GetNumberOfTracks();
214         AliTriggerAnalysis *trigAna = new AliTriggerAnalysis;
215         ftimestamp=fESD->GetTimeStamp(); // - 1301217882; 
216         //        printf (" timestamp %d \n", ftimestamp);
217         
218         fMultiplicity =  AliESDtrackCuts::GetReferenceMultiplicity(fESD);
219         //        cout<<" fMultiplicity "<<fMultiplicity<<endl;
220         fOrbit=fESD->GetOrbitNumber();
221         fBC=fESD->GetBunchCrossNumber();
222         fNcont = fESD->GetPrimaryVertexSPD()->GetNContributors();
223         fNcontTPC = fESD->GetPrimaryVertexTPC()->GetNContributors();
224         if(fNcontTPC>=2 )
225           fVertexTPC = fESD->GetPrimaryVertexTPC() ->GetZ();
226         if(fNcont>=0 ) {
227           fVertexSPD = fESD->GetPrimaryVertex()->GetZ();
228         }
229         fTrackletSPD = mult->GetNumberOfTracklets();
230         fClustersSPD = mult->GetNumberOfITSClusters(0); 
231         fMultV0A =  esdV0->GetMTotV0A();
232         fMultV0C =  esdV0->GetMTotV0C();
233         //TOF hit  
234         Int_t ntracksMatchedToTOF = 0; 
235         Int_t ntracks = fESD->GetNumberOfTracks();
236         for(Int_t itrk=0;itrk<ntracks;itrk++){
237           AliESDtrack* track = fESD->GetTrack(itrk);
238           if (!track) {
239             Printf("ERROR: Could not receive track %d", itrk);
240             continue;
241           }
242           //no track selection just TOF hit
243           if (track->IsOn(AliESDtrack::kTOFout)) ntracksMatchedToTOF++;
244         }
245         
246         fTOFtracks = ntracksMatchedToTOF;
247         if(fESDpid){ //get T0_TOF 
248           fESDpid->SetTOFResponse(fESD,AliESDpid::kTOF_T0);
249           fT0tofTrack =(Float_t) (fESDpid->GetTOFResponse().GetStartTime(10.0)); //Get start time "from all tracks 
250           //   printf("@@TOF T0 %f\n",fT0tofTrack);
251         }       
252         
253         
254         fpileup = fESD->IsPileupFromSPD();
255         fTimeV0C = esdV0->GetV0CTime();
256         fTimeV0A = esdV0->GetV0ATime();
257         //        printf(" Vo info \n");      
258         
259         
260         mean = fESD->GetT0TOF();
261         fVertex=fESD->GetT0zVertex();
262         fMeanA = mean[1];
263         fMeanC = mean[2];
264         if (TMath::Abs(fMeanA)<9999 && TMath::Abs(fMeanC)<9999) 
265             fMeanAC = mean[0] ;
266         fSumampC = tz->GetMultC();
267         fSumampA = tz->GetMultA();
268         //    printf("!!!!!!!!fSumampC %f fSumampA %f \n",fSumampC, fSumampA);
269         
270         amp=fESD->GetT0amplitude();
271         time=fESD->GetT0time();
272         for (Int_t i=0; i<24; i++){ 
273           if( time[i]>100 && amp[i]>0.1){
274             famp[i] = amp[i];
275             ftime[i] = time[i];
276           }
277         }
278         
279         //new raw OrA OrC TVDC
280         meanbest = tz->GetT0TOFbest();
281         fMeanAcalc = meanbest[1];
282         fMeanCcalc = meanbest[2];
283         if (TMath::Abs(fMeanAcalc)<9999 && TMath::Abs(fMeanCcalc)<9999) 
284           fMeanACcalc = meanbest[0] ;
285         for (Int_t ii=0; ii<5; ii++){ 
286           orA   = tz->GetOrA(ii);
287           orC   = tz->GetOrC(ii);
288           tvdc  = tz->GetTVDC(ii);
289           //    printf("@@@@@  new signasl %d  %f %f %f \n",ii, orA, orC, tvdc);
290           if(ii==0) {
291             fOrA[ii] = orA;
292             fOrC[ii] = orC;
293             fTVDC[ii] = tvdc;
294             //  cout<<tvdc<<" "<<fT0Trigger<<endl;
295             // if (tvdc>-5 && tvdc<5 && tvdc!=0) 
296             //  cout<<"@@@@@@@@@ "<< tvdc<<" "<<fT0Trigger<<endl;
297           } else {
298             if ( ii>0 && fOrA[ii-1] !=orA)      fOrA[ii] = orA;
299             else fOrA[ii]=-9999;
300             if ( ii>0 && fOrC[ii-1] !=orC)      fOrC[ii] = orC;
301             else fOrC[ii]=-9999;
302           if ( ii>0 && fTVDC[ii-1] != tvdc)     fTVDC[ii] = tvdc;
303           else fTVDC[ii]=-9999;
304           }
305           for (Int_t iii=0; iii<24; iii++)
306             fRawTime[iii][ii] =  tz->GetTimeFull(iii,ii);
307         }
308         fT0pileup[0] = tz->GetPileupFlag();
309         fT0pileup[1] = tz->GetBackgroundFlag();
310         fT0pileup[2] = tz->GetSatellite();
311         
312         AliCentrality *centrality = fESD->GetCentrality();
313         
314         //The centrality function you can use
315         
316         fcentralityV0M = centrality->GetCentralityPercentile("V0M"); // returns the centrality 
317         fcentralityTRK = centrality->GetCentralityPercentile("TRK"); // returns the centrality 
318         fcentralityZDC = centrality->GetCentralityPercentile("ZEMvsZDC"); // returns the 
319
320       
321       } //selected
322     } //ESD
323
324   fT0OutTree->Fill();
325     
326   PostData(1, fOutputList);
327   
328 }      
329
330 //________________________________________________________________________
331 void AliT0CalibAnalysisTask::Terminate(Option_t *) 
332 {
333   // Draw result to the screen
334   // Called once at the end of the query
335
336   /* fOutputList = dynamic_cast<TList*> (GetOutputData(1));
337   if (!fOutputList) {
338     printf("ERROR: Output list not available\n");
339     return;
340   }
341     */  
342
343 }