]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/RESONANCES/AliRsnVAnalysisTask.cxx
macros:
[u/mrichter/AliRoot.git] / PWG2 / RESONANCES / AliRsnVAnalysisTask.cxx
1 //
2 // Class AliRsnVAnalysisTask
3 //
4 // Virtual Class derivated from AliAnalysisTaskSE which will be base class
5 // for all RSN SE tasks
6 //
7 // authors: Martin Vala (martin.vala@cern.ch)
8 //          Alberto Pulvirenti (alberto.pulvirenti@ct.infn.it)
9 //
10
11 #include <TH1.h>
12
13 #include "AliESDEvent.h"
14 #include "AliMCEvent.h"
15 #include "AliAODEvent.h"
16 #include "AliAnalysisManager.h"
17 #include "AliMCEventHandler.h"
18 #include "AliMultiInputEventHandler.h"
19 #include "AliMixInputEventHandler.h"
20
21
22 #include "AliRsnEvent.h"
23 #include "AliRsnTarget.h"
24
25 #include "AliRsnVAnalysisTask.h"
26
27 ClassImp(AliRsnVAnalysisTask)
28
29 //_____________________________________________________________________________
30 AliRsnVAnalysisTask::AliRsnVAnalysisTask
31 (const char *name, Bool_t mcOnly) :
32    AliAnalysisTaskSE(name),
33    fLogType(AliLog::kInfo),
34    fLogClassesString(""),
35    fIsMixing(kFALSE),
36    fMCOnly(mcOnly),
37    fInfoList(0x0),
38    fTaskInfo(name),
39    fMixedEH(0),
40    fUseMixingRange(kTRUE)
41 {
42 //
43 // Default constructor.
44 // Define the output slot for histograms.
45 //
46
47    DefineOutput(1, TList::Class());
48    DefineOutput(2, TList::Class());
49    
50    for (Int_t i = 0; i < 2; i++) {
51       fESDEvent[i] = 0;
52       fMCEvent[i] = 0;
53       fAODEventIn[i] = 0;
54       fAODEventOut[i] = 0;
55    }
56 }
57
58 //_____________________________________________________________________________
59 AliRsnVAnalysisTask::AliRsnVAnalysisTask(const AliRsnVAnalysisTask& copy) :
60    AliAnalysisTaskSE(copy),
61    fLogType(copy.fLogType),
62    fLogClassesString(copy.fLogClassesString),
63    fIsMixing(copy.fIsMixing),
64    fMCOnly(copy.fMCOnly),
65    fInfoList(0x0),
66    fTaskInfo(copy.fTaskInfo),
67    fMixedEH(copy.fMixedEH),
68    fUseMixingRange(copy.fUseMixingRange)
69 {
70 //
71 // Copy constructor.
72 // Defined for coding conventions compliance but never used.
73 //
74    AliDebug(AliLog::kDebug + 2, "<-");
75    
76    for (Int_t i = 0; i < 2; i++) {
77       fESDEvent[i] = copy.fESDEvent[i];
78       fMCEvent[i] = copy.fMCEvent[i];
79       fAODEventIn[i] = copy.fAODEventIn[i];
80       fAODEventOut[i] = copy.fAODEventOut[i];
81       fRsnEvent[i] = copy.fRsnEvent[i];
82    }
83    
84    AliDebug(AliLog::kDebug + 2, "->");
85 }
86
87 //_____________________________________________________________________________
88 void AliRsnVAnalysisTask::LocalInit()
89 {
90 //
91 // Local initialization.
92 // Defines the debug message level and calls the mother class LocalInit().
93 //
94
95    AliAnalysisTaskSE::LocalInit();
96    SetDebugForAllClasses();
97 }
98
99 //_____________________________________________________________________________
100 Bool_t AliRsnVAnalysisTask::UserNotify()
101 {
102 //
103 // Calls the mother class Notify()
104 //
105
106    return AliAnalysisTaskSE::UserNotify();
107 }
108
109 //_____________________________________________________________________________
110 void AliRsnVAnalysisTask::ConnectInputData(Option_t *opt)
111 {
112 //
113 // Connect input data, which consist in initializing properly
114 // the pointer to the input event, which is dynamically casted
115 // to all available types, and this allows to know its type.
116 // Calls also the mother class omonyme method.
117 //
118
119    AliAnalysisTaskSE::ConnectInputData(opt);
120
121    // get AliESDEvent and, if successful
122    // retrieve the corresponding MC if exists
123    fESDEvent[0] = dynamic_cast<AliESDEvent *>(fInputEvent);
124    if (fESDEvent[0]) {
125       fMCEvent[0] = (AliMCEvent*) MCEvent();
126       AliInfo(Form("Input event is of type ESD   (%p)", fESDEvent[0]));
127       if (fMCEvent[0]) AliInfo(Form("Input has an associated MC (%p)", fMCEvent[0]));
128    }
129
130    // get AliAODEvent from input and, if successful
131    // it will contain both the reconstructed and MC informations
132    fAODEventIn[0] = dynamic_cast<AliAODEvent *>(fInputEvent);
133    if (fAODEventIn[0]) {
134       AliInfo(Form("Input event if of type native AOD (%p)", fAODEventIn[0]));
135    }
136
137    // get AliAODEvent from output of previous task
138    fAODEventOut[0] = dynamic_cast<AliAODEvent *>(AODEvent());
139    if (fAODEventOut[0]) {
140       AliInfo(Form("Input event if of type produced AOD from previous step (%p)", fAODEventOut[0]));
141    }
142 }
143
144 //_____________________________________________________________________________
145 void AliRsnVAnalysisTask::UserCreateOutputObjects()
146 {
147 //
148 // Creates and links to task all output objects.
149 // Does explicitly the initialization for the event info class,
150 // and then calls the customized function which must be overloaded
151 // in the applications of this base class.
152 //
153
154    SetDebugForAllClasses();
155
156    // set event info outputs
157    fInfoList = new TList();
158    fInfoList->SetOwner();
159    fTaskInfo.GenerateInfoList(fInfoList);
160
161    // create customized outputs
162    RsnUserCreateOutputObjects();
163
164    PostData(1, fInfoList);
165 }
166
167 //_____________________________________________________________________________
168 void AliRsnVAnalysisTask::UserExec(Option_t* opt)
169 {
170 //
171 // Prepares for execution, setting the correct pointers of the
172 // RSN package event interface, which will point to the not NULL
173 // objects obtained from dynamic-casts called in ConnectInputData().
174 //
175
176    if (!IsMixing()) {
177
178       if (fMCOnly && fMCEvent[0]) {
179          fRsnEvent[0].SetRef(fMCEvent[0]);
180          fRsnEvent[0].SetRefMC(fMCEvent[0]);
181       } else if (fESDEvent[0]) {
182          fRsnEvent[0].SetRef(fESDEvent[0]);
183          fRsnEvent[0].SetRefMC(fMCEvent[0]);
184       } else if (fAODEventOut[0]) {
185          fRsnEvent[0].SetRef(fAODEventOut[0]);
186          fRsnEvent[0].SetRefMC(fAODEventOut[0]);
187       } else if (fAODEventIn[0]) {
188          fRsnEvent[0].SetRef(fAODEventIn[0]);
189          fRsnEvent[0].SetRefMC(fAODEventIn[0]);
190       } else {
191          AliError("Unknown input event format. Skipping");
192          return;
193       }
194
195       // call event preprocessing...
196       Bool_t preCheck = RsnEventProcess();
197       // ...then fill the information object and print informations...
198       fTaskInfo.FillInfo(&fRsnEvent[0]);
199       fTaskInfo.PrintInfo(fTaskInfo.GetNumerOfEventsProcessed());
200       // ...and return if event did not pass selections
201       if (!preCheck) {
202          AliDebug(AliLog::kDebug, "Event preprocessing has failed. Skipping event");
203          return;
204       }
205       
206       // call customized implementation for execution
207       RsnUserExec(opt);
208    }
209    
210    // post outputs for the info object
211    // (eventually others are done in the derived classes)
212    PostData(1, fInfoList);
213 }
214
215 void AliRsnVAnalysisTask::UserExecMix(Option_t* option)
216 {
217    AliDebug(AliLog::kDebug + 2, "<-");
218
219
220    if (!IsMixing()) return;
221
222    SetupMixingEvents();
223
224    if (!fMixedEH) return;
225
226    if (fMCOnly && fMCEvent[0]) {
227       fRsnEvent[0].SetRef(fMCEvent[0]);
228       fRsnEvent[0].SetRefMC(fMCEvent[0]);
229       fRsnEvent[1].SetRef(fMCEvent[1]);
230       fRsnEvent[1].SetRefMC(fMCEvent[1]);
231    } else if (fESDEvent[0]) {
232       fRsnEvent[0].SetRef(fESDEvent[0]);
233       fRsnEvent[0].SetRefMC(fMCEvent[0]);
234       fRsnEvent[1].SetRef(fESDEvent[1]);
235       fRsnEvent[1].SetRefMC(fMCEvent[1]);
236    } else if (fAODEventOut) {
237       fRsnEvent[0].SetRef(fAODEventOut[0]);
238       fRsnEvent[0].SetRefMC(fAODEventOut[0]);
239       fRsnEvent[1].SetRef(fAODEventOut[1]);
240       fRsnEvent[1].SetRefMC(fAODEventOut[1]);
241    } else if (fAODEventIn) {
242       fRsnEvent[0].SetRef(fAODEventIn[0]);
243       fRsnEvent[0].SetRefMC(fAODEventIn[0]);
244       fRsnEvent[1].SetRef(fAODEventIn[1]);
245       fRsnEvent[1].SetRefMC(fAODEventIn[1]);
246    } else {
247       AliError("NO ESD or AOD object!!! Skipping ...");
248       return;
249    }
250
251    // call event preprocessing...
252    Bool_t preCheck = RsnEventProcess();
253    // ...then fill the information object and print informations...
254    fTaskInfo.FillInfo(&fRsnEvent[0]);
255    fTaskInfo.PrintInfo(fTaskInfo.GetNumerOfEventsProcessed());
256    // ...and return if event did not pass selections
257    if (!preCheck) {
258       AliDebug(AliLog::kDebug, "Event preprocessing has failed. Skipping event");
259       return;
260    }
261
262    RsnUserExecMix(option);
263    AliDebug(AliLog::kDebug + 2, "->");
264 }
265
266
267 //_____________________________________________________________________________
268 void AliRsnVAnalysisTask::Terminate(Option_t* opt)
269 {
270 //
271 // Termination routines.
272 // Stores all histograms (after checking they exist)
273 // and includes to the TList all task informations.
274 //
275
276    AliAnalysisTask::Terminate();
277
278    TList* list  = dynamic_cast<TList*>(GetOutputData(1));
279    if (!list) {
280       AliError(Form("At end of analysis, fOutList is %p", list));
281       return;
282    }
283
284    RsnTerminate(opt);
285
286    TH1I *hEventInfo = (TH1I*) list->FindObject(fTaskInfo.GetEventHistogramName());
287    if (!hEventInfo) {
288       AliError(Form("hEventInfo is %p", hEventInfo));
289       return;
290    }
291    AliInfo(Form("=== %s ==================", GetName()));
292    AliInfo(Form("Number Of Events Processed : %10lld", (Long64_t)hEventInfo->Integral()));
293    AliInfo(Form("Number Of Events Accepted  : %10lld", (Long64_t)hEventInfo->GetBinContent(2)));
294    AliInfo(Form("Number Of Events Skipped   : %10lld", (Long64_t)hEventInfo->GetBinContent(1)));
295    AliInfo(Form("=== end %s ==============", GetName()));
296
297    AliDebug(AliLog::kDebug + 2, "->");
298 }
299
300 //_____________________________________________________________________________
301 void AliRsnVAnalysisTask::RsnUserCreateOutputObjects()
302 {
303 //
304 // Define here all instructions to create output objects.
305 // This method will be called inside the "UserCreateOutputObjects"
306 // in the used task.
307 //
308
309    AliWarning("Implement this in derived classes");
310 }
311
312 //_____________________________________________________________________________
313 void AliRsnVAnalysisTask::RsnUserExec(Option_t*)
314 {
315 //
316 //
317 //
318
319    AliWarning("Implement this in derived classes");
320 }
321
322 void AliRsnVAnalysisTask::RsnUserExecMix(Option_t*)
323 {
324    //
325    //
326    //
327
328    AliWarning("Implement this in derived classes");
329 }
330
331 //_____________________________________________________________________________
332 void AliRsnVAnalysisTask::RsnTerminate(Option_t*)
333 {
334 //
335 // Overload this to add additional termination operations
336 //
337
338    AliWarning("Implement this in derived classes");
339 }
340
341 //_____________________________________________________________________________
342 Bool_t AliRsnVAnalysisTask::RsnEventProcess()
343 {
344 //
345 // Performs some pre-processing of current event,
346 // which is useful for all the operations which
347 // need to be done only once for each event.
348 //
349
350    // if not using mixing cuts return kTRUE
351    if (!IsMixing() || !IsUsingMixingRange()) return kTRUE;
352
353    // cut if event was in range of mixing cuts
354    AliVEventHandler *inh = AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler();
355    if (inh->InheritsFrom(AliMultiInputEventHandler::Class())) {
356       AliMultiInputEventHandler *inEvHMain = dynamic_cast<AliMultiInputEventHandler *>(inh);
357       if (inEvHMain) {
358          fMixedEH = dynamic_cast<AliMixInputEventHandler *>(inEvHMain->GetFirstMultiInputHandler());
359          if (fMixedEH) {
360             if (fMixedEH->CurrentBinIndex() < 0) return kFALSE;
361          }
362       }
363    }
364
365    // in this case, return always a success
366    return kTRUE;
367 }
368
369 //_____________________________________________________________________________
370 void AliRsnVAnalysisTask::SetupMixingEvents()
371 {
372 //
373 // Setup the pointers to the mixed event.
374 // This requires to retrieve them from the mixed event handler
375 //
376    
377    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
378    AliMultiInputEventHandler *inEvHMain = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
379    if (inEvHMain) {
380       fMixedEH = dynamic_cast<AliMixInputEventHandler *>(inEvHMain->GetFirstMultiInputHandler());
381       if (fMixedEH) {
382          AliMultiInputEventHandler *inEvHMainTmpMix = dynamic_cast<AliMultiInputEventHandler *>(fMixedEH->InputEventHandler(0));
383          if (!inEvHMainTmpMix) return;
384          AliInputEventHandler *inEvHMixTmp = dynamic_cast<AliInputEventHandler *>(inEvHMainTmpMix->GetFirstInputEventHandler());
385          AliMCEventHandler *inEvHMCMixTmp = dynamic_cast<AliMCEventHandler *>(inEvHMainTmpMix->GetFirstMCEventHandler());
386          if (!inEvHMixTmp) return;
387          fESDEvent[1] = dynamic_cast<AliESDEvent *>(inEvHMixTmp->GetEvent());
388          if (fESDEvent[1]) AliDebug(AliLog::kDebug, Form("Input is ESD (%p) MIXED", fESDEvent[1]));
389          // getting AliAODEvent from input
390          fAODEventIn[1] = dynamic_cast<AliAODEvent *>(inEvHMixTmp->GetEvent());
391          if (fAODEventIn[1]) AliDebug(AliLog::kDebug, Form("Input is AOD (%p) MIXED", fAODEventIn[1]));
392          // getting AliAODEvent if it is output from previous task (not supported)
393          fAODEventOut[1] = 0;
394
395          if (inEvHMCMixTmp) {
396             fMCEvent[1] = inEvHMCMixTmp->MCEvent();
397             if (fMCEvent[1]) AliDebug(AliLog::kDebug, Form("Input is ESDMC (%p) MIXED", fMCEvent[1]));
398          }
399
400       }
401
402    }
403 }
404
405 //_____________________________________________________________________________
406 void AliRsnVAnalysisTask::SetDebugForAllClasses()
407 {
408 //
409 // Set debug level for all classes for which it is required
410 //
411
412    TObjArray  *array = fLogClassesString.Tokenize(":");
413    TObjString *objStr;
414    TString     str;
415    Int_t       i, n = array->GetEntriesFast();
416
417    for (i = 0; i < n; i++) {
418       objStr = (TObjString*)array->At(i);
419       str    = objStr->GetString();
420       AliLog::SetClassDebugLevel(str.Data(), fLogType);
421       AliInfo(Form("Setting Debug to %s", str.Data()));
422    }
423 }
424