]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGDQ/dielectron/AliAnalysisTaskMultiDielectron.cxx
small update
[u/mrichter/AliRoot.git] / PWGDQ / dielectron / AliAnalysisTaskMultiDielectron.cxx
1 /*************************************************************************
2 * Copyright(c) 1998-2009, 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 ///////////////////////////////////////////////////////////////////////////
17 //                                                                       //
18 //                        Basic Analysis Task                            //
19 //                                                                       //
20 ///////////////////////////////////////////////////////////////////////////
21
22 #include <TChain.h>
23 #include <TH1D.h>
24
25 #include <AliCFContainer.h>
26 #include <AliInputEventHandler.h>
27 #include <AliESDInputHandler.h>
28 #include <AliAODInputHandler.h>
29 #include <AliAnalysisManager.h>
30 #include <AliVEvent.h>
31 #include <AliTriggerAnalysis.h>
32 #include <AliPIDResponse.h>
33 #include <AliTPCPIDResponse.h>
34
35 #include "AliDielectron.h"
36 #include "AliDielectronHistos.h"
37 #include "AliDielectronCF.h"
38 #include "AliDielectronMC.h"
39 #include "AliDielectronMixingHandler.h"
40 #include "AliAnalysisTaskMultiDielectron.h"
41
42 ClassImp(AliAnalysisTaskMultiDielectron)
43
44 //_________________________________________________________________________________
45 AliAnalysisTaskMultiDielectron::AliAnalysisTaskMultiDielectron() :
46   AliAnalysisTaskSE(),
47         fPairArray(0x0),
48   fListDielectron(),
49   fListHistos(),
50   fListCF(),
51   fSelectPhysics(kFALSE),
52   fTriggerMask(AliVEvent::kMB),
53   fExcludeTriggerMask(0),
54   fTriggerOnV0AND(kFALSE),
55   fFiredTrigger(""),
56   fFiredExclude(kFALSE),
57   fRejectPileup(kFALSE),
58   fBeamEnergy(-1.),
59   fTriggerLogic(kAny),
60   fTriggerAnalysis(0x0),
61   fEventFilter(0x0),
62   fEventStat(0x0)
63 {
64   //
65   // Constructor
66   //
67 }
68
69 //_________________________________________________________________________________
70 AliAnalysisTaskMultiDielectron::AliAnalysisTaskMultiDielectron(const char *name) :
71   AliAnalysisTaskSE(name),
72   fPairArray(0x0),
73   fListDielectron(),
74   fListHistos(),
75   fListCF(),
76   fSelectPhysics(kFALSE),
77   fTriggerMask(AliVEvent::kMB),
78   fExcludeTriggerMask(0),
79   fTriggerOnV0AND(kFALSE),
80   fFiredTrigger(""),
81   fFiredExclude(kFALSE),
82   fRejectPileup(kFALSE),
83   fBeamEnergy(-1.),
84   fTriggerLogic(kAny),
85   fTriggerAnalysis(0x0),
86   fEventFilter(0x0),
87   fEventStat(0x0)
88 {
89   //
90   // Constructor
91   //
92   DefineInput(0,TChain::Class());
93   DefineOutput(1, TList::Class());
94   DefineOutput(2, TList::Class());
95   DefineOutput(3, TH1D::Class());
96   fListHistos.SetName("Dielectron_Histos_Multi");
97   fListCF.SetName("Dielectron_CF_Multi");
98   fListDielectron.SetOwner();
99   fListHistos.SetOwner();
100   fListCF.SetOwner();
101 }
102
103 //_________________________________________________________________________________
104 AliAnalysisTaskMultiDielectron::~AliAnalysisTaskMultiDielectron()
105 {
106   //
107   // Destructor
108   //
109
110   //histograms and CF are owned by the dielectron framework.
111   //however they are streamed to file, so in the first place the
112   //lists need to be owner...
113   fListHistos.SetOwner(kFALSE);
114   fListCF.SetOwner(kFALSE);
115
116   //  if(fPairArray)       { delete fPairArray;       fPairArray=0; }
117   // try to reduce memory issues
118   if(fEventStat)       { delete fEventStat;       fEventStat=0; }
119   if(fTriggerAnalysis) { delete fTriggerAnalysis; fTriggerAnalysis=0; }
120 }
121 //_________________________________________________________________________________
122 void AliAnalysisTaskMultiDielectron::UserCreateOutputObjects()
123 {
124   //
125   // Add all histogram manager histogram lists to the output TList
126   //
127
128   if (!fListHistos.IsEmpty()||!fListCF.IsEmpty()) return; //already initialised
129
130 //   AliAnalysisManager *man=AliAnalysisManager::GetAnalysisManager();
131 //   Bool_t isESD=man->GetInputEventHandler()->IsA()==AliESDInputHandler::Class();
132 //   Bool_t isAOD=man->GetInputEventHandler()->IsA()==AliAODInputHandler::Class();
133   
134   TIter nextDie(&fListDielectron);
135   AliDielectron *die=0;
136   while ( (die=static_cast<AliDielectron*>(nextDie())) ){
137     die->Init();
138     if (die->GetHistogramList())    fListHistos.Add(const_cast<THashList*>(die->GetHistogramList()));
139     if (die->GetHistogramArray())   fListHistos.Add(const_cast<TObjArray*>(die->GetHistogramArray()));
140     if (die->GetQAHistArray())      fListHistos.Add(const_cast<TObjArray*>(die->GetQAHistArray()));
141     if (die->GetCFManagerPair())    fListCF.Add(const_cast<AliCFContainer*>(die->GetCFManagerPair()->GetContainer()));
142   }
143
144   Int_t cuts=fListDielectron.GetEntries();
145   Int_t nbins=kNbinsEvent+2*cuts;
146   if (!fEventStat){
147     fEventStat=new TH1D("hEventStat","Event statistics",nbins,0,nbins);
148     fEventStat->GetXaxis()->SetBinLabel(1,"Before Phys. Sel.");
149     fEventStat->GetXaxis()->SetBinLabel(2,"After Phys. Sel.");
150
151     //default names
152     fEventStat->GetXaxis()->SetBinLabel(3,"Bin3 not used");
153     fEventStat->GetXaxis()->SetBinLabel(4,"Bin4 not used");
154     fEventStat->GetXaxis()->SetBinLabel(5,"Bin5 not used");
155     
156     if(fTriggerOnV0AND) fEventStat->GetXaxis()->SetBinLabel(3,"V0and triggers");
157     if (fEventFilter) fEventStat->GetXaxis()->SetBinLabel(4,"After Event Filter");
158     if (fRejectPileup) fEventStat->GetXaxis()->SetBinLabel(5,"After Pileup rejection");
159     
160     for (Int_t i=0; i<cuts; ++i){
161       fEventStat->GetXaxis()->SetBinLabel((kNbinsEvent+1)+2*i,Form("#splitline{1 candidate}{%s}",fListDielectron.At(i)->GetName()));
162       fEventStat->GetXaxis()->SetBinLabel((kNbinsEvent+2)+2*i,Form("#splitline{With >1 candidate}{%s}",fListDielectron.At(i)->GetName()));
163     }
164   }
165
166   if (!fTriggerAnalysis) fTriggerAnalysis=new AliTriggerAnalysis;
167   fTriggerAnalysis->EnableHistograms();
168   fTriggerAnalysis->SetAnalyzeMC(AliDielectronMC::Instance()->HasMC());
169   
170   PostData(1, &fListHistos);
171   PostData(2, &fListCF);
172   PostData(3, fEventStat);
173 }
174
175 //_________________________________________________________________________________
176 void AliAnalysisTaskMultiDielectron::UserExec(Option_t *)
177 {
178   //
179   // Main loop. Called for every event
180   //
181
182   if (fListHistos.IsEmpty()&&fListCF.IsEmpty()) return;
183
184   AliAnalysisManager *man=AliAnalysisManager::GetAnalysisManager();
185   Bool_t isESD=man->GetInputEventHandler()->IsA()==AliESDInputHandler::Class();
186   Bool_t isAOD=man->GetInputEventHandler()->IsA()==AliAODInputHandler::Class();
187   
188   AliInputEventHandler* inputHandler = (AliInputEventHandler*) (man->GetInputEventHandler());
189   if (!inputHandler) return;
190   
191 //   AliPIDResponse *pidRes=inputHandler->GetPIDResponse();
192   if ( inputHandler->GetPIDResponse() ){
193     // for the 2.76 pass2 MC private train. Together with a sigma shift of -0.169
194 //    pidRes->GetTPCResponse().SetSigma(4.637e-3,2.41332105409873257e+04);
195     AliDielectronVarManager::SetPIDResponse( inputHandler->GetPIDResponse() );
196   } else {
197     AliFatal("This task needs the PID response attached to the input event handler!");
198   }
199   
200   // Was event selected ?
201   ULong64_t isSelected = AliVEvent::kAny;
202   Bool_t isRejected = kFALSE;
203   if( fSelectPhysics && inputHandler){
204     if((isESD && inputHandler->GetEventSelection()) || isAOD){
205       isSelected = inputHandler->IsEventSelected();
206       if (fExcludeTriggerMask && (isSelected&fExcludeTriggerMask)) isRejected=kTRUE;
207       if (fTriggerLogic==kAny) isSelected&=fTriggerMask;
208       else if (fTriggerLogic==kExact) isSelected=((isSelected&fTriggerMask)==fTriggerMask);
209    
210       TString firedTriggerClasses=InputEvent()->GetFiredTriggerClasses();
211       if(!fFiredTrigger.IsNull()) isSelected=(firedTriggerClasses.Contains(fFiredTrigger))^fFiredExclude;
212     }
213    }
214  
215  
216   //Before physics selection
217   fEventStat->Fill(kAllEvents);
218   if (isSelected==0||isRejected) {
219     PostData(3,fEventStat);
220     return;
221   }
222   //after physics selection
223   fEventStat->Fill(kSelectedEvents);
224
225   //V0and
226   if(fTriggerOnV0AND){
227   if(isESD){if (!fTriggerAnalysis->IsOfflineTriggerFired(static_cast<AliESDEvent*>(InputEvent()), AliTriggerAnalysis::kV0AND))
228             return;}
229   if(isAOD){if(!((static_cast<AliAODEvent*>(InputEvent()))->GetVZEROData()->GetV0ADecision() == AliVVZERO::kV0BB &&
230             (static_cast<AliAODEvent*>(InputEvent()))->GetVZEROData()->GetV0CDecision() == AliVVZERO::kV0BB) )
231             return;}
232    }
233   
234
235   fEventStat->Fill(kV0andEvents);
236
237   //Fill Event histograms before the event filter
238   TIter nextDie(&fListDielectron);
239   AliDielectron *die=0;
240   Bool_t hasMC=AliDielectronMC::Instance()->HasMC();
241   while ( (die=static_cast<AliDielectron*>(nextDie())) ){
242     AliDielectronHistos *h=die->GetHistoManager();
243     if (h){
244       AliDielectronVarManager::SetFillMap(h->GetUsedVars());
245       if (hasMC && AliDielectronMC::Instance()->ConnectMCEvent() && h->GetHistogramList()->FindObject("MCEvent_noCuts")) {
246         AliDielectronVarManager::SetEvent(AliDielectronMC::Instance()->GetMCEvent());
247         h->FillClass("MCEvent_noCuts",AliDielectronVarManager::kNMaxValues,AliDielectronVarManager::GetData());
248       }
249       if (h->GetHistogramList()->FindObject("Event_noCuts")) {
250         AliDielectronVarManager::SetEvent(InputEvent());
251         h->FillClass("Event_noCuts",AliDielectronVarManager::kNMaxValues,AliDielectronVarManager::GetData());
252       }
253     }
254   }
255   nextDie.Reset();
256   
257   //event filter
258   if (fEventFilter) {
259     if (!fEventFilter->IsSelected(InputEvent())) return;
260   }
261   fEventStat->Fill(kFilteredEvents);
262   
263   //pileup
264   if (fRejectPileup){
265     if (InputEvent()->IsPileupFromSPD(3,0.8,3.,2.,5.)) return;
266   }
267   fEventStat->Fill(kPileupEvents);
268   
269   //bz for AliKF
270   Double_t bz = InputEvent()->GetMagneticField();
271   AliKFParticle::SetField( bz );
272
273   AliDielectronPID::SetCorrVal((Double_t)InputEvent()->GetRunNumber());
274   AliDielectronPair::SetBeamEnergy(InputEvent(), fBeamEnergy);
275   
276   //Process event in all AliDielectron instances
277   //   TIter nextDie(&fListDielectron);
278   //   AliDielectron *die=0;
279   Bool_t sel=kFALSE;
280   Int_t idie=0;
281   while ( (die=static_cast<AliDielectron*>(nextDie())) ){
282     if(die->DoEventProcess()) {
283       sel= die->Process(InputEvent());
284       // input for internal train
285       if(die->DontClearArrays()) {
286         fPairArray = (*(die->GetPairArraysPointer()));
287
288       }
289     }
290     else {
291       // internal train
292       if(sel) die->Process(fPairArray);
293     }
294
295     if (die->HasCandidates()){
296       Int_t ncandidates=die->GetPairArray(1)->GetEntriesFast();
297       if (ncandidates==1) fEventStat->Fill((kNbinsEvent)+2*idie);
298       else if (ncandidates>1) fEventStat->Fill((kNbinsEvent+1)+2*idie);
299     }
300     ++idie;
301   }
302
303   PostData(1, &fListHistos);
304   PostData(2, &fListCF);
305   PostData(3,fEventStat);
306 }
307
308 //_________________________________________________________________________________
309 void AliAnalysisTaskMultiDielectron::FinishTaskOutput()
310 {
311   //
312   // Write debug tree
313   //
314   TIter nextDie(&fListDielectron);
315   Int_t ic=0;
316   AliDielectron *die=0;
317   AliDielectron *die2=0;
318   fPairArray=0x0;
319
320   // main loop
321   while ( (die=static_cast<AliDielectron*>(nextDie())) ){
322     ic++;
323
324     // debug tree
325     die->SaveDebugTree();
326
327     // skip internal train tasks in main loop
328     if(!die->DoEventProcess()) continue;
329
330     // mix remaining
331     AliDielectronMixingHandler *mix=die->GetMixingHandler();
332     if (!mix || !mix->GetMixUncomplete()) continue;
333
334     // loop over all pools
335     for (Int_t ipool=0; ipool<mix->GetNumberOfBins(); ++ipool){
336       //      printf("mix remaining %04d/%04d \n",ipool,mix->GetNumberOfBins());
337       if(! mix->MixRemaining(die, ipool) ) { fPairArray=0x0;  continue; }
338
339       fPairArray = (*(die->GetPairArraysPointer()));
340       if(!fPairArray) continue;
341
342       // loop over internal train task candidates
343       for(Int_t i=ic; i<fListDielectron.GetEntries(); i++) {
344         die2 = static_cast<AliDielectron*>(fListDielectron.At(i));
345         // abort if tasks following are not internal wagons
346         if(die2->DoEventProcess()) break;
347         // fill internal train output
348         die2->SetPairArraysPointer(fPairArray);
349         //      printf(" --> fill internal train output %s \n",die2->GetName());
350         die2->FillHistogramsFromPairArray(kTRUE);
351       }
352       // printf("\n\n\n===============\ncall mix in Terminate: %p (%p)\n=================\n\n",mix,die);
353
354     }
355
356   }
357
358   PostData(1, &fListHistos);
359   PostData(2, &fListCF);
360 }
361