]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliAnalysisTaskSE.cxx
New version including PWG2 AddTaskCentral, modified KINK resonances like sign macros...
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskSE.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, 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 #include <TROOT.h>
19 #include <TSystem.h>
20 #include <TInterpreter.h>
21 #include <TChain.h>
22 #include <TFile.h>
23 #include <TList.h>
24
25 #include "AliAnalysisTaskSE.h"
26 #include "AliAnalysisManager.h"
27 #include "AliAnalysisDataSlot.h"
28 #include "AliESDEvent.h"
29 #include "AliESD.h"
30 #include "AliAODEvent.h"
31 #include "AliAODHeader.h"
32 #include "AliAODTracklets.h"
33 #include "AliAODCaloCells.h"
34 #include "AliAODMCParticle.h"
35 #include "AliVEvent.h"
36 #include "AliAODHandler.h"
37 #include "AliAODInputHandler.h"
38 #include "AliMCEventHandler.h"
39 #include "AliInputEventHandler.h"
40 #include "AliMCEvent.h"
41 #include "AliStack.h"
42 #include "AliLog.h"
43
44
45 ClassImp(AliAnalysisTaskSE)
46
47 ////////////////////////////////////////////////////////////////////////
48 AliAODHeader*    AliAnalysisTaskSE::fgAODHeader        = NULL;
49 TClonesArray*    AliAnalysisTaskSE::fgAODTracks        = NULL;
50 TClonesArray*    AliAnalysisTaskSE::fgAODVertices      = NULL;
51 TClonesArray*    AliAnalysisTaskSE::fgAODV0s           = NULL;
52 TClonesArray*    AliAnalysisTaskSE::fgAODPMDClusters   = NULL;
53 TClonesArray*    AliAnalysisTaskSE::fgAODJets          = NULL;
54 TClonesArray*    AliAnalysisTaskSE::fgAODFMDClusters   = NULL;
55 TClonesArray*    AliAnalysisTaskSE::fgAODCaloClusters  = NULL;
56 TClonesArray*    AliAnalysisTaskSE::fgAODMCParticles   = NULL;
57 AliAODTracklets* AliAnalysisTaskSE::fgAODTracklets     = NULL;
58 AliAODCaloCells* AliAnalysisTaskSE::fgAODEmcalCells    = NULL;
59 AliAODCaloCells* AliAnalysisTaskSE::fgAODPhosCells     = NULL;
60
61
62 AliAnalysisTaskSE::AliAnalysisTaskSE():
63     AliAnalysisTask(),
64     fDebug(0),
65     fEntry(0),
66     fInputEvent(0x0),
67     fInputHandler(0x0),
68     fOutputAOD(0x0),
69     fMCEvent(0x0),
70     fTreeA(0x0),
71     fCurrentRunNumber(-1),
72     fHistosQA(0x0)
73 {
74   // Default constructor
75 }
76
77 AliAnalysisTaskSE::AliAnalysisTaskSE(const char* name):
78     AliAnalysisTask(name, "AnalysisTaskSE"),
79     fDebug(0),
80     fEntry(0),
81     fInputEvent(0x0),
82     fInputHandler(0x0),
83     fOutputAOD(0x0),
84     fMCEvent(0x0),
85     fTreeA(0x0),
86     fCurrentRunNumber(-1),
87     fHistosQA(0x0)
88 {
89   // Default constructor
90     DefineInput (0, TChain::Class());
91     DefineOutput(0,  TTree::Class());
92 }
93
94 AliAnalysisTaskSE::AliAnalysisTaskSE(const AliAnalysisTaskSE& obj):
95     AliAnalysisTask(obj),
96     fDebug(0),
97     fEntry(0),
98     fInputEvent(0x0),
99     fInputHandler(0x0),
100     fOutputAOD(0x0),
101     fMCEvent(0x0),
102     fTreeA(0x0),
103     fCurrentRunNumber(-1),
104     fHistosQA(0x0)
105 {
106 // Copy constructor
107     fDebug            = obj.fDebug;
108     fEntry            = obj.fEntry;
109     fInputEvent       = obj.fInputEvent;
110     fInputHandler     = obj.fInputHandler;
111     fOutputAOD        = obj.fOutputAOD;
112     fMCEvent          = obj.fMCEvent;
113     fTreeA            = obj.fTreeA;    
114     fCurrentRunNumber = obj.fCurrentRunNumber;
115     fHistosQA         = obj.fHistosQA;
116 }
117
118
119 AliAnalysisTaskSE& AliAnalysisTaskSE::operator=(const AliAnalysisTaskSE& other)
120 {
121 // Assignment
122     AliAnalysisTask::operator=(other);
123     fDebug            = other.fDebug;
124     fEntry            = other.fEntry;
125     fInputEvent       = other.fInputEvent;
126     fInputHandler     = other.fInputHandler;
127     fOutputAOD        = other.fOutputAOD;
128     fMCEvent          = other.fMCEvent;
129     fTreeA            = other.fTreeA;    
130     fCurrentRunNumber = other.fCurrentRunNumber;
131     fHistosQA         = other.fHistosQA;
132     return *this;
133 }
134
135
136 void AliAnalysisTaskSE::ConnectInputData(Option_t* /*option*/)
137 {
138 // Connect the input data
139     if (fDebug > 1) printf("AnalysisTaskSE::ConnectInputData() \n");
140 //
141 //  ESD
142 //
143     fInputHandler = (AliInputEventHandler*) 
144          ((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
145 //
146 //  Monte Carlo
147 //
148     AliMCEventHandler*    mcH = 0;
149     mcH = (AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
150     if (mcH) {
151         fMCEvent = mcH->MCEvent();
152     } 
153     
154     if (fInputHandler) {
155          fInputEvent = fInputHandler->GetEvent();
156     } else if( fMCEvent ) {
157          AliWarning("No Input Event Handler connected, only MC Truth Event Handler") ; 
158     } else {
159          AliError("No Input Event Handler connected") ; 
160          return ; 
161     }
162 }
163
164 void AliAnalysisTaskSE::CreateOutputObjects()
165 {
166 // Create the output container
167 //
168 //  Default AOD
169     if (fDebug > 1) printf("AnalysisTaskSE::CreateOutPutData() \n");
170
171     AliAODHandler* handler = (AliAODHandler*) 
172          ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
173     
174     Bool_t merging = kFALSE;
175     AliAODInputHandler* aodIH = static_cast<AliAODInputHandler*>((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
176     if (aodIH) {
177         if (aodIH->GetMergeEvents()) merging = kTRUE;
178     }
179     
180     // Check if AOD replication has been required
181     
182     if (handler) {
183         fOutputAOD   = handler->GetAOD();
184         fTreeA = handler->GetTree();
185         if (!(handler->IsStandard())) {
186             if ((handler->NeedsHeaderReplication()) && !(fgAODHeader)) 
187                 {
188                  if (fDebug > 1) AliInfo("Replicating header");
189                  fgAODHeader = new AliAODHeader;
190                  handler->AddBranch("AliAODHeader", &fgAODHeader);
191                 }
192             if ((handler->NeedsTracksBranchReplication() || merging) && !(fgAODTracks))      
193             {   
194                 if (fDebug > 1) AliInfo("Replicating track branch\n");
195                 fgAODTracks = new TClonesArray("AliAODTrack",500);
196                 fgAODTracks->SetName("tracks");
197                 handler->AddBranch("TClonesArray", &fgAODTracks);
198             }    
199             if ((handler->NeedsVerticesBranchReplication() || merging) && !(fgAODVertices))
200             {
201                 if (fDebug > 1) AliInfo("Replicating vertices branch\n");
202                 fgAODVertices = new TClonesArray("AliAODVertex",500);
203                 fgAODVertices->SetName("vertices");
204                 handler->AddBranch("TClonesArray", &fgAODVertices);
205             }   
206             if ((handler->NeedsV0sBranchReplication()) && !(fgAODV0s))    
207             {   
208                 if (fDebug > 1) AliInfo("Replicating V0s branch\n");
209                 fgAODV0s = new TClonesArray("AliAODv0",500);
210                 fgAODV0s->SetName("v0s");
211                 handler->AddBranch("TClonesArray", &fgAODV0s);
212             }
213             if ((handler->NeedsTrackletsBranchReplication()) && !(fgAODTracklets))        
214             {   
215                 if (fDebug > 1) AliInfo("Replicating Tracklets branch\n");
216                 fgAODTracklets = new AliAODTracklets("tracklets","tracklets");
217                 handler->AddBranch("AliAODTracklets", &fgAODTracklets);
218             }
219             if ((handler->NeedsPMDClustersBranchReplication()) && !(fgAODPMDClusters))    
220             {   
221                 if (fDebug > 1) AliInfo("Replicating PMDClusters branch\n");
222                 fgAODPMDClusters = new TClonesArray("AliAODPmdCluster",500);
223                 fgAODPMDClusters->SetName("pmdClusters");
224                 handler->AddBranch("TClonesArray", &fgAODPMDClusters);
225             }
226             if ((handler->NeedsJetsBranchReplication() || merging) && !(fgAODJets))       
227             {   
228                 if (fDebug > 1) AliInfo("Replicating Jets branch\n");
229                 fgAODJets = new TClonesArray("AliAODJet",500);
230                 fgAODJets->SetName("jets");
231                 handler->AddBranch("TClonesArray", &fgAODJets);
232             }
233             if ((handler->NeedsFMDClustersBranchReplication()) && !(fgAODFMDClusters))    
234             {   
235                 AliInfo("Replicating FMDClusters branch\n");
236                 fgAODFMDClusters = new TClonesArray("AliAODFmdCluster",500);
237                 fgAODFMDClusters->SetName("fmdClusters");
238                 handler->AddBranch("TClonesArray", &fgAODFMDClusters);
239             }
240             if ((handler->NeedsCaloClustersBranchReplication() || merging) && !(fgAODCaloClusters))       
241             {   
242                 if (fDebug > 1) AliInfo("Replicating CaloClusters branch\n");
243                 fgAODCaloClusters = new TClonesArray("AliAODCaloCluster",500);
244                 fgAODCaloClusters->SetName("caloClusters");
245                 handler->AddBranch("TClonesArray", &fgAODCaloClusters);
246
247                 fgAODEmcalCells = new AliAODCaloCells();
248                 fgAODEmcalCells->SetName("emcalCells");
249                 handler->AddBranch("AliAODCaloCells", &fgAODEmcalCells);
250
251                 fgAODPhosCells = new AliAODCaloCells();
252                 fgAODPhosCells->SetName("phosCells");
253                 handler->AddBranch("AliAODCaloCells", &fgAODPhosCells);
254             }
255             if ((handler->NeedsMCParticlesBranchReplication() || merging) && !(fgAODMCParticles))         
256             {   
257                 if (fDebug > 1) AliInfo("Replicating MCParticles branch\n");
258                 fgAODMCParticles = new TClonesArray("AliAODMCParticle",500);
259                 fgAODMCParticles->SetName("mcparticles");
260                 handler->AddBranch("TClonesArray", &fgAODMCParticles);
261             }
262             // cache the pointerd in the AODEvent
263             fOutputAOD->GetStdContent();
264         }
265     } else {
266         AliWarning("No AOD Event Handler connected.") ; 
267     }
268     UserCreateOutputObjects();
269 }
270
271 void AliAnalysisTaskSE::Exec(Option_t* option)
272 {
273 //
274 // Exec analysis of one event
275     if (fDebug > 1) AliInfo("AliAnalysisTaskSE::Exec() \n");
276
277     if( fInputHandler ) {
278        fEntry = fInputHandler->GetReadEntry();
279     }
280 // Notify the change of run number
281     if (InputEvent()->GetRunNumber() != fCurrentRunNumber) {
282         fCurrentRunNumber = InputEvent()->GetRunNumber();
283         NotifyRun();
284     }    
285            
286     else if( fMCEvent )
287        fEntry = fMCEvent->Header()->GetEvent(); 
288     if ( !((Entry()-1)%100) && fDebug > 0) 
289          AliInfo(Form("%s ----> Processing event # %lld", CurrentFileName(), Entry()));
290
291     AliAODHandler* handler = (AliAODHandler*) 
292         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
293     AliAODInputHandler* aodH = dynamic_cast<AliAODInputHandler*>(fInputHandler);
294
295     if (handler && aodH) {
296         fMCEvent = aodH->MCEvent();
297         Bool_t merging = aodH->GetMergeEvents();
298         
299         if (!(handler->IsStandard()) && !(handler->AODIsReplicated())) {
300             if ((handler->NeedsHeaderReplication()) && (fgAODHeader))
301             {
302               // copy the contents by assigment
303               *fgAODHeader =  *(dynamic_cast<AliAODHeader*>(InputEvent()->GetHeader()));
304             }
305             if ((handler->NeedsTracksBranchReplication() || merging) && (fgAODTracks))
306             {
307                 TClonesArray* tracks = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracks();
308                 new (fgAODTracks) TClonesArray(*tracks);
309             }
310             if ((handler->NeedsVerticesBranchReplication() || merging) && (fgAODVertices))
311             {
312                 TClonesArray* vertices = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetVertices();
313                 new (fgAODVertices) TClonesArray(*vertices);
314             }
315             if ((handler->NeedsV0sBranchReplication()) && (fgAODV0s))
316             {
317                 TClonesArray* v0s = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetV0s();
318                 new (fgAODV0s) TClonesArray(*v0s);
319             }
320             if ((handler->NeedsTrackletsBranchReplication()) && (fgAODTracklets))
321             {
322               *fgAODTracklets = *(dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracklets();
323             }
324             if ((handler->NeedsPMDClustersBranchReplication()) && (fgAODPMDClusters))
325             {
326                 TClonesArray* pmdClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetPmdClusters();
327                 new (fgAODPMDClusters) TClonesArray(*pmdClusters);
328             }
329             if ((handler->NeedsJetsBranchReplication() || merging) && (fgAODJets))
330             {
331                 TClonesArray* jets = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetJets();
332                 new (fgAODJets) TClonesArray(*jets);
333             }
334             if ((handler->NeedsFMDClustersBranchReplication()) && (fgAODFMDClusters))
335             {
336                 TClonesArray* fmdClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetFmdClusters();
337                 new (fgAODFMDClusters) TClonesArray(*fmdClusters);
338             }
339             if ((handler->NeedsCaloClustersBranchReplication() || merging) && (fgAODCaloClusters))
340             {
341                 TClonesArray* caloClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetCaloClusters();
342                 new (fgAODCaloClusters) TClonesArray(*caloClusters);
343             }
344
345             if ((handler->NeedsMCParticlesBranchReplication() || merging) && (fgAODMCParticles))
346             {
347                 TClonesArray* mcParticles = (TClonesArray*) ((dynamic_cast<AliAODEvent*>(InputEvent()))->FindListObject("mcparticles"));
348                 new (fgAODMCParticles) TClonesArray(*mcParticles);
349             }
350         
351             // Additional merging if needed
352             if (merging) {
353                 // mcParticles
354                 TClonesArray* mcparticles = (TClonesArray*) ((aodH->GetEventToMerge())->FindListObject("mcparticles"));
355                 Int_t npart = mcparticles->GetEntries();
356                 Int_t nc = fgAODMCParticles->GetEntries();
357                 Int_t nc0 = nc;
358                 
359                 for (Int_t i = 0; i < npart; i++) {
360                     AliAODMCParticle* particle = (AliAODMCParticle*) mcparticles->At(i);
361                     new((*fgAODMCParticles)[nc++]) AliAODMCParticle(*particle);
362                 }
363                 
364                 // tracks
365                 TClonesArray* tracks = aodH->GetEventToMerge()->GetTracks();
366                 Int_t ntr = tracks->GetEntries();
367                 nc  = fgAODTracks->GetEntries();        
368                 for (Int_t i = 0; i < ntr; i++) {
369                     AliAODTrack*    track = (AliAODTrack*) tracks->At(i);
370                     AliAODTrack* newtrack = new((*fgAODTracks)[nc++]) AliAODTrack(*track);
371
372                     newtrack->SetLabel(newtrack->GetLabel() + nc0);
373                 }
374
375                 for (Int_t i = 0; i < nc; i++) 
376                 {
377                     AliAODTrack* track = (AliAODTrack*) fgAODTracks->At(i);
378                     track->ResetBit(kIsReferenced);
379                     track->SetUniqueID(0);
380                 }
381                 
382                 
383                 // clusters
384                 TClonesArray* clusters = aodH->GetEventToMerge()->GetCaloClusters();
385                 Int_t ncl  = clusters->GetEntries();
386                 nc         =  fgAODCaloClusters->GetEntries();
387                 for (Int_t i = 0; i < ncl; i++) {
388                     AliAODCaloCluster*    cluster = (AliAODCaloCluster*) clusters->At(i);
389                     new((*fgAODCaloClusters)[nc++]) AliAODCaloCluster(*cluster);
390                 }
391                 // cells
392                 AliAODCaloCells* cellsA = aodH->GetEventToMerge()->GetEMCALCells();
393                 Int_t ncells  = cellsA->GetNumberOfCells();
394                 nc = fgAODEmcalCells->GetNumberOfCells();
395                 
396                 for (Int_t i  = 0; i < ncells; i++) {
397                     Int_t cn  = cellsA->GetCellNumber(i);
398                     Int_t pos = fgAODEmcalCells->GetCellPosition(cn);
399                     if (pos >= 0) {
400                         Double_t amp = cellsA->GetAmplitude(i) + fgAODEmcalCells->GetAmplitude(pos);
401                         fgAODEmcalCells->SetCell(pos, cn, amp);
402                     } else {
403                         Double_t amp = cellsA->GetAmplitude(i);
404                         fgAODEmcalCells->SetCell(nc++, cn, amp);
405                         fgAODEmcalCells->Sort();
406                     }
407                 }
408                 
409                 
410             } // merging
411             
412             handler->SetAODIsReplicated();
413         }
414     }
415
416 // Call the user analysis    
417     UserExec(option);
418     // Added protection in case the derived task is not an AOD producer.
419     AliAnalysisDataSlot *out0 = GetOutputSlot(0);
420     if (out0 && out0->IsConnected()) PostData(0, fTreeA);    
421 }
422
423 const char* AliAnalysisTaskSE::CurrentFileName()
424 {
425 // Returns the current file name    
426     if( fInputHandler )
427       return fInputHandler->GetTree()->GetCurrentFile()->GetName();
428     else if( fMCEvent )
429       return ((AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler()))->TreeK()->GetCurrentFile()->GetName();
430     else return "";
431 }
432
433 void AliAnalysisTaskSE::AddAODBranch(const char* cname, void* addobj, const char *fname)
434 {
435     // Add a new branch to the aod tree
436     AliAODHandler* handler = (AliAODHandler*) 
437         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
438     if (handler) {
439         handler->AddBranch(cname, addobj, fname);
440     }
441 }
442
443 Bool_t AliAnalysisTaskSE::IsStandardAOD() const
444 {
445 // Check if the output AOD handler is configured for standard or delta AOD.
446 // Users should first check that AODEvent() returns non-null.
447     AliAODHandler* handler = (AliAODHandler*) 
448          ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
449     if (!handler) {
450        Error("IsStandardAOD", "No AOD handler. Please use AODEvent() to check this first");
451        return kTRUE;
452     }
453     return handler->IsStandard();   
454 }
455
456 Bool_t AliAnalysisTaskSE::Notify()
457 {
458     return (UserNotify());
459 }
460
461