2 // Class AliMixEventInputHandler
4 // Mixing input handler prepare N events before UserExec
7 // Martin Vala (martin.vala@cern.ch)
12 #include <TEntryList.h>
13 #include <TChainElement.h>
17 #include "AliAnalysisManager.h"
18 #include "AliInputEventHandler.h"
20 #include "AliMixEventPool.h"
21 #include "AliMixInputEventHandler.h"
22 #include "AliMixInputHandlerInfo.h"
24 #include "AliAnalysisTaskSE.h"
26 ClassImp(AliMixInputEventHandler)
28 AliMixInputEventHandler::AliMixInputEventHandler(const Int_t size, const Int_t mixNum): AliMultiInputEventHandler(size),
30 fTreeMap(size > 0 ? size : 1),
31 fMixIntupHandlerInfoTmp(0),
36 fUseDefautProcess(kFALSE),
37 fUsePreMixEvents(kTRUE),
44 // Default constructor.
46 AliDebug(AliLog::kDebug + 10, "<-");
47 fMixTrees.SetOwner(kTRUE);
49 AliDebug(AliLog::kDebug + 10, "->");
52 //_____________________________________________________________________________
53 AliInputEventHandler *AliMixInputEventHandler::InputEventHandler(const Int_t index)
56 // Returns input handler
58 AliDebug(AliLog::kDebug + 5, Form("<-"));
59 if ((index >= 0) && (index < fBufferSize)) {
60 AliMultiInputEventHandler *eh = dynamic_cast<AliMultiInputEventHandler *>(fInputHandlers.At(index));
62 return (AliInputEventHandler *) eh;
63 // return (AliInputEventHandler *) eh->InputEventHandler(0);
65 return (AliInputEventHandler *) fInputHandlers.At(index);
67 AliDebug(AliLog::kDebug + 5, Form("->"));
70 //_____________________________________________________________________________
71 void AliMixInputEventHandler::SetInputHandlerForMixing(const AliInputEventHandler *const inHandler)
74 // Create N (fBufferSize) copies of input handler
76 AliDebug(AliLog::kDebug + 5, Form("<-"));
77 fInputHandlers.Clear();
78 AliDebug(AliLog::kDebug + 5, Form("Creating %d input event handlers ...", fBufferSize));
79 for (Int_t i = 0; i < fBufferSize; i++) {
80 AliDebug(AliLog::kDebug + 5, Form("Adding %d ...", i));
81 fInputHandlers.Add((AliInputEventHandler *) inHandler->Clone());
83 AliDebug(AliLog::kDebug + 5, Form("->"));
86 //_____________________________________________________________________________
87 Bool_t AliMixInputEventHandler::Init(TTree *tree, Option_t *opt)
90 // Init(const char*path) is called for all mix input handlers.
91 // Create event pool if needed
93 AliDebug(AliLog::kDebug + 5, Form("<- %p %s", (void *)tree, opt));
96 AliDebug(AliLog::kDebug + 5, Form("->"));
99 // clears array of input handlers
101 // create AliMixInputHandlerInfo
102 if (!fMixIntupHandlerInfoTmp) {
103 // loads first file TChain (tree)
105 fMixIntupHandlerInfoTmp = new AliMixInputHandlerInfo(tree->GetName());
108 // // adds current file
109 // fMixIntupHandlerInfoTmp->AddTreeToChain(tree->GetTree());
110 // Int_t lastIndex = fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->GetEntries();
111 // TChainElement *che = (TChainElement *)fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->At(lastIndex - 1);
112 // AliMixInputHandlerInfo *mixIHI = 0;
113 // for(Int_t i = 0; i < fInputHandlers.GetEntries(); i++) {
114 // AliDebug(AliLog::kDebug+5, Form("fInputHandlers[%d]", i));
115 // mixIHI = new AliMixInputHandlerInfo(fMixIntupHandlerInfoTmp->GetName(), fMixIntupHandlerInfoTmp->GetTitle());
116 // mixIHI->PrepareEntry(che, -1, InputEventHandler(i),fAnalysisType);
117 // AliDebug(AliLog::kDebug+5, Form("chain[%d]->GetEntries() = %lld", i, mixIHI->GetChain()->GetEntries()));
118 // fMixTrees.Add(mixIHI);
120 // AliDebug(AliLog::kDebug+5, Form("fEntryCounter=%lld", fEntryCounter));
121 // if(fEventPool && fEventPool->NeedInit())
122 // fEventPool->Init();
123 AliDebug(AliLog::kDebug + 5, Form("->"));
126 //_____________________________________________________________________________
127 Bool_t AliMixInputEventHandler::Notify()
130 // Notify() is called for all mix input handlers
132 AliDebug(AliLog::kDebug + 5, Form("<-"));
133 if (fUseDefautProcess) {
134 AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
135 return AliMultiInputEventHandler::Notify();
137 AliDebug(AliLog::kDebug + 5, Form("->"));
141 //_____________________________________________________________________________
142 Bool_t AliMixInputEventHandler::Notify(const char *path)
145 // Notify(const char*path) is called for all mix input handlers
147 AliDebug(AliLog::kDebug + 5, Form("<- %s", path));
149 fMixIntupHandlerInfoTmp->AddTreeToChain(path);
150 Int_t lastIndex = fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->GetEntries();
151 TChainElement *che = (TChainElement *)fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->At(lastIndex - 1);
152 AliMixInputHandlerInfo *mixIHI = 0;
153 for (Int_t i = 0; i < fInputHandlers.GetEntries(); i++) {
154 AliDebug(AliLog::kDebug + 5, Form("fInputHandlers[%d]", i));
155 mixIHI = new AliMixInputHandlerInfo(fMixIntupHandlerInfoTmp->GetName(), fMixIntupHandlerInfoTmp->GetTitle());
156 mixIHI->PrepareEntry(che, -1, InputEventHandler(i), fAnalysisType);
157 AliDebug(AliLog::kDebug + 5, Form("chain[%d]->GetEntries() = %lld", i, mixIHI->GetChain()->GetEntries()));
158 fMixTrees.Add(mixIHI);
160 AliDebug(AliLog::kDebug + 5, Form("fEntryCounter=%lld", fEntryCounter));
161 if (fEventPool && fEventPool->NeedInit())
163 if (fUseDefautProcess) {
164 AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
165 return AliMultiInputEventHandler::Notify(path);
167 AliDebug(AliLog::kDebug + 5, Form("->"));
171 //_____________________________________________________________________________
172 Bool_t AliMixInputEventHandler::BeginEvent(Long64_t entry)
175 // BeginEvent(Long64_t entry) is called for all mix input handlers
177 AliDebug(AliLog::kDebug + 5, Form("-> %lld", entry));
178 if (fUseDefautProcess) {
179 AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
180 AliMultiInputEventHandler::BeginEvent(entry);/* return GetEntry();*/
182 AliDebug(AliLog::kDebug + 5, Form("->"));
185 //_____________________________________________________________________________
186 Bool_t AliMixInputEventHandler::GetEntry()
189 // All mixed events are set
191 AliDebug(AliLog::kDebug + 5, Form("<-"));
192 // if no event pool MixStd
193 // if (fEventPool && fEventPool->NeedInit()) fEventPool->Init();
197 // if buffer size is higher then 1
198 else if (fBufferSize > 1) {
201 // if mix number is higher then 0 and buffer size is 1
202 else if (fMixNumber > 0) {
203 MixEventsMoreTimesWithOneEvent();
205 AliWarning("Not supported Mixing !!!");
207 AliDebug(AliLog::kDebug + 5, Form("->"));
211 //_____________________________________________________________________________
212 Bool_t AliMixInputEventHandler::MixStd()
215 // Mix std - No event pool
217 AliDebug(AliLog::kDebug + 5, Form("<-"));
218 AliDebug(AliLog::kDebug + 1, "Mix method");
219 // get correct handler
220 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
221 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
222 AliInputEventHandler *inEvHMain = 0;
223 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
224 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
225 if (!inEvHMain) return kFALSE;
226 // return in case of 0 entry in full chain
227 if (!fEntryCounter) {
228 AliDebug(AliLog::kDebug + 3, Form("-> fEntryCounter == 0"));
229 // runs UserExecMix for all tasks, if needed
230 UserExecMixAllTasks(fEntryCounter, 1, fEntryCounter, -1, 0);
234 Int_t mixNum = fMixNumber;
235 if (fUsePreMixEvents) {
236 if (fEntryCounter <= 2 * fMixNumber) mixNum = 2 * fMixNumber + 2;
239 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
242 AliMixInputHandlerInfo *mihi = 0;
243 Long64_t entryMix = 0, entryMixReal = 0;
245 for (counter = 0; counter < mixNum; counter++) {
246 entryMix = fEntryCounter - 1 - counter ;
247 AliDebug(AliLog::kDebug + 5, Form("Handler[%d] entryMix %lld ", counter, entryMix));
248 if (entryMix < 0) break;
249 entryMixReal = entryMix;
250 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(0);
251 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
253 AliError("te is null. this is error. tell to developer (#1)")
255 mihi->PrepareEntry(te, entryMix, InputEventHandler(0), fAnalysisType);
256 // runs UserExecMix for all tasks
258 UserExecMixAllTasks(fEntryCounter, 1, fEntryCounter, entryMixReal, fNumberMixed);
259 InputEventHandler(0)->FinishEvent();
262 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
263 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
264 AliDebug(AliLog::kDebug + 5, Form("->"));
265 AliDebug(AliLog::kDebug + 5, Form("->"));
269 //_____________________________________________________________________________
270 Bool_t AliMixInputEventHandler::MixBuffer()
273 // Mix in event buffer
275 AliDebug(AliLog::kDebug + 5, Form("<-"));
276 AliDebug(AliLog::kDebug + 1, "Mix method");
277 // get correct handler
278 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
279 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
280 AliInputEventHandler *inEvHMain = 0;
281 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
282 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
283 if (!inEvHMain) return kFALSE;
284 // find out zero chain entries
285 Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetTree()->GetEntries();
287 Long64_t currentMainEntry = inEvHMain->GetTree()->GetTree()->GetReadEntry() + zeroChainEntries;
289 if (fEventPool && inEvHMain) fEventPool->AddEntry(currentMainEntry, inEvHMain->GetEvent());
291 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
296 Int_t idEntryList = -1;
297 if (fEventPool) el = fEventPool->FindEntryList(inEvHMain->GetEvent(), idEntryList);
298 // return in case of 0 entry in full chain
299 if (!fEntryCounter) {
300 AliDebug(AliLog::kDebug + 3, Form("-> fEntryCounter == 0"));
301 // runs UserExecMix for all tasks, if needed
302 if (el) UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
303 else UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
307 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (el null) +++++++++++++++++++", fEntryCounter));
308 UserExecMixAllTasks(fEntryCounter, -1, fEntryCounter, -1, 0);
312 if (elNum < fBufferSize + 1) {
313 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
314 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (%lld) +++++++++++++++++++", fEntryCounter, elNum));
318 AliMixInputHandlerInfo *mihi = 0;
319 Long64_t entryMix = 0, entryMixReal = 0;
321 AliInputEventHandler *eh = 0;
322 TObjArrayIter next(&fInputHandlers);
323 while ((eh = dynamic_cast<AliInputEventHandler *>(next()))) {
324 if (fEventPool && fEventPool->GetListOfEventCuts()->GetEntries() > 0) {
326 if (elNum >= fBufferSize) {
327 Long64_t entryInEntryList = elNum - 2 - counter;
328 if (entryInEntryList < 0) break;
329 entryMix = el->GetEntry(entryInEntryList);
332 AliDebug(AliLog::kDebug + 5, Form("Handler[%d] entryMix %lld ", counter, entryMix));
334 UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
337 entryMixReal = entryMix;
338 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(counter);
339 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
341 AliError("te is null. this is error. tell to developer (#1)")
343 AliDebug(AliLog::kDebug + 3, Form("Preparing InputEventHandler(%d)", counter));
344 mihi->PrepareEntry(te, entryMix, InputEventHandler(counter), fAnalysisType);
345 // runs UserExecMix for all tasks
346 UserExecMixAllTasks(fEntryCounter, idEntryList, fEntryCounter, entryMixReal, counter);
351 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
352 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
353 AliDebug(AliLog::kDebug + 5, Form("->"));
354 AliDebug(AliLog::kDebug + 5, Form("->"));
358 //_____________________________________________________________________________
359 Bool_t AliMixInputEventHandler::MixEventsMoreTimesWithOneEvent()
362 // Mix in history with one event in buffer
364 AliDebug(AliLog::kDebug + 5, "<-");
365 AliDebug(AliLog::kDebug + 1, "Mix method");
366 // get correct handler
367 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
368 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
369 AliInputEventHandler *inEvHMain = 0;
370 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
371 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
372 if (!inEvHMain) return kFALSE;
373 // find out zero chain entries
374 Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetTree()->GetEntries();
376 Long64_t currentMainEntry = inEvHMain->GetTree()->GetTree()->GetReadEntry() + zeroChainEntries;
377 if (fEventPool && inEvHMain) fEventPool->AddEntry(currentMainEntry, inEvHMain->GetEvent());
379 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
383 Int_t idEntryList = -1;
385 if (fEventPool) el = fEventPool->FindEntryList(inEvHMain->GetEvent(), idEntryList);
386 // return in case of 0 entry in full chain
387 if (!fEntryCounter) {
388 // runs UserExecMix for all tasks, if needed
389 if (el) UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
390 else UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
391 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (fEntryCounter=0, idEntryList=%d) +++++++++++++++++++", fEntryCounter, idEntryList));
396 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (el null, idEntryList=%d) +++++++++++++++++++", fEntryCounter, idEntryList));
397 UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
402 if (elNum < fBufferSize + 1) {
403 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
404 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED [FIRST ENTRY in el] (elnum=%lld, idEntryList=%d) +++++++++++++++++++", fEntryCounter, elNum, idEntryList));
409 Int_t mixNum = fMixNumber;
410 if (fUsePreMixEvents) {
411 if (elNum <= 2 * fMixNumber + 1) mixNum = elNum + 1;
413 AliMixInputHandlerInfo *mihi = 0;
414 Long64_t entryMix = 0, entryMixReal = 0;
416 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(0);
417 // fills num for main events
418 for (counter = 0; counter < mixNum; counter++) {
419 Long64_t entryInEntryList = elNum - 2 - counter;
420 AliDebug(AliLog::kDebug + 3, Form("entryInEntryList=%lld", entryInEntryList));
421 if (entryInEntryList < 0) break;
422 entryMix = el->GetEntry(entryInEntryList);
423 AliDebug(AliLog::kDebug + 3, Form("entryMix=%lld", entryMix));
424 if (entryMix < 0) break;
425 entryMixReal = entryMix;
426 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
428 AliError("te is null. this is error. tell to developer (#2)")
430 mihi->PrepareEntry(te, entryMix, InputEventHandler(0), fAnalysisType);
431 // runs UserExecMix for all tasks
433 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, entryMixReal, fNumberMixed);
434 InputEventHandler(0)->FinishEvent();
437 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
438 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
439 AliDebug(AliLog::kDebug + 5, Form("->"));
440 AliDebug(AliLog::kDebug + 5, Form("->"));
444 //_____________________________________________________________________________
445 Bool_t AliMixInputEventHandler::MixEventsMoreTimesWithBuffer()
448 // Mix more events in buffer with mixing with history
450 AliWarning("Not implemented");
454 //_____________________________________________________________________________
455 Bool_t AliMixInputEventHandler::FinishEvent()
458 // FinishEvent() is called for all mix input handlers
460 AliDebug(AliLog::kDebug + 5, Form("<-"));
461 AliMultiInputEventHandler::FinishEvent();
463 AliDebug(AliLog::kDebug + 5, Form("->"));
467 //_____________________________________________________________________________
468 void AliMixInputEventHandler::AddInputEventHandler(AliVEventHandler*)
471 // AddInputEventHandler will not be used
473 AliWarning("Function AddInputEventHandler is disabled for AliMixEventInputHandler !!!");
474 AliWarning("Use AliMixEventInputHandler::SetInputHandlerForMixing instead. Exiting ...");
477 //_____________________________________________________________________________
478 void AliMixInputEventHandler::UserExecMixAllTasks(Long64_t entryCounter, Int_t idEntryList, Long64_t entryMainReal, Long64_t entryMixReal, Int_t numMixed)
481 // Execute all task and sets mixing parameters
483 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
484 AliAnalysisTaskSE *mixTask = 0;
485 TObjArrayIter next(mgr->GetTasks());
486 while ((mixTask = (AliAnalysisTaskSE *) next())) {
487 if (dynamic_cast<AliAnalysisTaskSE *>(mixTask)) {
488 AliDebug(AliLog::kDebug, Form("%s %lld %d [%lld,%lld] %d", mixTask->GetName(), entryCounter, numMixed, entryMainReal, entryMixReal, idEntryList));
489 fCurrentEntry = entryCounter;
490 fCurrentEntryMain = entryMainReal;
491 fCurrentEntryMix = entryMixReal;
492 fCurrentBinIndex = idEntryList;
493 if (entryMixReal >= 0) mixTask->UserExecMix("");
498 //_____________________________________________________________________________
499 void AliMixInputEventHandler::SetMixNumber(const Int_t mixNum)
504 if (fMixNumber > 1 && fBufferSize > 1) {
505 AliWarning("Sleeping 10 sec to show Warning Message ...")
506 AliWarning("=========================================================================================");
507 AliWarning(Form("BufferSize(%d) higher > 1 and fMixNumber(%d) > 1, which is not supported", fBufferSize, mixNum));
509 AliWarning("\tBufferSize will be set to 1");
511 AliWarning("Hints:");
513 AliWarning("\t1.If you want to use buffer do:");
514 AliWarning(Form("\t\tAliMixInputEventHandler *mixH = new AliMixInputEventHandler(%d,1)", fBufferSize));
516 AliWarning("\t2.If you want to use mix more time with buffer size 1, then do:");
517 AliWarning(Form("\t\tAliMixInputEventHandler *mixH = new AliMixInputEventHandler(1,%d)", mixNum));
519 AliWarning("=========================================================================================");
520 gSystem->Sleep(10000);