Load 1st event in Notify()
[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     
281            
282     else if( fMCEvent )
283        fEntry = fMCEvent->Header()->GetEvent(); 
284     if ( !((Entry()-1)%100) && fDebug > 0) 
285          AliInfo(Form("%s ----> Processing event # %lld", CurrentFileName(), Entry()));
286
287     AliAODHandler* handler = (AliAODHandler*) 
288         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
289     AliAODInputHandler* aodH = dynamic_cast<AliAODInputHandler*>(fInputHandler);
290
291     if (handler && aodH) {
292         fMCEvent = aodH->MCEvent();
293         Bool_t merging = aodH->GetMergeEvents();
294         
295         if (!(handler->IsStandard()) && !(handler->AODIsReplicated())) {
296             if ((handler->NeedsHeaderReplication()) && (fgAODHeader))
297             {
298               // copy the contents by assigment
299               *fgAODHeader =  *(dynamic_cast<AliAODHeader*>(InputEvent()->GetHeader()));
300             }
301             if ((handler->NeedsTracksBranchReplication() || merging) && (fgAODTracks))
302             {
303                 TClonesArray* tracks = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracks();
304                 new (fgAODTracks) TClonesArray(*tracks);
305             }
306             if ((handler->NeedsVerticesBranchReplication() || merging) && (fgAODVertices))
307             {
308                 TClonesArray* vertices = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetVertices();
309                 new (fgAODVertices) TClonesArray(*vertices);
310             }
311             if ((handler->NeedsV0sBranchReplication()) && (fgAODV0s))
312             {
313                 TClonesArray* v0s = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetV0s();
314                 new (fgAODV0s) TClonesArray(*v0s);
315             }
316             if ((handler->NeedsTrackletsBranchReplication()) && (fgAODTracklets))
317             {
318               *fgAODTracklets = *(dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracklets();
319             }
320             if ((handler->NeedsPMDClustersBranchReplication()) && (fgAODPMDClusters))
321             {
322                 TClonesArray* pmdClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetPmdClusters();
323                 new (fgAODPMDClusters) TClonesArray(*pmdClusters);
324             }
325             if ((handler->NeedsJetsBranchReplication() || merging) && (fgAODJets))
326             {
327                 TClonesArray* jets = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetJets();
328                 new (fgAODJets) TClonesArray(*jets);
329             }
330             if ((handler->NeedsFMDClustersBranchReplication()) && (fgAODFMDClusters))
331             {
332                 TClonesArray* fmdClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetFmdClusters();
333                 new (fgAODFMDClusters) TClonesArray(*fmdClusters);
334             }
335             if ((handler->NeedsCaloClustersBranchReplication() || merging) && (fgAODCaloClusters))
336             {
337                 TClonesArray* caloClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetCaloClusters();
338                 new (fgAODCaloClusters) TClonesArray(*caloClusters);
339             }
340
341             if ((handler->NeedsMCParticlesBranchReplication() || merging) && (fgAODMCParticles))
342             {
343                 TClonesArray* mcParticles = (TClonesArray*) ((dynamic_cast<AliAODEvent*>(InputEvent()))->FindListObject("mcparticles"));
344                 new (fgAODMCParticles) TClonesArray(*mcParticles);
345             }
346         
347             // Additional merging if needed
348             if (merging) {
349                 // mcParticles
350                 TClonesArray* mcparticles = (TClonesArray*) ((aodH->GetEventToMerge())->FindListObject("mcparticles"));
351                 Int_t npart = mcparticles->GetEntries();
352                 Int_t nc = fgAODMCParticles->GetEntries();
353                 Int_t nc0 = nc;
354                 
355                 for (Int_t i = 0; i < npart; i++) {
356                     AliAODMCParticle* particle = (AliAODMCParticle*) mcparticles->At(i);
357                     new((*fgAODMCParticles)[nc++]) AliAODMCParticle(*particle);
358                 }
359                 
360                 // tracks
361                 TClonesArray* tracks = aodH->GetEventToMerge()->GetTracks();
362                 Int_t ntr = tracks->GetEntries();
363                 nc  = fgAODTracks->GetEntries();        
364                 for (Int_t i = 0; i < ntr; i++) {
365                     AliAODTrack*    track = (AliAODTrack*) tracks->At(i);
366                     AliAODTrack* newtrack = new((*fgAODTracks)[nc++]) AliAODTrack(*track);
367
368                     newtrack->SetLabel(newtrack->GetLabel() + nc0);
369                 }
370
371                 for (Int_t i = 0; i < nc; i++) 
372                 {
373                     AliAODTrack* track = (AliAODTrack*) fgAODTracks->At(i);
374                     track->ResetBit(kIsReferenced);
375                     track->SetUniqueID(0);
376                 }
377                 
378                 
379                 // clusters
380                 TClonesArray* clusters = aodH->GetEventToMerge()->GetCaloClusters();
381                 Int_t ncl  = clusters->GetEntries();
382                 nc         =  fgAODCaloClusters->GetEntries();
383                 for (Int_t i = 0; i < ncl; i++) {
384                     AliAODCaloCluster*    cluster = (AliAODCaloCluster*) clusters->At(i);
385                     new((*fgAODCaloClusters)[nc++]) AliAODCaloCluster(*cluster);
386                 }
387                 // cells
388                 AliAODCaloCells* cellsA = aodH->GetEventToMerge()->GetEMCALCells();
389                 Int_t ncells  = cellsA->GetNumberOfCells();
390                 nc = fgAODEmcalCells->GetNumberOfCells();
391                 
392                 for (Int_t i  = 0; i < ncells; i++) {
393                     Int_t cn  = cellsA->GetCellNumber(i);
394                     Int_t pos = fgAODEmcalCells->GetCellPosition(cn);
395                     if (pos >= 0) {
396                         Double_t amp = cellsA->GetAmplitude(i) + fgAODEmcalCells->GetAmplitude(pos);
397                         fgAODEmcalCells->SetCell(pos, cn, amp);
398                     } else {
399                         Double_t amp = cellsA->GetAmplitude(i);
400                         fgAODEmcalCells->SetCell(nc++, cn, amp);
401                         fgAODEmcalCells->Sort();
402                     }
403                 }
404                 
405                 
406             } // merging
407             
408             handler->SetAODIsReplicated();
409         }
410     }
411
412 // Call the user analysis    
413     UserExec(option);
414     // Added protection in case the derived task is not an AOD producer.
415     AliAnalysisDataSlot *out0 = GetOutputSlot(0);
416     if (out0 && out0->IsConnected()) PostData(0, fTreeA);    
417 }
418
419 const char* AliAnalysisTaskSE::CurrentFileName()
420 {
421 // Returns the current file name    
422     if( fInputHandler )
423       return fInputHandler->GetTree()->GetCurrentFile()->GetName();
424     else if( fMCEvent )
425       return ((AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler()))->TreeK()->GetCurrentFile()->GetName();
426     else return "";
427 }
428
429 void AliAnalysisTaskSE::AddAODBranch(const char* cname, void* addobj, const char *fname)
430 {
431     // Add a new branch to the aod tree
432     AliAODHandler* handler = (AliAODHandler*) 
433         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
434     if (handler) {
435         handler->AddBranch(cname, addobj, fname);
436     }
437 }
438
439 Bool_t AliAnalysisTaskSE::IsStandardAOD() const
440 {
441 // Check if the output AOD handler is configured for standard or delta AOD.
442 // Users should first check that AODEvent() returns non-null.
443     AliAODHandler* handler = (AliAODHandler*) 
444          ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
445     if (!handler) {
446        Error("IsStandardAOD", "No AOD handler. Please use AODEvent() to check this first");
447        return kTRUE;
448     }
449     return handler->IsStandard();   
450 }
451
452 Bool_t AliAnalysisTaskSE::Notify()
453 {
454     // Called for every change of input file
455     if (fInputHandler) {
456         if (fInputHandler->GetTree()) 
457             fInputHandler->GetTree()->GetEntry(0);
458     }
459     
460     if (InputEvent()->GetRunNumber() != fCurrentRunNumber) {
461         fCurrentRunNumber = InputEvent()->GetRunNumber();
462         NotifyRun();
463     }
464     return (UserNotify());
465 }
466
467