Port of new Event Mixing implementation
[u/mrichter/AliRoot.git] / ANALYSIS / EventMixing / AliAnalysisTaskMixInfo.cxx
1 //
2 // Class AliAnalysisTaskMixInfo
3 //
4 // AliAnalysisTaskMixInfo is task
5 // for mixing info
6 //
7 // authors:
8 //          Martin Vala (martin.vala@cern.ch)
9 //
10
11 #include <TList.h>
12
13 #include "AliAnalysisManager.h"
14
15 #include "AliMixInputEventHandler.h"
16 #include "AliAnalysisTaskMixInfo.h"
17 #include "AliMixInfo.h"
18 #include "AliMixEventPool.h"
19
20 ClassImp(AliAnalysisTaskMixInfo)
21
22 //________________________________________________________________________
23 AliAnalysisTaskMixInfo::AliAnalysisTaskMixInfo(const char *name)
24    : AliAnalysisTaskSE(name),
25      fOutputList(0),
26      fMixInfo(0),
27      fCurrentEntryTmp(-1),
28      fLogType(AliLog::kInfo),
29      fLogClassesString()
30 {
31    //
32    // Constructor
33    //
34    DefineOutput(1, TList::Class());
35 }
36
37 //________________________________________________________________________
38 AliAnalysisTaskMixInfo::~AliAnalysisTaskMixInfo()
39 {
40    //
41    // Destructor
42    //
43    if (fOutputList && !AliAnalysisManager::GetAnalysisManager()->IsProofMode()) delete fOutputList;
44 }
45
46 //________________________________________________________________________
47 void AliAnalysisTaskMixInfo::UserCreateOutputObjects()
48 {
49    // Create histograms
50    // Called once
51
52    SetDebugForAllClasses();
53    fOutputList = new TList();
54    fOutputList->SetOwner(kTRUE);
55    if (!fMixInfo) InitMixInfo();
56
57    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
58    AliMultiInputEventHandler *inEvHMain = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
59    if (inEvHMain) {
60       AliMixInputEventHandler *mixEH = dynamic_cast<AliMixInputEventHandler *>(inEvHMain->GetFirstMultiInputHandler());
61       if (mixEH) {
62          AliMixEventPool *evPool = mixEH->GetEventPool();
63          if (evPool) {
64             evPool->SetBufferSize(mixEH->BufferSize());
65             evPool->SetMixNumber(mixEH->MixNumber());
66             fMixInfo->SetEventPool(evPool);
67          }
68       }
69    }
70
71    if (fMixInfo) fOutputList->Add(fMixInfo);
72
73    // Post output data.
74    PostData(1, fOutputList);
75 }
76
77 //________________________________________________________________________
78 void AliAnalysisTaskMixInfo::UserExec(Option_t *)
79 {
80    // Main loop
81    // Called for each event
82 //     if (fCurrentEntry != fCurrentEntryTmp) {
83 //       fCurrentEntryTmp = fCurrentEntry;
84    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
85    AliMultiInputEventHandler *inEvHMain = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
86    if (inEvHMain) {
87       AliMixInputEventHandler *mixEH = dynamic_cast<AliMixInputEventHandler *>(inEvHMain->GetFirstMultiInputHandler());
88       if (fMixInfo && mixEH) {
89          if (mixEH->BufferSize() > 1) {
90             if (mixEH->NumberMixedTimes() >= mixEH->BufferSize())
91                fMixInfo->FillHistogram(AliMixInfo::kMainEvents, mixEH->CurrentBinIndex());
92          } else {
93             fMixInfo->FillHistogram(AliMixInfo::kMainEvents, mixEH->CurrentBinIndex());
94          }
95       }
96       if (mixEH->CurrentEntryMix() >= 0)
97          AliDebug(AliLog::kDebug + 1, Form("Main %lld %d [%lld,%lld] %d", mixEH->CurrentEntry(), mixEH->NumberMixed(), mixEH->CurrentEntryMain(), mixEH->CurrentEntryMix(), mixEH->NumberMixed()));
98
99    }
100    // Post output data.
101    PostData(1, fOutputList);
102 }
103
104 //________________________________________________________________________
105 void AliAnalysisTaskMixInfo::UserExecMix(Option_t *)
106 {
107    // UserExecMix
108    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
109    AliMultiInputEventHandler *inEvHMain = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
110    if (inEvHMain) {
111       AliMixInputEventHandler *mixEH = dynamic_cast<AliMixInputEventHandler *>(inEvHMain->GetFirstMultiInputHandler());
112
113       if (fMixInfo) fMixInfo->FillHistogram(AliMixInfo::kMixedEvents, mixEH->CurrentBinIndex());
114       if (!mixEH || mixEH->CurrentEntryMix() < 0) {
115          AliError("Mix entry is -1 and it should not happen !!!!!");
116          return ;
117       }
118       if (mixEH)
119          AliDebug(AliLog::kDebug, Form("Mixing %lld %d [%lld,%lld] %d", mixEH->CurrentEntry(), mixEH->NumberMixed(), mixEH->CurrentEntryMain(), mixEH->CurrentEntryMix(), mixEH->CurrentBinIndex()));
120    }
121    // Post output data.
122    PostData(1, fOutputList);
123 }
124
125 //________________________________________________________________________
126 void AliAnalysisTaskMixInfo::FinishTaskOutput()
127 {
128    // FinishTaskOutput
129    if (fMixInfo) fMixInfo->Print();
130 }
131
132
133 //________________________________________________________________________
134 void AliAnalysisTaskMixInfo::Terminate(Option_t *)
135 {
136    // Draw result to the screen
137    // Called once at the end of the query
138    fOutputList = dynamic_cast<TList *>(GetOutputData(1));
139    if (!fOutputList) {
140       AliError("fOutputList not available");
141       return;
142    }
143    fOutputList->Print();
144    fMixInfo = (AliMixInfo *) fOutputList->FindObject("mixInfo");
145    if (fMixInfo) {
146       fMixInfo->Draw("HIST");
147       AliMixEventPool *evPool = (AliMixEventPool *) fMixInfo->GetEventPool("mixEventPool");
148       if (evPool) evPool->Print();
149    }
150 }
151
152 //_____________________________________________________________________________
153 void AliAnalysisTaskMixInfo::SetLogType(AliLog::EType_t type, TString allClasses)
154 {
155    //
156    // Set Log level for this and other classes (list of their names)
157    //
158    AliDebug(AliLog::kDebug + 10, "<-");
159    fLogType = type;
160    fLogClassesString = allClasses;
161    SetDebugForAllClasses();
162    AliDebug(AliLog::kDebug + 10, "->");
163 }
164
165 //_____________________________________________________________________________
166 void AliAnalysisTaskMixInfo::SetDebugForAllClasses()
167 {
168    //
169    // Set debug level for all classes for which it is required
170    //
171    AliDebug(AliLog::kDebug + 10, "<-");
172    TObjArray *array = fLogClassesString.Tokenize(":");
173    TObjString *str;
174    TString strr;
175    for (Int_t i = 0; i < array->GetEntriesFast(); i++) {
176       str = (TObjString *) array->At(i);
177       strr = str->GetString();
178       AliLog::SetClassDebugLevel(strr.Data(), fLogType);
179       AliDebug(AliLog::kDebug + 5, Form("Setting Debug level %d to %s ...", (Int_t)fLogType - AliLog::kDebug, strr.Data()));
180    }
181    AliDebug(AliLog::kDebug + 10, "->");
182 }
183
184
185 void AliAnalysisTaskMixInfo::InitMixInfo()
186 {
187    //
188    // Init mixing info
189    //
190    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
191    AliMultiInputEventHandler *inEvHMain = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
192    if (inEvHMain) {
193       //         AliMixEventInputHandler *mixEH = dynamic_cast<AliMixEventInputHandler *>(inEvHMain->MixingHandler());
194       AliMixInputEventHandler *mixEH = dynamic_cast<AliMixInputEventHandler *>(inEvHMain->GetFirstMultiInputHandler());
195       if (mixEH) {
196          fMixInfo = new AliMixInfo("mixInfo", "Mix title");
197 //             if(fMixInfo) fMixInfo->SetOutputList(fOutputList);
198          AliMixEventPool *evPool = mixEH->GetEventPool();
199          if (!evPool) {
200             //             TList *list = new TList;
201             if (fMixInfo) fMixInfo->CreateHistogram(AliMixInfo::kMainEvents, 1, 1, 2);
202             if (fMixInfo) fMixInfo->CreateHistogram(AliMixInfo::kMixedEvents, 1, 1, 2);
203          } else {
204             if (evPool->NeedInit()) evPool->Init();
205             Int_t num = evPool->GetListOfEntryLists()->GetEntriesFast();
206             if (fMixInfo) fMixInfo->CreateHistogram(AliMixInfo::kMainEvents, num, 1, num + 1);
207             if (fMixInfo) fMixInfo->CreateHistogram(AliMixInfo::kMixedEvents, num, 1, num + 1);
208          }
209       } else {
210          AliError("No mixEH");
211       }
212    } else {
213       AliError("No inEvHMain");
214    }
215 }