]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliAnalysisTaskSE.cxx
printf removed.
[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 }
104
105
106 AliAnalysisTaskSE& AliAnalysisTaskSE::operator=(const AliAnalysisTaskSE& other)
107 {
108 // Assignment
109     AliAnalysisTask::operator=(other);
110     fDebug        = other.fDebug;
111     fEntry        = other.fEntry;
112     fInputEvent   = other.fInputEvent;
113     fInputHandler = other.fInputHandler;
114     fOutputAOD    = other.fOutputAOD;
115     fMCEvent      = other.fMCEvent;
116     fTreeA        = other.fTreeA;    
117     return *this;
118 }
119
120
121 void AliAnalysisTaskSE::ConnectInputData(Option_t* /*option*/)
122 {
123 // Connect the input data
124     if (fDebug > 1) printf("AnalysisTaskSE::ConnectInputData() \n");
125 //
126 //  ESD
127 //
128     fInputHandler = (AliInputEventHandler*) 
129          ((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
130 //
131 //  Monte Carlo
132 //
133     AliMCEventHandler*    mcH = 0;
134     mcH = (AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
135     if (mcH) {
136         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
239     if( fInputHandler ) {
240        fEntry = fInputHandler->GetReadEntry();
241     }
242     
243            
244     else if( fMCEvent )
245        fEntry = fMCEvent->Header()->GetEvent(); 
246     if ( !((Entry()-1)%100) && fDebug > 0) 
247          AliInfo(Form("%s ----> Processing event # %lld", CurrentFileName(), Entry()));
248
249     AliAODHandler* handler = (AliAODHandler*) 
250         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
251     AliAODInputHandler* aodH = dynamic_cast<AliAODInputHandler*>(fInputHandler);
252
253     if (handler && aodH) {
254         fMCEvent = aodH->MCEvent();
255         
256         if (!(handler->IsStandard()) && !(handler->AODIsReplicated())) {
257             if ((handler->NeedsHeaderReplication()) && (fgAODHeader))
258             {
259               // copy the contents by assigment
260               *fgAODHeader =  *(dynamic_cast<AliAODHeader*>(InputEvent()->GetHeader()));
261             }
262             if ((handler->NeedsTracksBranchReplication()) && (fgAODTracks))
263             {
264                 TClonesArray* tracks = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracks();
265                 new (fgAODTracks) TClonesArray(*tracks);
266             }
267             if ((handler->NeedsVerticesBranchReplication()) && (fgAODVertices))
268             {
269                 TClonesArray* vertices = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetVertices();
270                 new (fgAODVertices) TClonesArray(*vertices);
271             }
272             if ((handler->NeedsV0sBranchReplication()) && (fgAODV0s))
273             {
274                 TClonesArray* V0s = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetV0s();
275                 new (fgAODV0s) TClonesArray(*V0s);
276             }
277             if ((handler->NeedsTrackletsBranchReplication()) && (fgAODTracklets))
278             {
279               *fgAODTracklets = *(dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracklets();
280             }
281             if ((handler->NeedsPMDClustersBranchReplication()) && (fgAODPMDClusters))
282             {
283                 TClonesArray* PMDClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetPmdClusters();
284                 new (fgAODPMDClusters) TClonesArray(*PMDClusters);
285             }
286             if ((handler->NeedsJetsBranchReplication()) && (fgAODJets))
287             {
288                 TClonesArray* Jets = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetJets();
289                 new (fgAODJets) TClonesArray(*Jets);
290             }
291             if ((handler->NeedsFMDClustersBranchReplication()) && (fgAODFMDClusters))
292             {
293                 TClonesArray* FMDClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetFmdClusters();
294                 new (fgAODFMDClusters) TClonesArray(*FMDClusters);
295             }
296             if ((handler->NeedsCaloClustersBranchReplication()) && (fgAODCaloClusters))
297             {
298                 TClonesArray* CaloClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetCaloClusters();
299                 new (fgAODCaloClusters) TClonesArray(*CaloClusters);
300             }
301             //
302             handler->SetAODIsReplicated();
303             
304         }
305     }
306
307 // Call the user analysis    
308     UserExec(option);
309     // Added protection in case the derived task is not an AOD producer.
310     AliAnalysisDataSlot *out0 = GetOutputSlot(0);
311     if (out0 && out0->IsConnected()) PostData(0, fTreeA);    
312 }
313
314 const char* AliAnalysisTaskSE::CurrentFileName()
315 {
316 // Returns the current file name    
317     if( fInputHandler )
318       return fInputHandler->GetTree()->GetCurrentFile()->GetName();
319     else if( fMCEvent )
320       return ((AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler()))->TreeK()->GetCurrentFile()->GetName();
321     else return "";
322 }
323
324 void AliAnalysisTaskSE::AddAODBranch(const char* cname, void* addobj, const char *fname)
325 {
326     // Add a new branch to the aod tree
327     AliAODHandler* handler = (AliAODHandler*) 
328         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
329     if (handler) {
330         handler->AddBranch(cname, addobj, fname);
331     }
332 }
333
334 Bool_t AliAnalysisTaskSE::IsStandardAOD() const
335 {
336 // Check if the output AOD handler is configured for standard or delta AOD.
337 // Users should first check that AODEvent() returns non-null.
338     AliAODHandler* handler = (AliAODHandler*) 
339          ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
340     if (!handler) {
341        Error("IsStandardAOD", "No AOD handler. Please use AODEvent() to check this first");
342        return kTRUE;
343     }
344     return handler->IsStandard();   
345 }