Updates in PbPb cuts (Andrea Rossi)
[u/mrichter/AliRoot.git] / PWG3 / vertexingHF / AliNormalizationCounter.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2008, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //*************************************************************************
19 // Class AliNormalizationCounter
20 // Class to store the informations relevant for the normalization in the 
21 // barrel for each run
22 // Authors: G. Ortona, ortona@to.infn.it
23 // D. Caffarri, davide.caffarri@pd.to.infn.it
24 // with many thanks to P. Pillot
25 /////////////////////////////////////////////////////////////
26
27 #include "AliNormalizationCounter.h"
28 #include <AliESDEvent.h>
29 #include <AliESDtrack.h>
30 #include <AliAODEvent.h>
31 #include <AliVParticle.h>
32 #include <AliTriggerAnalysis.h>
33 #include <TH1F.h>
34 #include <TH2F.h>
35 #include <TList.h>
36 #include <TString.h>
37 #include <TCanvas.h>
38 #include <AliPhysicsSelection.h>
39 #include <AliMultiplicity.h>
40
41 ClassImp(AliNormalizationCounter)
42
43 //____________________________________________
44 AliNormalizationCounter::AliNormalizationCounter(): 
45 TNamed(),
46 fCounters(),
47 fESD(kFALSE),
48 fHistTrackFilterEvMult(0),
49 fHistTrackAnaEvMult(0),
50 fHistTrackFilterSpdMult(0),
51 fHistTrackAnaSpdMult(0)
52 {
53   // empty constructor
54 }
55
56 //__________________________________________________                            
57 AliNormalizationCounter::AliNormalizationCounter(const char *name): 
58 TNamed(name,name),
59 fCounters(name),
60 fESD(kFALSE),
61 fHistTrackFilterEvMult(0),
62 fHistTrackAnaEvMult(0),
63 fHistTrackFilterSpdMult(0),
64 fHistTrackAnaSpdMult(0)
65 {
66   //default constructor
67   fCounters.AddRubric("Event","triggered/V0AND/PileUp/PbPbC0SMH-B-NOPF-ALLNOTRD/Candles0.3/PrimaryV/countForNorm/noPrimaryV/zvtxGT10/!V0A&Candle03/!V0A&PrimaryV/Candid(Filter)/Candid(Analysis)/NCandid(Filter)/NCandid(Analysis)");
68   fCounters.AddRubric("Run", 1000000);
69   fCounters.Init();
70   fHistTrackFilterEvMult=new TH2F("FiltCandidvsTracksinEv","FiltCandidvsTracksinEv",10000,-0.5,9999.5,200,-0.5,199.5);
71   fHistTrackFilterEvMult->GetYaxis()->SetTitle("NCandidates");
72   fHistTrackFilterEvMult->GetXaxis()->SetTitle("NTracksinEvent");
73   fHistTrackAnaEvMult=new TH2F("AnaCandidvsTracksinEv","AnaCandidvsTracksinEv",10000,-0.5,9999.5,100,-0.5,99.5);
74   fHistTrackAnaEvMult->GetYaxis()->SetTitle("NCandidates");
75   fHistTrackAnaEvMult->GetXaxis()->SetTitle("NTracksinEvent");
76   fHistTrackFilterSpdMult=new TH2F("FilterCandidvsSpdMult","FilterCandidvsSpdMult",5000,-0.5,4999.5,200,-0.5,199.5);
77   fHistTrackFilterSpdMult->GetYaxis()->SetTitle("NCandidates");
78   fHistTrackFilterSpdMult->GetXaxis()->SetTitle("NSPDTracklets");
79   fHistTrackAnaSpdMult=new TH2F("AnaCandidvsSpdMult","AnaCandidvsSpdMult",5000,-0.5,4999.5,100,-0.5,99.5);
80   fHistTrackAnaSpdMult->GetYaxis()->SetTitle("NCandidates");
81   fHistTrackAnaSpdMult->GetXaxis()->SetTitle("NSPDTracklets");
82 }
83 //______________________________________________
84 AliNormalizationCounter::~AliNormalizationCounter()
85 {
86   //destructor
87   if(fHistTrackFilterEvMult){
88     delete fHistTrackFilterEvMult;
89     fHistTrackFilterEvMult =0;
90   }
91   if(fHistTrackAnaEvMult){
92     delete fHistTrackAnaEvMult;
93     fHistTrackAnaEvMult=0;
94   }
95   if(fHistTrackFilterSpdMult){
96     delete fHistTrackFilterSpdMult;
97     fHistTrackFilterSpdMult=0;
98   }
99   if(fHistTrackAnaSpdMult){
100     delete fHistTrackAnaSpdMult;
101     fHistTrackAnaSpdMult=0;
102   }
103 }
104
105 //______________________________________________
106 Long64_t AliNormalizationCounter::Merge(TCollection* list){
107   if (!list) return 0;
108   if (list->IsEmpty()) return 0;//(Long64_t)fCounters.Merge(list);
109
110   TIter next(list);
111   const TObject* obj = 0x0;
112   while ((obj = next())) {
113     
114     // check that "obj" is an object of the class AliNormalizationCounter
115     const AliNormalizationCounter* counter = dynamic_cast<const AliNormalizationCounter*>(obj);
116     if (!counter) {
117       AliError(Form("object named %s is not AliNormalizationCounter! Skipping it.", counter->GetName()));
118       continue;
119     }
120
121     Add(counter);
122
123   }
124   
125   return (Long64_t)1;//(Long64_t)fCounters->GetEntries();
126 }
127 //_______________________________________
128 void AliNormalizationCounter::Add(const AliNormalizationCounter *norm){
129   fCounters.Add(&(norm->fCounters));
130   fHistTrackFilterEvMult->Add(norm->fHistTrackFilterEvMult);
131   fHistTrackAnaEvMult->Add(norm->fHistTrackAnaEvMult);
132   fHistTrackFilterSpdMult->Add(norm->fHistTrackFilterSpdMult);
133   fHistTrackAnaSpdMult->Add(norm->fHistTrackAnaSpdMult);
134 }
135 //_______________________________________
136 /*
137 Stores the variables used for normalization as function of run number
138 returns kTRUE if the event is to be counted for normalization
139 (pass event selection cuts OR has no primary vertex)
140  */
141 void AliNormalizationCounter::StoreEvent(AliVEvent *event,AliRDHFCuts *rdCut,Bool_t mc){
142   //
143
144   Bool_t v0A=kFALSE; 
145   Bool_t v0B=kFALSE;
146   Bool_t flag03=kFALSE;
147   Bool_t flagPV=kFALSE;
148
149   //Run Number
150   Int_t runNumber = event->GetRunNumber();
151  
152   //Find CINT1B
153   AliESDEvent *eventESD = (AliESDEvent*)event;
154   if(!eventESD){AliError("ESD event not available");return;}
155   if(mc&&event->GetEventType() != 0)return;
156   //event must be either physics or MC
157   if(!(event->GetEventType() == 7||event->GetEventType() == 0))return;
158   
159   fCounters.Count(Form("Event:triggered/Run:%d",runNumber));
160    
161   //Find V0AND
162   AliTriggerAnalysis trAn; /// Trigger Analysis
163   v0B = trAn.IsOfflineTriggerFired(eventESD , AliTriggerAnalysis::kV0C);
164   v0A = trAn.IsOfflineTriggerFired(eventESD , AliTriggerAnalysis::kV0A);
165   if(v0A&&v0B){fCounters.Count(Form("Event:V0AND/Run:%d",runNumber));}
166   
167   //FindPrimary vertex  
168   // AliVVertex *vtrc =  (AliVVertex*)event->GetPrimaryVertex();
169   // if(vtrc && vtrc->GetNContributors()>0){
170   //   fCounters.Count(Form("Event:PrimaryV/Run:%d",runNumber));
171   //   flagPV=kTRUE;
172   // }
173
174   //trigger
175   AliAODEvent *eventAOD = (AliAODEvent*)event;
176   TString trigclass=eventAOD->GetFiredTriggerClasses();
177   if(trigclass.Contains("C0SMH-B-NOPF-ALLNOTRD")||trigclass.Contains("C0SMH-B-NOPF-ALL"))fCounters.Count(Form("Event:PbPbC0SMH-B-NOPF-ALLNOTRD/Run:%d",runNumber));
178
179   //FindPrimary vertex  
180   if(rdCut->IsEventSelected(event)){
181     fCounters.Count(Form("Event:PrimaryV/Run:%d",runNumber));
182     flagPV=kTRUE;
183   }else{
184     if(rdCut->GetWhyRejection()==0)fCounters.Count(Form("Event:noPrimaryV/Run:%d",runNumber));
185     //find good vtx outside range
186     if(rdCut->GetWhyRejection()==6){
187       fCounters.Count(Form("Event:zvtxGT10/Run:%d",runNumber));
188       fCounters.Count(Form("Event:PrimaryV/Run:%d",runNumber));
189       flagPV=kTRUE;
190     }
191     if(rdCut->GetWhyRejection()==1)fCounters.Count(Form("Event:PileUp/Run:%d",runNumber));
192   }
193   //to be counted for normalization
194   if(rdCut->CountEventForNormalization())fCounters.Count(Form("Event:countForNorm/Run:%d",runNumber));
195   
196
197   //Find Candle
198   Int_t trkEntries = (Int_t)event->GetNumberOfTracks();
199   for(Int_t i=0;i<trkEntries&&!flag03;i++){
200     AliAODTrack *track=(AliAODTrack*)event->GetTrack(i);
201     if((track->Pt()>0.3)&&(!flag03)){
202       fCounters.Count(Form("Event:Candles0.3/Run:%d",runNumber));
203       flag03=kTRUE;
204       break;
205     }
206   }
207   
208   if(!(v0A&&v0B)&&(flag03))fCounters.Count(Form("Event:!V0A&Candle03/Run:%d",runNumber));
209   if(!(v0A&&v0B)&&flagPV)fCounters.Count(Form("Event:!V0A&PrimaryV/Run:%d",runNumber));
210   
211   return;
212 }
213 //_____________________________________________________________________
214 void AliNormalizationCounter::StoreCandidates(AliVEvent *event,Int_t nCand,Bool_t flagFilter){
215   
216   Int_t ntracks=event->GetNumberOfTracks();
217   if(flagFilter)fHistTrackFilterEvMult->Fill(ntracks,nCand);
218   else fHistTrackAnaEvMult->Fill(ntracks,nCand);
219   Int_t nSPD=0;
220   if(fESD){
221     AliESDEvent *ESDevent=(AliESDEvent*)event;
222     const AliMultiplicity *alimult = ESDevent->GetMultiplicity();
223     nSPD = alimult->GetNumberOfTracklets();
224
225   }else{
226     AliAODEvent *aodEvent =(AliAODEvent*)event;
227     AliAODTracklets *trklets=aodEvent->GetTracklets();
228     nSPD = trklets->GetNumberOfTracklets();
229   }
230   if(flagFilter)fHistTrackFilterSpdMult->Fill(nSPD,nCand);
231   else fHistTrackAnaSpdMult->Fill(nSPD,nCand);
232   
233   Int_t runNumber = event->GetRunNumber();
234   if(nCand==0)return;
235   if(flagFilter){
236     fCounters.Count(Form("Event:Candid(Filter)/Run:%d",runNumber));
237     for(Int_t i=0;i<nCand;i++)fCounters.Count(Form("Event:NCandid(Filter)/Run:%d",runNumber));
238   }else{
239     fCounters.Count(Form("Event:Candid(Analysis)/Run:%d",runNumber));
240     for(Int_t i=0;i<nCand;i++)fCounters.Count(Form("Event:NCandid(Analysis)/Run:%d",runNumber));
241   }
242   return;
243 }
244 //_______________________________________________________________________
245 TH1D* AliNormalizationCounter::DrawAgainstRuns(TString candle,Bool_t drawHist){
246   //
247   fCounters.SortRubric("Run");
248   TString selection;
249   selection.Form("event:%s",candle.Data());
250   TH1D* histoneD = fCounters.Get("run",selection.Data());
251
252   histoneD->Sumw2();
253   if(drawHist)histoneD->DrawClone();
254   return histoneD;
255 }
256 //___________________________________________________________________________
257 TH1D* AliNormalizationCounter::DrawRatio(TString candle1,TString candle2){
258   //
259   fCounters.SortRubric("Run");
260   TString name;
261
262   name.Form("%s/%s",candle1.Data(),candle2.Data());
263   TH1D* num=DrawAgainstRuns(candle1.Data(),kFALSE);
264   TH1D* den=DrawAgainstRuns(candle2.Data(),kFALSE);
265
266   den->SetTitle(candle2.Data());
267   den->SetName(candle2.Data());
268   num->Divide(num,den,1,1,"B");
269   num->SetTitle(name.Data());
270   num->SetName(name.Data());
271   num->DrawClone();
272   return num;
273 }
274 //___________________________________________________________________________
275 void AliNormalizationCounter::PrintRubrics(){
276   fCounters.PrintKeyWords();
277 }
278 //___________________________________________________________________________
279 Double_t AliNormalizationCounter::GetSum(TString candle){
280   TString selection="event:";
281   selection.Append(candle);
282   return fCounters.GetSum(selection.Data());
283 }
284 //___________________________________________________________________________
285 TH2F* AliNormalizationCounter::GetHist(Bool_t filtercuts,Bool_t spdtracklets,Bool_t drawHist){
286   if(filtercuts){
287     if(spdtracklets){
288       if(drawHist)fHistTrackFilterSpdMult->DrawCopy("LEGO2Z 0");
289       return fHistTrackFilterSpdMult;
290     }else{
291       if(drawHist)fHistTrackFilterEvMult->DrawCopy("LEGO2Z 0");
292       return fHistTrackFilterEvMult;
293     }
294   }else{
295     if(spdtracklets){
296       if(drawHist)fHistTrackAnaSpdMult->DrawCopy("LEGO2Z 0");
297       return fHistTrackAnaSpdMult;
298     }else{
299       if(drawHist)fHistTrackAnaEvMult->DrawCopy("LEGO2Z 0");
300       return fHistTrackAnaEvMult;
301     }
302   }
303 }
304 //___________________________________________________________________________
305 Double_t AliNormalizationCounter::GetNEventsForNorm(){
306   Double_t noVtxzGT10=GetSum("noPrimaryV")*GetSum("zvtxGT10")/GetSum("PrimaryV");
307   return GetSum("countForNorm")-noVtxzGT10;
308 }
309 //___________________________________________________________________________
310 Double_t AliNormalizationCounter::GetNEventsForNorm(Int_t runnumber){
311   TString listofruns = fCounters.GetKeyWords("RUN");
312   if(!listofruns.Contains(Form("%d",runnumber))){
313     printf("WARNING: %d is not a valid run number\n",runnumber);
314     fCounters.Print("Run","",kTRUE);
315     return 0.;
316   }
317   TString suffix;suffix.Form("/RUN:%d",runnumber);
318   TString zvtx;zvtx.Form("zvtxGT10%s",suffix.Data());
319   TString noPV;noPV.Form("noPrimaryV%s",suffix.Data());
320   TString pV;pV.Form("PrimaryV%s",suffix.Data());
321   TString tbc;tbc.Form("countForNorm%s",suffix.Data());
322   Double_t noVtxzGT10=GetSum(noPV.Data())*GetSum(zvtx.Data())/GetSum(pV.Data());
323   return GetSum(tbc.Data())-noVtxzGT10;
324 }
325 //___________________________________________________________________________
326 TH1D* AliNormalizationCounter::DrawNEventsForNorm(Bool_t drawRatio){
327   //usare algebra histos
328   fCounters.SortRubric("Run");
329   TString selection;
330
331   selection.Form("event:noPrimaryV");
332   TH1D* hnoPrimV = fCounters.Get("run",selection.Data());
333   hnoPrimV->Sumw2();
334
335   selection.Form("event:zvtxGT10");
336   TH1D*  hzvtx= fCounters.Get("run",selection.Data());
337   hzvtx->Sumw2();
338
339   selection.Form("event:PrimaryV");
340   TH1D* hPrimV = fCounters.Get("run",selection.Data());
341   hPrimV->Sumw2();
342
343   hzvtx->Multiply(hnoPrimV);
344   hzvtx->Divide(hPrimV);
345
346   selection.Form("event:countForNorm");
347   TH1D* hCountForNorm = fCounters.Get("run",selection.Data());
348   hCountForNorm->Sumw2();
349
350   hCountForNorm->Add(hzvtx,-1.);
351
352   if(drawRatio){
353     selection.Form("event:triggered");
354     TH1D* htriggered = fCounters.Get("run",selection.Data());
355     htriggered->Sumw2();
356     hCountForNorm->Divide(htriggered);
357   }
358
359   hCountForNorm->DrawClone();
360   return hCountForNorm;
361 }