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