]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/EventMixing/AliAnalysisTaskMixInfo.cxx
Event mixing is now working propertly with buffersize > 1 + Mixing info fixed
[u/mrichter/AliRoot.git] / ANALYSIS / EventMixing / AliAnalysisTaskMixInfo.cxx
1 //
2 // Class AliAnalysisTaskMixInfo
3 //
4 // AliAnalysisTaskMixInfo is task
5 // for mixing info
6 //
7 // Mixing info can be enabled by setting one of following lines in UserCreateOutput() in your task
8 //       // prints mixing info
9 //       AliLog::SetClassDebugLevel("AliAnalysisTaskMixInfo", AliLog::kDebug);
10 //       // prints mixing info + event info for both (main and mixed) events
11 //       AliLog::SetClassDebugLevel("AliAnalysisTaskMixInfo", AliLog::kDebug+1);
12 //
13 // authors:
14 //          Martin Vala (martin.vala@cern.ch)
15 //
16
17 #include <TList.h>
18 #include <TObjString.h>
19
20 #include "AliAnalysisManager.h"
21
22 #include "AliMixInputEventHandler.h"
23 #include "AliAnalysisTaskMixInfo.h"
24 #include "AliMixInfo.h"
25 #include "AliMixEventPool.h"
26 #include "AliMixEventCutObj.h"
27
28
29 ClassImp(AliAnalysisTaskMixInfo)
30
31 //________________________________________________________________________
32 AliAnalysisTaskMixInfo::AliAnalysisTaskMixInfo(const char *name)
33    : AliAnalysisTaskSE(name),
34      fInputEHMain(0),
35      fInputEHMix(0),
36      fOutputList(0),
37      fMixInfo(0),
38      fCurrentEntryTmp(-1),
39      fLogType(AliLog::kInfo),
40      fLogClassesString()
41 {
42    //
43    // Constructor
44    //
45    DefineOutput(1, TList::Class());
46 }
47
48 //________________________________________________________________________
49 AliAnalysisTaskMixInfo::~AliAnalysisTaskMixInfo()
50 {
51    //
52    // Destructor
53    //
54    if (fOutputList && !AliAnalysisManager::GetAnalysisManager()->IsProofMode()) delete fOutputList;
55 }
56
57 //________________________________________________________________________
58 void AliAnalysisTaskMixInfo::UserCreateOutputObjects()
59 {
60    // Create histograms
61    // Called once
62
63    SetDebugForAllClasses();
64    fOutputList = new TList();
65    fOutputList->SetOwner(kTRUE);
66
67    // sets all Inuput Handler pointers
68    InitInputHandlers();
69
70    // inits mix info
71    InitMixInfo();
72
73    if (fInputEHMix) {
74       AliMixEventPool *evPool = fInputEHMix->GetEventPool();
75       if (evPool) {
76          evPool->SetBufferSize(fInputEHMix->BufferSize());
77          evPool->SetMixNumber(fInputEHMix->MixNumber());
78          fMixInfo->SetEventPool(evPool);
79       }
80    }
81    if (fMixInfo) fOutputList->Add(fMixInfo);
82
83    // Post output data.
84    PostData(1, fOutputList);
85 }
86
87 //________________________________________________________________________
88 void AliAnalysisTaskMixInfo::UserExec(Option_t *)
89 {
90    // Main loop
91    // Called for each event
92    if (fMixInfo && fInputEHMix) {
93       if (fInputEHMix->BufferSize() > 1) {
94          if (fInputEHMix->NumberMixedTimes() >= fInputEHMix->BufferSize())
95             fMixInfo->FillHistogram(AliMixInfo::kMainEvents, fInputEHMix->CurrentBinIndex());
96       } else {
97          if ((!fInputEHMix->IsMixingIfNotEnoughEvents())) {
98             if (fInputEHMix->NumberMixed() == fInputEHMix->MixNumber())
99                // add main entry only when there was enough mixed events mixed
100                fMixInfo->FillHistogram(AliMixInfo::kMainEvents, fInputEHMix->CurrentBinIndex());
101          } else {
102             fMixInfo->FillHistogram(AliMixInfo::kMainEvents, fInputEHMix->CurrentBinIndex());
103          }
104       }
105       AliDebug(AliLog::kDebug, Form("Main %lld %d [%lld,%lld] %d", fInputEHMix->CurrentEntry(), fInputEHMix->CurrentBinIndex(), fInputEHMix->CurrentEntryMain(), fInputEHMix->CurrentEntryMix(), fInputEHMix->NumberMixed()));
106    }
107    // Post output data.
108    PostData(1, fOutputList);
109 }
110
111 //________________________________________________________________________
112 void AliAnalysisTaskMixInfo::UserExecMix(Option_t *)
113 {
114    // UserExecMix
115
116    if (!fInputEHMix) return;
117
118    // fills bin index (even when it is -1, so we know out of range combinations)
119    if (fMixInfo) {
120       if (fInputEHMix->CurrentBinIndex()==-1) {
121          fMixInfo->FillHistogram(AliMixInfo::kMixedEvents, fInputEHMix->CurrentBinIndex());
122       } else {
123          for(Int_t iBuff=0; iBuff<fInputEHMix->BufferSize(); iBuff++) {
124             fMixInfo->FillHistogram(AliMixInfo::kMixedEvents, fInputEHMix->CurrentBinIndex());
125          }
126       }
127    }
128
129    // just test
130    if (fInputEHMix->CurrentEntryMix() < 0) {
131       AliError("Mix entry is -1 and it should not happen !!!!!");
132       return ;
133    }
134    AliDebug(AliLog::kDebug, Form("Mixing %lld %d [%lld,%lld] %d", fInputEHMix->CurrentEntry(), fInputEHMix->CurrentBinIndex(), fInputEHMix->CurrentEntryMain(), fInputEHMix->CurrentEntryMix(), fInputEHMix->NumberMixed()));
135    if (AliLog::GetDebugLevel("", IsA()->GetName()) > AliLog::kDebug) PrintEventInfo();
136    // Post output data.
137    PostData(1, fOutputList);
138 }
139
140 //________________________________________________________________________
141 void AliAnalysisTaskMixInfo::FinishTaskOutput()
142 {
143    // FinishTaskOutput
144    if (fMixInfo) fMixInfo->Print();
145 }
146
147
148 //________________________________________________________________________
149 void AliAnalysisTaskMixInfo::Terminate(Option_t *)
150 {
151    // Draw result to the screen
152    // Called once at the end of the query
153    fOutputList = dynamic_cast<TList *>(GetOutputData(1));
154    if (!fOutputList) {
155       AliError("fOutputList not available");
156       return;
157    }
158    fOutputList->Print();
159    fMixInfo = (AliMixInfo *) fOutputList->FindObject("mixInfo");
160    if (fMixInfo) {
161       fMixInfo->Draw("HIST");
162       AliMixEventPool *evPool = (AliMixEventPool *) fMixInfo->GetEventPool("mixEventPool");
163       if (evPool) evPool->Print();
164    }
165 }
166
167 //_____________________________________________________________________________
168 void AliAnalysisTaskMixInfo::SetLogType(AliLog::EType_t type, TString allClasses)
169 {
170    //
171    // Set Log level for this and other classes (list of their names)
172    //
173    AliDebug(AliLog::kDebug + 10, "<-");
174    fLogType = type;
175    fLogClassesString = allClasses;
176    SetDebugForAllClasses();
177    AliDebug(AliLog::kDebug + 10, "->");
178 }
179
180 //_____________________________________________________________________________
181 void AliAnalysisTaskMixInfo::SetDebugForAllClasses()
182 {
183    //
184    // Set debug level for all classes for which it is required
185    //
186    AliDebug(AliLog::kDebug + 10, "<-");
187    TObjArray *array = fLogClassesString.Tokenize(":");
188    TObjString *str;
189    TString strr;
190    for (Int_t i = 0; i < array->GetEntriesFast(); i++) {
191       str = (TObjString *) array->At(i);
192       strr = str->GetString();
193       AliLog::SetClassDebugLevel(strr.Data(), fLogType);
194       AliDebug(AliLog::kDebug + 5, Form("Setting Debug level %d to %s ...", (Int_t)fLogType - AliLog::kDebug, strr.Data()));
195    }
196    AliDebug(AliLog::kDebug + 10, "->");
197 }
198
199 //_____________________________________________________________________________
200 void AliAnalysisTaskMixInfo::InitMixInfo()
201 {
202    //
203    // Init mixing info
204    //
205    if (fInputEHMix) {
206       fMixInfo = new AliMixInfo("mixInfo", "Mix title");
207       AliMixEventPool *evPool = fInputEHMix->GetEventPool();
208       if (!evPool) {
209          //             TList *list = new TList;
210          if (fMixInfo) fMixInfo->CreateHistogram(AliMixInfo::kMainEvents, 1, 1, 2);
211          if (fMixInfo) fMixInfo->CreateHistogram(AliMixInfo::kMixedEvents, 1, 1, 2);
212       } else {
213          if (evPool->NeedInit()) evPool->Init();
214          Int_t num = evPool->GetListOfEntryLists()->GetEntriesFast();
215          if (fMixInfo) fMixInfo->CreateHistogram(AliMixInfo::kMainEvents, num, 1, num + 1);
216          if (fMixInfo) fMixInfo->CreateHistogram(AliMixInfo::kMixedEvents, num, 1, num + 1);
217       }
218    }
219 }
220
221 //_____________________________________________________________________________
222 void AliAnalysisTaskMixInfo::PrintEventInfo()
223 {
224    //
225    // Prints event info for all event mxing cuts
226    //
227    if (fInputEHMix) {
228       TObjArrayIter next(fInputEHMix->GetEventPool()->GetListOfEventCuts());
229       AliMixEventCutObj *cut;
230       AliInputEventHandler *ihMain = fInputEHMain->GetFirstInputEventHandler();
231       AliMultiInputEventHandler *ihMultiMix = fInputEHMix->GetFirstMultiInputHandler();
232       AliInputEventHandler *ihMix = 0;
233       if (ihMultiMix) ihMix = ihMultiMix->GetFirstInputEventHandler();
234       if (!ihMix) return;
235       while ((cut = (AliMixEventCutObj *) next())) {
236          cut->PrintValues(ihMain->GetEvent(), ihMix->GetEvent());
237       }
238    }
239 }
240
241 //_____________________________________________________________________________
242 void AliAnalysisTaskMixInfo::InitInputHandlers()
243 {
244    //
245    // Sets needed input handlers
246    //
247    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
248    fInputEHMain = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
249    if (fInputEHMain) {
250       fInputEHMix = dynamic_cast<AliMixInputEventHandler *>(fInputEHMain->GetFirstMultiInputHandler());
251    }
252 }