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