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