]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGCF/FEMTOSCOPY/AliFemto/AliAnalysisTaskFemto.cxx
memory leak fixes in AliFemto code
[u/mrichter/AliRoot.git] / PWGCF / FEMTOSCOPY / AliFemto / AliAnalysisTaskFemto.cxx
1 //------------------------------------------------------
2 // AliAnalysisTaskFemto - A task for the analysis framework
3 // from the FEMTOSCOPY analysis of PWG2. Creates the necessary
4 // connection between the ESD or AOD input and the femtoscopic
5 // code.
6 // Author: Adam Kisiel, OSU; Adam.Kisiel@cern.ch
7 //------------------------------------------------------
8 #include "TROOT.h"
9 #include "TChain.h"
10 #include "TH1.h"
11 #include "TCanvas.h"
12 #include "TSystem.h"
13 #include "TFile.h"
14 #include "TInterpreter.h"
15
16 //#include "AliAnalysisTask.h"
17 #include "AliAnalysisTaskSE.h"
18
19 #include "AliESDEvent.h"
20
21 #include "AliFemtoAnalysis.h"
22 #include "AliAnalysisTaskFemto.h"
23 #include "AliVHeader.h"
24 #include "AliGenEventHeader.h"
25 #include "AliGenHijingEventHeader.h"
26 #include "AliGenCocktailEventHeader.h"
27
28 ClassImp(AliAnalysisTaskFemto)
29
30 // Default name for the setup macro of femto analysis  
31 // This function MUST be defined in the separate file !!!
32 // extern AliFemtoManager *ConfigFemtoAnalysis();
33
34 //________________________________________________________________________
35 AliAnalysisTaskFemto::AliAnalysisTaskFemto(TString name, TString aConfigMacro, TString aConfigParams, Bool_t aVerbose):
36 AliAnalysisTaskSE(name), //AliAnalysisTask(name,""), 
37   fESD(0), 
38   fESDpid(0),
39   fAOD(0),
40   fAODpidUtil(0),
41   fAODheader(0),
42   fStack(0),
43   fOutputList(0), 
44   fReader(0x0),
45   fManager(0x0),
46   fAnalysisType(0),
47     fConfigMacro(aConfigMacro),
48     fConfigParams(aConfigParams),
49     fVerbose(aVerbose)
50 {
51   // Constructor.
52   // Input slot #0 works with an Ntuple
53   //DefineInput(0, TChain::Class());
54   // Output slot #0 writes into a TH1 container
55   DefineOutput(0, TList::Class());
56
57 }
58 //________________________________________________________________________
59 AliAnalysisTaskFemto::AliAnalysisTaskFemto(TString name, TString aConfigMacro="ConfigFemtoAnalysis.C", Bool_t aVerbose): 
60     AliAnalysisTaskSE(name), //AliAnalysisTask(name,""), 
61     fESD(0), 
62     fESDpid(0),
63     fAOD(0),
64     fAODpidUtil(0),
65     fAODheader(0),
66     fStack(0),
67     fOutputList(0), 
68     fReader(0x0),
69     fManager(0x0),
70     fAnalysisType(0),
71     fConfigMacro(aConfigMacro),
72     fConfigParams(""),
73     fVerbose(aVerbose)
74 {
75   // Constructor.
76   // Input slot #0 works with an Ntuple
77   //DefineInput(0, TChain::Class());
78   // Output slot #0 writes into a TH1 container
79   DefineOutput(0, TList::Class());
80
81 }
82
83 AliAnalysisTaskFemto::AliAnalysisTaskFemto(const AliAnalysisTaskFemto& aFemtoTask):
84     AliAnalysisTaskSE(aFemtoTask), //AliAnalysisTask(aFemtoTask), 
85     fESD(0), 
86     fESDpid(0),
87     fAOD(0),
88     fAODpidUtil(0),
89     fAODheader(0),
90     fStack(0),
91     fOutputList(0), 
92     fReader(0x0),
93     fManager(0x0),
94     fAnalysisType(0),
95     fConfigMacro(0),
96     fConfigParams(0),
97     fVerbose(kFALSE)
98 {
99   // copy constructor
100   fESD = aFemtoTask.fESD; 
101   fESDpid = aFemtoTask.fESDpid; 
102   fAOD = aFemtoTask.fAOD; 
103   fAODpidUtil = aFemtoTask.fAODpidUtil;
104   fAODheader = aFemtoTask.fAODheader;
105   fStack = aFemtoTask.fStack;
106   fOutputList = aFemtoTask.fOutputList;   
107   fReader = aFemtoTask.fReader;       
108   fManager = aFemtoTask.fManager;      
109   fAnalysisType = aFemtoTask.fAnalysisType; 
110
111   fConfigMacro = aFemtoTask.fConfigMacro;
112   fConfigParams = aFemtoTask.fConfigParams;
113
114     fVerbose = aFemtoTask.fVerbose;
115 }
116
117
118 AliAnalysisTaskFemto& AliAnalysisTaskFemto::operator=(const AliAnalysisTaskFemto& aFemtoTask){
119   // assignment operator
120   if (this == &aFemtoTask)
121     return *this;
122
123   fESD = aFemtoTask.fESD; 
124   fESDpid = aFemtoTask.fESDpid;
125   fAOD = aFemtoTask.fAOD; 
126   fAODpidUtil = aFemtoTask.fAODpidUtil;
127   fAODheader = aFemtoTask.fAODheader;
128   fStack = aFemtoTask.fStack;
129   fOutputList = aFemtoTask.fOutputList;   
130   fReader = aFemtoTask.fReader;       
131   fManager = aFemtoTask.fManager;      
132   fAnalysisType = aFemtoTask.fAnalysisType; 
133
134   fConfigMacro = aFemtoTask.fConfigMacro;
135   fConfigParams = aFemtoTask.fConfigParams;
136     fVerbose = aFemtoTask.fVerbose;
137
138
139   return *this;
140 }
141
142 AliAnalysisTaskFemto::~AliAnalysisTaskFemto() 
143 {
144 }
145
146
147 //________________________________________________________________________
148 void AliAnalysisTaskFemto::ConnectInputData(Option_t *) {
149   AliInfo(Form("   ConnectInputData %s\n", GetName()));
150
151   fESD = 0;
152   fESDpid = 0;
153   fAOD = 0;
154   fAODpidUtil = 0;
155   fAODheader=0;
156   fAnalysisType = 0;
157
158   TTree* tree = dynamic_cast<TTree*> (GetInputData(0));
159   if (!tree) {
160     AliWarning("Could not read chain from input slot 0");
161     return;
162   } 
163
164   AliFemtoEventReaderESDChain *femtoReader = dynamic_cast<AliFemtoEventReaderESDChain *> (fReader);
165   if ((dynamic_cast<AliFemtoEventReaderESDChain *> (fReader))) {
166     AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
167     
168     if(esdH) {
169       if (fVerbose)
170         AliInfo("Selected ESD analysis");
171       fAnalysisType = 1;
172       
173       fESD = esdH->GetEvent();
174       fESDpid = esdH->GetESDpid();
175       femtoReader->SetESDPid(fESDpid);
176     }
177   }
178   else if ((dynamic_cast<AliFemtoEventReaderKinematicsChain *> (fReader))) {
179     AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
180     
181     if(esdH) {
182       if (fVerbose)
183         AliInfo("Selected ESD analysis");
184       fAnalysisType = 1;
185       fESD = esdH->GetEvent();
186     }
187   }
188  else if ((dynamic_cast<AliFemtoEventReaderKinematicsChainESD *> (fReader))) {
189     AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
190     if(esdH) {
191       if (fVerbose)
192         AliInfo("Selected ESD analysis");
193       fAnalysisType = 1;
194       fESD = esdH->GetEvent();
195     }
196   }
197
198   AliFemtoEventReaderESDChainKine *femtoReaderESDKine = dynamic_cast<AliFemtoEventReaderESDChainKine *> (fReader);
199   if ((dynamic_cast<AliFemtoEventReaderESDChainKine *> (fReader))) {
200     AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
201     
202     if(esdH) {
203       if (fVerbose)           
204         AliInfo("Selected ESD analysis");
205       fAnalysisType = 1;
206       fESD = esdH->GetEvent();
207       fESDpid = esdH->GetESDpid();
208       femtoReaderESDKine->SetESDPid(fESDpid);
209
210     }
211   }
212
213   //  AliFemtoEventReaderKinematicsChain *femtoReaderKine = dynamic_cast<AliFemtoEventReaderKinematicsChain *> (fReader);
214   if ((dynamic_cast<AliFemtoEventReaderKinematicsChain *> (fReader))) {
215     AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
216     
217     if(esdH) {
218             if (fVerbose)
219       AliInfo("Selected ESD analysis");
220       fAnalysisType = 1;
221       
222       //       if (!esdH) {
223       //        AliWarning("Could not get ESDInputHandler");
224       //       } 
225       //       else {
226       fESD = esdH->GetEvent();
227       //fESDpid = esdH->GetESDpid();
228       //femtoReader->SetESDPid(fESDpid);
229       //       }
230     }
231   }
232   
233   
234   AliFemtoEventReaderAODChain *femtoReaderAOD = dynamic_cast<AliFemtoEventReaderAODChain *> (fReader);
235   if (dynamic_cast<AliFemtoEventReaderAODChain *> (fReader)) {
236     AliAODInputHandler *aodH = dynamic_cast<AliAODInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
237     
238     if (!aodH) {
239       TObject *handler = AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler();
240             if (fVerbose)
241       AliInfo("Has output handler ");
242       if( handler && handler->InheritsFrom("AliAODHandler") ) {
243                 if (fVerbose)
244         AliInfo("Selected AOD analysis");
245         
246         fAOD = ((AliAODHandler*)handler)->GetAOD();
247         fAnalysisType = 2;
248       }
249       else {
250                 if (fVerbose)
251         AliWarning("Selected AOD reader but no AOD handler found");
252       }
253     } 
254     else {
255             if (fVerbose)
256       AliInfo("Selected AOD analysis");
257       fAnalysisType = 2;
258       
259       fAOD = aodH->GetEvent();
260
261       fAODpidUtil = aodH->GetAODpidUtil(); //correct way
262       //fAODpidUtil = new AliAODpidUtil(); //not correct way
263       //      printf("aodH->GetAODpidUtil(): %x",aodH->GetAODpidUtil());
264             if (fVerbose)
265       cout<<"AliAnalysisTaskFemto::AodpidUtil:"<<fAODpidUtil<<endl;
266       femtoReaderAOD->SetAODpidUtil(fAODpidUtil);
267
268       fAODheader = fAOD->GetHeader();
269       femtoReaderAOD->SetAODheader(fAODheader);
270    
271     }
272   }
273
274   if ((!fAOD) && (!fESD)) {
275         if (fVerbose)
276     AliWarning("Wrong analysis type: Only ESD and AOD types are allowed!");
277   }
278 }
279
280 //________________________________________________________________________
281 void AliAnalysisTaskFemto::CreateOutputObjects() {
282     if (fVerbose)
283   AliInfo("Creating Femto Analysis objects\n");
284
285   gSystem->SetIncludePath("-I$ROOTSYS/include -I./STEERBase/ -I./ESD/ -I./AOD/ -I./ANALYSIS/ -I./ANALYSISalice/ -I./PWG2AOD/AOD -I./PWG2femtoscopy/FEMTOSCOPY/AliFemto -I./PWG2femtoscopyUser/FEMTOSCOPY/AliFemtoUser");
286   //  char fcm[2000];
287 //   sprintf(fcm, "%s++", fConfigMacro);
288 //   gROOT->LoadMacro(fcm);
289   gROOT->LoadMacro(fConfigMacro);
290   //  fJetFinder = (AliJetFinder*) gInterpreter->ProcessLine("ConfigJetAnalysis()");
291   if (!fConfigParams)
292     SetFemtoManager((AliFemtoManager *) gInterpreter->ProcessLine("ConfigFemtoAnalysis()"));
293   else
294       SetFemtoManager((AliFemtoManager *) gInterpreter->ProcessLine(Form("ConfigFemtoAnalysis(%s)", fConfigParams.Data())));
295
296   TList *tOL;
297   fOutputList = fManager->Analysis(0)->GetOutputList();
298
299   for (unsigned int ian = 1; ian<fManager->AnalysisCollection()->size(); ian++) {
300     tOL = fManager->Analysis(ian)->GetOutputList();
301
302     TIter nextListCf(tOL);
303     while (TObject *obj = nextListCf()) {
304       fOutputList->Add(obj);
305     }
306
307     delete tOL;
308   }
309
310   PostData(0, fOutputList);
311 }
312
313 //________________________________________________________________________
314 void AliAnalysisTaskFemto::Exec(Option_t *) {
315   // Task making a femtoscopic analysis.
316   if(fOfflineTriggerMask){
317     Bool_t isSelected = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & fOfflineTriggerMask);
318     if(!isSelected) {
319       if (fVerbose)
320         cout << "AliAnalysisTaskFemto: is not selected" << endl; 
321       return;
322     }
323   }
324   if (fAnalysisType==1) {
325     if (!fESD) {
326       if (fVerbose)
327         AliWarning("fESD not available");
328       return;
329     }
330     //Get MC data
331     AliMCEventHandler*    mctruth = (AliMCEventHandler*) 
332       ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
333     
334     AliGenHijingEventHeader *hdh = 0;
335     if(mctruth) {
336       fStack = mctruth->MCEvent()->Stack();
337
338       AliGenCocktailEventHeader *hd = dynamic_cast<AliGenCocktailEventHeader *> (mctruth->MCEvent()->GenEventHeader());
339       
340       if (hd) {
341         
342         //      AliInfo ("Got MC cocktail event header %p\n", (void *) hd);
343         TList *lhd = hd->GetHeaders();
344         //      AliInfo ("Got list of headers %d\n", lhd->GetEntries());
345         
346         for (int iterh=0; iterh<lhd->GetEntries(); iterh++) 
347           {
348             hdh = dynamic_cast<AliGenHijingEventHeader *> (lhd->At(iterh));
349             //      AliInfo ("HIJING header at %i is %p\n", iterh, (void *) hdh);
350           }
351       }    
352     }
353
354     // Get ESD
355     AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
356     
357     if (!esdH) {
358       if (fVerbose)
359         AliWarning("Could not get ESDInputHandler");
360       return;
361     } 
362     else {
363       fESD = esdH->GetEvent();
364       fESDpid = esdH->GetESDpid();   
365      }
366
367     if (fVerbose)
368       AliInfo(Form("Tracks in ESD: %d \n",fESD->GetNumberOfTracks()));
369
370     if (fESD->GetNumberOfTracks() >= 0) {
371     
372       if (!fReader) {
373         if (fVerbose)
374           AliWarning("No ESD reader for ESD analysis !\n");
375       }
376       
377       AliFemtoEventReaderESDChain* fesdc = dynamic_cast<AliFemtoEventReaderESDChain *> (fReader);
378       if (fesdc)
379         {
380           // Process the event with no Kine information
381           fesdc->SetESDSource(fESD);
382           fManager->ProcessEvent();
383         }
384     }
385     AliFemtoEventReaderKinematicsChain* fkinec = dynamic_cast<AliFemtoEventReaderKinematicsChain *> (fReader);
386     if (fkinec)
387       {
388         // Process the event with Kine information only
389         fkinec->SetStackSource(fStack);
390         fManager->ProcessEvent();
391       }
392
393
394     AliFemtoEventReaderESDChainKine* fesdck = dynamic_cast<AliFemtoEventReaderESDChainKine *> (fReader);
395     if (fesdck) 
396       {
397         // Process the event with Kine information
398         fesdck->SetESDSource(fESD);
399         fesdck->SetStackSource(fStack);
400
401         fesdck->SetGenEventHeader(hdh);
402         fManager->ProcessEvent();
403       }
404
405      
406     AliFemtoEventReaderKinematicsChainESD* fkcesd = dynamic_cast<AliFemtoEventReaderKinematicsChainESD *> (fReader);
407     if (fkcesd)
408       {
409         // Process the event with Kine information
410         fkcesd->SetESDSource(fESD);
411         fkcesd->SetStackSource(fStack);
412         fkcesd->SetGenEventHeader(hdh);
413         fManager->ProcessEvent();
414       }
415
416     AliFemtoEventReaderStandard* fstd = dynamic_cast<AliFemtoEventReaderStandard *> (fReader);
417     if (fstd) 
418       {
419         // Process the event with Kine information
420         fstd->SetESDSource(fESD);
421         if (mctruth) {
422           fstd->SetStackSource(fStack);
423           fstd->SetGenEventHeader(hdh);
424           fstd->SetInputType(AliFemtoEventReaderStandard::kESDKine);
425         }
426         else
427           fstd->SetInputType(AliFemtoEventReaderStandard::kESD);
428         fManager->ProcessEvent();
429       }
430   
431
432     // Post the output histogram list
433     PostData(0, fOutputList);
434   }
435
436   if (fAnalysisType==2) {    
437     if (!fAOD) {
438             if (fVerbose)
439       AliWarning("fAOD not available");
440       return;
441     }
442
443     // Get AOD
444 //     AliAODInputHandler *aodH = dynamic_cast<AliAODInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
445       
446 //     if (!aodH) {
447 //       AliWarning("Could not get AODInputHandler");
448 //       return;
449 //     } 
450 //     else {
451
452 //       fAOD = aodH->GetEvent();
453 //     }
454
455
456
457
458     if (fVerbose)
459       AliInfo(Form("Tracks in AOD: %d \n",fAOD->GetNumberOfTracks()));
460     
461     if (fAOD->GetNumberOfTracks() > 0) {
462       if (!fReader) {
463         if (fVerbose)
464           AliWarning("No AOD reader for AOD analysis! \n");
465       }
466       else {
467         AliFemtoEventReaderAODChain* faodc = dynamic_cast<AliFemtoEventReaderAODChain *> (fReader);
468
469         if (faodc) {
470           // Process the event
471           faodc->SetAODSource(fAOD);
472           fManager->ProcessEvent();
473         }
474         AliFemtoEventReaderStandard* fstd = dynamic_cast<AliFemtoEventReaderStandard *> (fReader);
475
476         if (fstd) {
477           // Process the event
478           fstd->SetAODSource(fAOD);
479           fstd->SetInputType(AliFemtoEventReaderStandard::kAOD);
480           fManager->ProcessEvent();
481         }
482       }
483     } 
484
485     // Post the output histogram list
486     PostData(0, fOutputList);
487   }
488 }      
489
490 //________________________________________________________________________
491 void AliAnalysisTaskFemto::Terminate(Option_t *) {
492   // Do the final processing
493   if (fManager) {
494     fManager->Finish();
495   }
496 }
497 //________________________________________________________________________
498 void AliAnalysisTaskFemto:: FinishTaskOutput() {
499   // Do the final processing
500   if (fManager) {
501     fManager->Finish();
502   }
503 }
504 //________________________________________________________________________
505 void AliAnalysisTaskFemto::SetFemtoReaderESD(AliFemtoEventReaderESDChain *aReader)
506 {
507     if (fVerbose)
508   AliInfo("Selecting Femto reader for ESD\n");
509   fReader = aReader;
510 }
511 //________________________________________________________________________
512 void AliAnalysisTaskFemto::SetFemtoReaderESDKine(AliFemtoEventReaderESDChainKine *aReader)
513 {
514     if (fVerbose)
515   AliInfo("Selecting Femto reader for ESD with Kinematics information\n");
516   fReader = aReader;
517 }
518 //________________________________________________________________________
519 void AliAnalysisTaskFemto::SetFemtoReaderAOD(AliFemtoEventReaderAODChain *aReader)
520 {
521     if (fVerbose)
522   AliInfo("Selecting Femto reader for AOD\n");
523   fReader = aReader;
524 }
525 void AliAnalysisTaskFemto::SetFemtoReaderStandard(AliFemtoEventReaderStandard *aReader)
526 {
527     if (fVerbose)
528   AliInfo("Selecting Standard all-purpose Femto reader\n");
529   fReader = aReader;
530 }
531 void AliAnalysisTaskFemto::SetFemtoReaderKinematics(AliFemtoEventReaderKinematicsChain *aReader)
532 {
533     if (fVerbose)
534   printf("Selecting Femto reader for Kinematics (Monte Carlo) information\n");
535   fReader = aReader;
536 }
537 void AliAnalysisTaskFemto::SetFemtoReaderKinematicsESD(AliFemtoEventReaderKinematicsChainESD *aReader)
538 {
539   if (fVerbose)
540     printf("Selecting Femto reader for Kinematics (Monte Carlo) information + ESD\n");
541   fReader = aReader;
542 }
543 //________________________________________________________________________
544 void AliAnalysisTaskFemto::SetFemtoManager(AliFemtoManager *aManager)
545 {
546   fManager = aManager;
547     if (fVerbose)
548   AliInfo(Form("Got reader %p\n", (void *) aManager->EventReader()));
549   AliFemtoEventReaderESDChain     *tReaderESDChain     = dynamic_cast<AliFemtoEventReaderESDChain *> (aManager->EventReader());
550   AliFemtoEventReaderESDChainKine *tReaderESDChainKine = dynamic_cast<AliFemtoEventReaderESDChainKine *> (aManager->EventReader());
551   AliFemtoEventReaderAODChain     *tReaderAODChain     = dynamic_cast<AliFemtoEventReaderAODChain *> (aManager->EventReader());
552   AliFemtoEventReaderStandard     *tReaderStandard     = dynamic_cast<AliFemtoEventReaderStandard *> (aManager->EventReader());
553   AliFemtoEventReaderKinematicsChain *tReaderKineChain = dynamic_cast<AliFemtoEventReaderKinematicsChain *> (aManager->EventReader());
554   AliFemtoEventReaderKinematicsChainESD *tReaderKineChainESD = dynamic_cast<AliFemtoEventReaderKinematicsChainESD *> (aManager->EventReader());
555
556  if ((!tReaderESDChain) && (!tReaderESDChainKine) && (!tReaderAODChain) && (!tReaderStandard) && (!tReaderKineChain)  && (!tReaderKineChainESD)) {
557         if (fVerbose)
558     AliWarning("No AliFemto event reader created. Will not run femto analysis.\n");
559     return;
560   }
561   if (tReaderESDChain) SetFemtoReaderESD(tReaderESDChain);
562   if (tReaderESDChainKine) SetFemtoReaderESDKine(tReaderESDChainKine);
563   if (tReaderAODChain) SetFemtoReaderAOD(tReaderAODChain);
564   if (tReaderStandard) SetFemtoReaderStandard(tReaderStandard);
565   if (tReaderKineChain) SetFemtoReaderKinematics(tReaderKineChain);
566   if (tReaderKineChainESD) SetFemtoReaderKinematicsESD(tReaderKineChainESD);
567 }
568