Forgot to increase class version
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskME.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 "AliAnalysisTaskME.h"
26 #include "AliAnalysisManager.h"
27 #include "AliAODEvent.h"
28 #include "AliVEvent.h"
29 #include "AliAODHandler.h"
30 #include "AliMultiEventInputHandler.h"
31 #include "AliLog.h"
32
33
34 ClassImp(AliAnalysisTaskME)
35
36 ////////////////////////////////////////////////////////////////////////
37
38 AliAnalysisTaskME::AliAnalysisTaskME():
39     AliAnalysisTask(),
40     fDebug(0),
41     fEntry(0),
42     fFreshBufferOnly(kFALSE),
43     fInputHandler(0x0),
44     fOutputAOD(0x0),
45     fTreeA(0x0)
46 {
47   // Default constructor
48 }
49
50 AliAnalysisTaskME::AliAnalysisTaskME(const char* name):
51     AliAnalysisTask(name, "AnalysisTaskME"),
52     fDebug(0),
53     fEntry(0),
54     fFreshBufferOnly(kFALSE),
55     fInputHandler(0x0),
56     fOutputAOD(0x0),
57     fTreeA(0x0)
58 {
59   // Default constructor
60     DefineInput (0, TChain::Class());
61     DefineOutput(0,  TTree::Class());
62 }
63
64 AliAnalysisTaskME::AliAnalysisTaskME(const AliAnalysisTaskME& obj):
65     AliAnalysisTask(obj),
66     fDebug(0),
67     fEntry(0),
68     fFreshBufferOnly(kFALSE),
69     fInputHandler(0x0),
70     fOutputAOD(0x0),
71     fTreeA(0x0)
72 {
73 // Copy constructor
74     fDebug        = obj.fDebug;
75     fEntry        = obj.fEntry;
76     fInputHandler = obj.fInputHandler;
77     fOutputAOD    = obj.fOutputAOD;
78     fTreeA        = obj.fTreeA;    
79 }
80
81
82 AliAnalysisTaskME& AliAnalysisTaskME::operator=(const AliAnalysisTaskME& other)
83 {
84 // Assignment
85     AliAnalysisTask::operator=(other);
86     fDebug           = other.fDebug;
87     fEntry           = other.fEntry;
88     fFreshBufferOnly = other.fFreshBufferOnly;
89     fInputHandler    = other.fInputHandler;
90     fOutputAOD       = other.fOutputAOD;
91     fTreeA           = other.fTreeA;    
92     return *this;
93 }
94
95
96 void AliAnalysisTaskME::ConnectInputData(Option_t* /*option*/)
97 {
98 // Connect the input data
99     if (fDebug > 1) printf("AnalysisTaskME::ConnectInputData() \n");
100 //
101 //  Multi AOD
102 //
103     fInputHandler = dynamic_cast<AliMultiEventInputHandler*> 
104         ((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
105     if (fInputHandler == 0) {
106         AliFatal("Event Handler has to be MultiEventInputHandler !");
107     } else {
108         // Check that we have an event pool
109         if (!fInputHandler->GetEventPool()) {
110             fInputHandler->SetEventPool(AliAnalysisManager::GetAnalysisManager()->GetEventPool());
111             if (!fInputHandler->GetEventPool()) 
112                 AliFatal("MultiEventInputHandler has no EventPool connected !");
113         }
114     }
115 }
116
117 void AliAnalysisTaskME::CreateOutputObjects()
118 {
119 // Create the output container
120 //
121 //  Default AOD
122     if (fDebug > 1) printf("AnalysisTaskME::CreateOutPutData() \n");
123
124     AliAODHandler* handler = (AliAODHandler*) 
125         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
126     
127     if (handler) {
128         fOutputAOD   = handler->GetAOD();
129         fTreeA = handler->GetTree();
130     } else {
131         AliWarning("No AOD Event Handler connected.") ; 
132     }
133     UserCreateOutputObjects();
134 }
135
136 void AliAnalysisTaskME::Exec(Option_t* option)
137 {
138 //
139 // Exec analysis of one event
140
141     if (fDebug > 1) AliInfo("AliAnalysisTaskME::Exec() \n");
142     if( fInputHandler ) 
143        fEntry = fInputHandler->GetReadEntry();
144     if ( !((Entry()-1)%100) && fDebug > 0) 
145          AliInfo(Form("%s ----> Processing event # %lld", CurrentFileName(), Entry()));
146
147     AliAODHandler* outputHandler = (AliAODHandler*) 
148         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());         
149 // Call the user analysis    
150     if (fInputHandler->IsBufferReady()) {
151         if ((fFreshBufferOnly && fInputHandler->IsFreshBuffer()) || !fFreshBufferOnly)
152         {
153             if (outputHandler) outputHandler->SetFillAOD(kTRUE);
154             UserExec(option);
155             PostData(0, fTreeA);
156         } else {
157             if (outputHandler) outputHandler->SetFillAOD(kFALSE);
158         }
159     } else {
160         AliInfo(Form("Waiting for buffer to be ready !\n"));
161     }
162 }
163
164 const char* AliAnalysisTaskME::CurrentFileName()
165 {
166 // Returns the current file name    
167     if(fInputHandler )
168         return fInputHandler->GetTree()->GetCurrentFile()->GetName();
169     else return "";
170 }
171
172 void AliAnalysisTaskME::AddAODBranch(const char* cname, void* addobj, const char *fname)
173 {
174     // Add a new branch to the aod tree
175     AliAODHandler* handler = (AliAODHandler*) 
176         ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
177     if (handler) {
178         handler->AddBranch(cname, addobj, fname);
179     }
180 }
181
182 AliVEvent*  AliAnalysisTaskME::GetEvent(Int_t iev)
183 {
184     // Get an event from the input handler
185     return (fInputHandler->GetEvent(iev));
186 }
187