- Common input and output data containers are now created automatically when attachin...
[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 "AliVEvent.h"
34 #include "AliAODHandler.h"
35 #include "AliAODInputHandler.h"
36 #include "AliMCEventHandler.h"
37 #include "AliInputEventHandler.h"
38 #include "AliMCEvent.h"
39 #include "AliStack.h"
40 #include "AliLog.h"
41
42
43 ClassImp(AliAnalysisTaskSE)
44
45 ////////////////////////////////////////////////////////////////////////
46 AliAODHeader*    AliAnalysisTaskSE::fgAODHeader        = NULL;
47 TClonesArray*    AliAnalysisTaskSE::fgAODTracks        = NULL;
48 TClonesArray*    AliAnalysisTaskSE::fgAODVertices      = NULL;
49 TClonesArray*    AliAnalysisTaskSE::fgAODV0s           = NULL;
50 TClonesArray*    AliAnalysisTaskSE::fgAODPMDClusters   = NULL;
51 TClonesArray*    AliAnalysisTaskSE::fgAODJets          = NULL;
52 TClonesArray*    AliAnalysisTaskSE::fgAODFMDClusters   = NULL;
53 TClonesArray*    AliAnalysisTaskSE::fgAODCaloClusters  = NULL;
54 AliAODTracklets* AliAnalysisTaskSE::fgAODTracklets     = NULL;
55
56
57 AliAnalysisTaskSE::AliAnalysisTaskSE():
58     AliAnalysisTask(),
59     fDebug(0),
60     fEntry(0),
61     fInputEvent(0x0),
62     fInputHandler(0x0),
63     fOutputAOD(0x0),
64     fMCEvent(0x0),
65     fTreeA(0x0)
66 {
67   // Default constructor
68 }
69
70 AliAnalysisTaskSE::AliAnalysisTaskSE(const char* name):
71     AliAnalysisTask(name, "AnalysisTaskSE"),
72     fDebug(0),
73     fEntry(0),
74     fInputEvent(0x0),
75     fInputHandler(0x0),
76     fOutputAOD(0x0),
77     fMCEvent(0x0),
78     fTreeA(0x0)
79 {
80   // Default constructor
81     DefineInput (0, TChain::Class());
82     DefineOutput(0,  TTree::Class());
83 }
84
85 AliAnalysisTaskSE::AliAnalysisTaskSE(const AliAnalysisTaskSE& obj):
86     AliAnalysisTask(obj),
87     fDebug(0),
88     fEntry(0),
89     fInputEvent(0x0),
90     fInputHandler(0x0),
91     fOutputAOD(0x0),
92     fMCEvent(0x0),
93     fTreeA(0x0)
94 {
95 // Copy constructor
96     fDebug        = obj.fDebug;
97     fEntry        = obj.fEntry;
98     fInputEvent   = obj.fInputEvent;
99     fInputHandler = obj.fInputHandler;
100     fOutputAOD    = obj.fOutputAOD;
101     fMCEvent      = obj.fMCEvent;
102     fTreeA        = obj.fTreeA;    
103     printf("Constructor (3) \n");
104 }
105
106
107 AliAnalysisTaskSE& AliAnalysisTaskSE::operator=(const AliAnalysisTaskSE& other)
108 {
109 // Assignment
110     AliAnalysisTask::operator=(other);
111     fDebug        = other.fDebug;
112     fEntry        = other.fEntry;
113     fInputEvent   = other.fInputEvent;
114     fInputHandler = other.fInputHandler;
115     fOutputAOD    = other.fOutputAOD;
116     fMCEvent      = other.fMCEvent;
117     fTreeA        = other.fTreeA;    
118     return *this;
119 }
120
121
122 void AliAnalysisTaskSE::ConnectInputData(Option_t* /*option*/)
123 {
124 // Connect the input data
125     if (fDebug > 1) printf("AnalysisTaskSE::ConnectInputData() \n");
126 //
127 //  ESD
128 //
129     fInputHandler = (AliInputEventHandler*) 
130          ((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
131 //
132 //  Monte Carlo
133 //
134     AliMCEventHandler*    mcH = 0;
135     mcH = (AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
136     if (mcH) fMCEvent = mcH->MCEvent();
137     
138     
139     if (fInputHandler) {
140          fInputEvent = fInputHandler->GetEvent();
141     } else if( fMCEvent ) {
142          AliWarning("No Input Event Handler connected, only MC Truth Event Handler") ; 
143     } else {
144          AliError("No Input Event Handler connected") ; 
145          return ; 
146     }
147 }
148
149 void AliAnalysisTaskSE::CreateOutputObjects()
150 {
151 // Create the output container
152 //
153 //  Default AOD
154     if (fDebug > 1) printf("AnalysisTaskSE::CreateOutPutData() \n");
155
156     AliAODHandler* handler = (AliAODHandler*) 
157          ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
158     
159     // Check if AOD replication has been required
160     
161     if (handler) {
162         fOutputAOD   = handler->GetAOD();
163         fTreeA = handler->GetTree();
164         if (!(handler->IsStandard())) {
165             if ((handler->NeedsHeaderReplication()) && !(fgAODHeader)) 
166                 {
167                  if (fDebug > 1) AliInfo("Replicating header");
168                  fgAODHeader = new AliAODHeader;
169                  handler->AddBranch("AliAODHeader", &fgAODHeader);
170                 }
171             if ((handler->NeedsTracksBranchReplication()) && !(fgAODTracks))      
172             {   
173                 if (fDebug > 1) AliInfo("Replicating track branch\n");
174                 fgAODTracks = new TClonesArray("AliAODTrack",500);
175                 fgAODTracks->SetName("tracks");
176                 handler->AddBranch("TClonesArray", &fgAODTracks);
177             }    
178             if ((handler->NeedsVerticesBranchReplication()) && !(fgAODVertices))
179             {
180                 if (fDebug > 1) AliInfo("Replicating vertices branch\n");
181                 fgAODVertices = new TClonesArray("AliAODVertex",500);
182                 fgAODVertices->SetName("vertices");
183                 handler->AddBranch("TClonesArray", &fgAODVertices);
184             }   
185             if ((handler->NeedsV0sBranchReplication()) && !(fgAODV0s))    
186             {   
187                 if (fDebug > 1) AliInfo("Replicating V0s branch\n");
188                 fgAODV0s = new TClonesArray("AliAODv0",500);
189                 fgAODV0s->SetName("v0s");
190                 handler->AddBranch("TClonesArray", &fgAODV0s);
191             }
192             if ((handler->NeedsTrackletsBranchReplication()) && !(fgAODTracklets))        
193             {   
194                 if (fDebug > 1) AliInfo("Replicating Tracklets branch\n");
195                 fgAODTracklets = new AliAODTracklets("tracklets","tracklets");
196                 handler->AddBranch("AliAODTracklets", &fgAODTracklets);
197             }
198             if ((handler->NeedsPMDClustersBranchReplication()) && !(fgAODPMDClusters))    
199             {   
200                 if (fDebug > 1) AliInfo("Replicating PMDClusters branch\n");
201                 fgAODPMDClusters = new TClonesArray("AliAODPmdCluster",500);
202                 fgAODPMDClusters->SetName("pmdClusters");
203                 handler->AddBranch("TClonesArray", &fgAODPMDClusters);
204             }
205             if ((handler->NeedsJetsBranchReplication()) && !(fgAODJets))          
206             {   
207                 if (fDebug > 1) AliInfo("Replicating Jets branch\n");
208                 fgAODJets = new TClonesArray("AliAODJet",500);
209                 fgAODJets->SetName("jets");
210                 handler->AddBranch("TClonesArray", &fgAODJets);
211             }
212             if ((handler->NeedsFMDClustersBranchReplication()) && !(fgAODFMDClusters))    
213             {   
214                 AliInfo("Replicating FMDClusters branch\n");
215                 fgAODFMDClusters = new TClonesArray("AliAODFmdCluster",500);
216                 fgAODFMDClusters->SetName("fmdClusters");
217                 handler->AddBranch("TClonesArray", &fgAODFMDClusters);
218             }
219             if ((handler->NeedsCaloClustersBranchReplication()) && !(fgAODCaloClusters))          
220             {   
221                 if (fDebug > 1) AliInfo("Replicating CaloClusters branch\n");
222                 fgAODCaloClusters = new TClonesArray("AliAODCaloCluster",500);
223                 fgAODCaloClusters->SetName("caloClusters");
224                 handler->AddBranch("TClonesArray", &fgAODCaloClusters);
225             }
226         }
227     } else {
228         AliWarning("No AOD Event Handler connected.") ; 
229     }
230     UserCreateOutputObjects();
231 }
232
233 void AliAnalysisTaskSE::Exec(Option_t* option)
234 {
235 //
236 // Exec analysis of one event
237     if (fDebug > 1) AliInfo("AliAnalysisTaskSE::Exec() \n");
238     if( fInputHandler ) 
239        fEntry = fInputHandler->GetReadEntry();
240     else if( fMCEvent )
241        fEntry = fMCEvent->Header()->GetEvent(); 
242     if ( !((Entry()-1)%100) && fDebug > 0) 
243          AliInfo(Form("%s ----> Processing event # %lld", CurrentFileName(), Entry()));
244
245     AliAODHandler* handler = (AliAODHandler*) 
246         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
247     AliAODInputHandler* aodH = dynamic_cast<AliAODInputHandler*>(fInputHandler);
248
249     if (handler && aodH) {
250         if (!(handler->IsStandard()) && !(handler->AODIsReplicated())) {
251             if ((handler->NeedsHeaderReplication()) && (fgAODHeader))
252             {
253               // copy the contents by assigment
254               *fgAODHeader =  *(dynamic_cast<AliAODHeader*>(InputEvent()->GetHeader()));
255             }
256             if ((handler->NeedsTracksBranchReplication()) && (fgAODTracks))
257             {
258                 TClonesArray* tracks = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracks();
259                 new (fgAODTracks) TClonesArray(*tracks);
260             }
261             if ((handler->NeedsVerticesBranchReplication()) && (fgAODVertices))
262             {
263                 TClonesArray* vertices = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetVertices();
264                 new (fgAODVertices) TClonesArray(*vertices);
265             }
266             if ((handler->NeedsV0sBranchReplication()) && (fgAODV0s))
267             {
268                 TClonesArray* V0s = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetV0s();
269                 new (fgAODV0s) TClonesArray(*V0s);
270             }
271             if ((handler->NeedsTrackletsBranchReplication()) && (fgAODTracklets))
272             {
273               *fgAODTracklets = *(dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracklets();
274             }
275             if ((handler->NeedsPMDClustersBranchReplication()) && (fgAODPMDClusters))
276             {
277                 TClonesArray* PMDClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetPmdClusters();
278                 new (fgAODPMDClusters) TClonesArray(*PMDClusters);
279             }
280             if ((handler->NeedsJetsBranchReplication()) && (fgAODJets))
281             {
282                 TClonesArray* Jets = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetJets();
283                 new (fgAODJets) TClonesArray(*Jets);
284             }
285             if ((handler->NeedsFMDClustersBranchReplication()) && (fgAODFMDClusters))
286             {
287                 TClonesArray* FMDClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetFmdClusters();
288                 new (fgAODFMDClusters) TClonesArray(*FMDClusters);
289             }
290             if ((handler->NeedsCaloClustersBranchReplication()) && (fgAODCaloClusters))
291             {
292                 TClonesArray* CaloClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetCaloClusters();
293                 new (fgAODCaloClusters) TClonesArray(*CaloClusters);
294             }
295             //
296             handler->SetAODIsReplicated();
297             
298         }
299     }
300
301 // Call the user analysis    
302     UserExec(option);
303     // Added protection in case the derived task is not an AOD producer.
304     AliAnalysisDataSlot *out0 = GetOutputSlot(0);
305     if (out0 && out0->IsConnected()) PostData(0, fTreeA);    
306 }
307
308 const char* AliAnalysisTaskSE::CurrentFileName()
309 {
310 // Returns the current file name    
311     if( fInputHandler )
312       return fInputHandler->GetTree()->GetCurrentFile()->GetName();
313     else if( fMCEvent )
314       return ((AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler()))->TreeK()->GetCurrentFile()->GetName();
315     else return "";
316 }
317
318 void AliAnalysisTaskSE::AddAODBranch(const char* cname, void* addobj)
319 {
320     // Add a new branch to the aod tree
321     AliAODHandler* handler = (AliAODHandler*) 
322         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
323     if (handler) {
324         handler->AddBranch(cname, addobj);
325     }
326 }