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