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