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),
38 fDoMixIfNotEnoughEvents(kTRUE),
43 fOfflineTriggerMask(0)
46 // Default constructor.
48 AliDebug(AliLog::kDebug + 10, "<-");
49 fMixTrees.SetOwner(kTRUE);
51 AliDebug(AliLog::kDebug + 10, "->");
54 //_____________________________________________________________________________
55 void AliMixInputEventHandler::SetInputHandlerForMixing(const AliInputEventHandler *const inHandler)
58 // Create N (fBufferSize) copies of input handler
60 AliDebug(AliLog::kDebug + 5, Form("<-"));
61 fInputHandlers.Clear();
62 AliDebug(AliLog::kDebug + 5, Form("Creating %d input event handlers ...", fBufferSize));
63 for (Int_t i = 0; i < fBufferSize; i++) {
64 AliDebug(AliLog::kDebug + 5, Form("Adding %d ...", i));
65 fInputHandlers.Add((AliInputEventHandler *) inHandler->Clone());
68 AliDebug(AliLog::kDebug + 5, Form("->"));
71 //_____________________________________________________________________________
72 Bool_t AliMixInputEventHandler::Init(TTree *tree, Option_t *opt)
75 // Init(const char*path) is called for all mix input handlers.
76 // Create event pool if needed
78 AliDebug(AliLog::kDebug + 5, Form("<- %p %s", (void *)tree, opt));
81 AliDebug(AliLog::kDebug + 5, Form("->"));
85 if (!fDoMixIfNotEnoughEvents) {
87 AliWarning("fDoMixIfNotEnoughEvents=kTRUE -> setting fDoMixExtra=kFALSE");
90 // clears array of input handlers
92 // create AliMixInputHandlerInfo
93 if (!fMixIntupHandlerInfoTmp) {
94 // loads first file TChain (tree)
96 fMixIntupHandlerInfoTmp = new AliMixInputHandlerInfo(tree->GetName());
99 AliInputEventHandler *ih = 0;
100 for (Int_t i = 0; i < fInputHandlers.GetEntries(); i++) {
101 ih = (AliInputEventHandler *) fInputHandlers.At(i);
102 ih->SetParentHandler(this);
103 // ih->Init(tree,opt);
106 AliDebug(AliLog::kDebug + 5, Form("->"));
109 //_____________________________________________________________________________
110 Bool_t AliMixInputEventHandler::Notify()
113 // Notify() is called for all mix input handlers
115 AliDebug(AliLog::kDebug + 5, Form("<-"));
116 if (fUseDefautProcess) {
117 AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
118 return AliMultiInputEventHandler::Notify();
120 AliDebug(AliLog::kDebug + 5, Form("->"));
124 //_____________________________________________________________________________
125 Bool_t AliMixInputEventHandler::Notify(const char *path)
128 // Notify(const char*path) is called for all mix input handlers
130 AliDebug(AliLog::kDebug + 5, Form("<- %s", path));
132 Bool_t doPrepareEntry=kTRUE;
133 TString anType = fAnalysisType;
135 // in case of local doPrepareEntry only first time
136 if (anType.CompareTo("proof")) doPrepareEntry = (fMixIntupHandlerInfoTmp->GetChain()->GetEntries()<=0);
139 fMixIntupHandlerInfoTmp->AddTreeToChain(path);
140 Int_t lastIndex = fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->GetEntries();
141 TChainElement *che = (TChainElement *)fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->At(lastIndex - 1);
142 AliMixInputHandlerInfo *mixIHI = 0;
143 for (Int_t i = 0; i < fInputHandlers.GetEntries(); i++) {
144 AliDebug(AliLog::kDebug + 5, Form("fInputHandlers[%d]", i));
145 mixIHI = new AliMixInputHandlerInfo(fMixIntupHandlerInfoTmp->GetName(), fMixIntupHandlerInfoTmp->GetTitle());
146 // mixIHI->PrepareEntry(che, -1, (AliInputEventHandler *)InputEventHandler(i), fAnalysisType);
147 if (doPrepareEntry) mixIHI->PrepareEntry(che, -1, (AliInputEventHandler *)InputEventHandler(i), fAnalysisType);
148 AliDebug(AliLog::kDebug + 5, Form("chain[%d]->GetEntries() = %lld", i, mixIHI->GetChain()->GetEntries()));
149 fMixTrees.Add(mixIHI);
151 AliDebug(AliLog::kDebug + 5, Form("fEntryCounter=%lld", fEntryCounter));
152 if (fEventPool && fEventPool->NeedInit())
154 if (fUseDefautProcess) {
155 AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
156 return AliMultiInputEventHandler::Notify(path);
158 AliDebug(AliLog::kDebug + 5, Form("->"));
162 //_____________________________________________________________________________
163 Bool_t AliMixInputEventHandler::BeginEvent(Long64_t entry)
166 // BeginEvent(Long64_t entry) is called for all mix input handlers
168 AliDebug(AliLog::kDebug + 5, Form("-> %lld", entry));
169 if (fUseDefautProcess) {
170 AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
171 AliMultiInputEventHandler::BeginEvent(entry);/* return GetEntry();*/
173 AliDebug(AliLog::kDebug + 5, Form("->"));
176 //_____________________________________________________________________________
177 Bool_t AliMixInputEventHandler::GetEntry()
180 // All mixed events are set
182 AliDebug(AliLog::kDebug + 5, Form("<-"));
187 // if buffer size is higher then 1
188 else if (fBufferSize > 1) {
191 // if mix number is higher then 0 and buffer size is 1
192 else if (fMixNumber > 0) {
193 MixEventsMoreTimesWithOneEvent();
195 AliWarning("Not supported Mixing !!!");
198 AliDebug(AliLog::kDebug + 5, Form("->"));
202 //_____________________________________________________________________________
203 Bool_t AliMixInputEventHandler::MixStd()
206 // Mix std - No event pool
208 AliDebug(AliLog::kDebug + 5, Form("<-"));
209 AliDebug(AliLog::kDebug + 1, "Mix method");
210 // get correct handler
211 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
212 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
213 AliInputEventHandler *inEvHMain = 0;
214 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
215 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
216 if (!inEvHMain) return kFALSE;
218 // check for PhysSelection
219 if (!IsEventCurrentSelected()) return kFALSE;
221 // return in case of 0 entry in full chain
222 if (!fEntryCounter) {
223 AliDebug(AliLog::kDebug + 3, Form("-> fEntryCounter == 0"));
224 // runs UserExecMix for all tasks, if needed
225 UserExecMixAllTasks(fEntryCounter, 1, fEntryCounter, -1, 0);
229 Int_t mixNum = fMixNumber;
231 if (fEntryCounter <= 2 * fMixNumber) mixNum = 2 * fMixNumber + 2;
234 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
237 AliMixInputHandlerInfo *mihi = 0;
238 Long64_t entryMix = 0, entryMixReal = 0;
240 for (counter = 0; counter < mixNum; counter++) {
241 entryMix = fEntryCounter - 1 - counter ;
242 AliDebug(AliLog::kDebug + 5, Form("Handler[%d] entryMix %lld ", counter, entryMix));
243 if (entryMix < 0) break;
244 entryMixReal = entryMix;
245 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(0);
246 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
248 AliError("te is null. this is error. tell to developer (#1)");
250 mihi->PrepareEntry(te, entryMix, (AliInputEventHandler *)InputEventHandler(0), fAnalysisType);
251 // runs UserExecMix for all tasks
253 UserExecMixAllTasks(fEntryCounter, 1, fEntryCounter, entryMixReal, fNumberMixed);
254 InputEventHandler(0)->FinishEvent();
257 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
258 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
259 AliDebug(AliLog::kDebug + 5, Form("->"));
260 AliDebug(AliLog::kDebug + 5, Form("->"));
264 //_____________________________________________________________________________
265 Bool_t AliMixInputEventHandler::MixBuffer()
268 // Mix in event buffer
270 AliDebug(AliLog::kDebug + 5, Form("<-"));
271 AliDebug(AliLog::kDebug + 1, "Mix method");
272 // get correct handler
273 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
274 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
275 AliInputEventHandler *inEvHMain = 0;
276 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
277 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
278 if (!inEvHMain) return kFALSE;
280 // check for PhysSelection
281 if (!IsEventCurrentSelected()) return kFALSE;
283 // find out zero chain entries
284 Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetTree()->GetEntries();
286 Long64_t currentMainEntry = inEvHMain->GetTree()->GetTree()->GetReadEntry() + zeroChainEntries;
288 if (fEventPool && inEvHMain) fEventPool->AddEntry(currentMainEntry, inEvHMain->GetEvent());
290 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
295 Int_t idEntryList = -1;
296 if (fEventPool) el = fEventPool->FindEntryList(inEvHMain->GetEvent(), idEntryList);
297 // return in case of 0 entry in full chain
298 if (!fEntryCounter) {
299 AliDebug(AliLog::kDebug + 3, Form("-> fEntryCounter == 0"));
300 // runs UserExecMix for all tasks, if needed
301 if (el) UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
302 else UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
306 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (el null) +++++++++++++++++++", fEntryCounter));
307 UserExecMixAllTasks(fEntryCounter, -1, fEntryCounter, -1, 0);
311 if (elNum < fBufferSize + 1) {
312 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
313 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (%lld) LESS THEN BUFFER +++++++++++++++++++", fEntryCounter, elNum));
317 AliMixInputHandlerInfo *mihi = 0;
318 Long64_t entryMix = 0, entryMixReal = 0;
320 AliInputEventHandler *eh = 0;
321 TObjArrayIter next(&fInputHandlers);
322 while ((eh = dynamic_cast<AliInputEventHandler *>(next()))) {
323 if (fEventPool && fEventPool->GetListOfEventCuts()->GetEntries() > 0) {
325 if (elNum >= fBufferSize) {
326 Long64_t entryInEntryList = elNum - 2 - counter;
327 if (entryInEntryList < 0) break;
328 entryMix = el->GetEntry(entryInEntryList);
331 AliDebug(AliLog::kDebug + 5, Form("Handler[%d] entryMix %lld ", counter, entryMix));
333 UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
336 entryMixReal = entryMix;
337 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(counter);
338 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
340 AliError("te is null. this is error. tell to developer (#1)");
342 AliDebug(AliLog::kDebug + 3, Form("Preparing InputEventHandler(%d)", counter));
343 mihi->PrepareEntry(te, entryMix, (AliInputEventHandler *)InputEventHandler(counter), fAnalysisType);
344 // runs UserExecMix for all tasks
345 UserExecMixAllTasks(fEntryCounter, idEntryList, fEntryCounter, entryMixReal, counter);
350 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
351 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
352 AliDebug(AliLog::kDebug + 5, Form("->"));
353 AliDebug(AliLog::kDebug + 5, Form("->"));
357 //_____________________________________________________________________________
358 Bool_t AliMixInputEventHandler::MixEventsMoreTimesWithOneEvent()
361 // Mix in history with one event in buffer
363 AliDebug(AliLog::kDebug + 5, "<-");
364 AliDebug(AliLog::kDebug + 1, "Mix method");
365 // get correct handler
366 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
367 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
368 AliInputEventHandler *inEvHMain = 0;
369 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
370 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
371 if (!inEvHMain) return kFALSE;
373 // check for PhysSelection
374 if (!IsEventCurrentSelected()) return kFALSE;
376 // find out zero chain entries
377 Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetTree()->GetEntries();
379 Long64_t currentMainEntry = inEvHMain->GetTree()->GetTree()->GetReadEntry() + zeroChainEntries;
380 if (fEventPool && inEvHMain) fEventPool->AddEntry(currentMainEntry, inEvHMain->GetEvent());
382 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
386 Int_t idEntryList = -1;
388 if (fEventPool) el = fEventPool->FindEntryList(inEvHMain->GetEvent(), idEntryList);
389 // return in case of 0 entry in full chain
390 if (!fEntryCounter) {
391 // runs UserExecMix for all tasks, if needed
392 if (el && fDoMixIfNotEnoughEvents) {
393 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
396 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
398 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (fEntryCounter=0, idEntryList=%d) +++++++++++++++++++", fEntryCounter, idEntryList));
403 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (el null, idEntryList=%d) +++++++++++++++++++", fEntryCounter, idEntryList));
404 UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
409 if (elNum < fBufferSize + 1) {
410 if (fDoMixIfNotEnoughEvents) {
411 // include main event in to counter in this case (so idEntryList>0)
412 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
414 // dont include it in main event counter (idEntryList = -1)
416 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
418 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED [FIRST ENTRY in el] (elnum=%lld, idEntryList=%d) +++++++++++++++++++", fEntryCounter, elNum, idEntryList));
421 if (!fDoMixIfNotEnoughEvents) {
422 if (elNum <= fMixNumber + 1) {
423 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
424 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (%lld) NOT ENOUGH EVENTS TO MIX => NEED=%d +++++++++++++++++++", fEntryCounter, elNum, fMixNumber + 1));
430 Int_t mixNum = fMixNumber;
432 if (elNum <= 2 * fMixNumber + 1) mixNum = elNum + 1;
434 AliMixInputHandlerInfo *mihi = 0;
435 Long64_t entryMix = 0, entryMixReal = 0;
437 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(0);
438 // fills num for main events
439 for (counter = 0; counter < mixNum; counter++) {
440 Long64_t entryInEntryList = elNum - 2 - counter;
441 AliDebug(AliLog::kDebug + 3, Form("entryInEntryList=%lld", entryInEntryList));
442 if (entryInEntryList < 0) break;
443 entryMix = el->GetEntry(entryInEntryList);
444 AliDebug(AliLog::kDebug + 3, Form("entryMix=%lld", entryMix));
445 if (entryMix < 0) break;
446 entryMixReal = entryMix;
447 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
449 AliError("te is null. this is error. tell to developer (#2)");
451 mihi->PrepareEntry(te, entryMix, (AliInputEventHandler *)InputEventHandler(0), fAnalysisType);
452 // runs UserExecMix for all tasks
454 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, entryMixReal, fNumberMixed);
455 InputEventHandler(0)->FinishEvent();
458 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
459 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
460 AliDebug(AliLog::kDebug + 5, Form("->"));
461 AliDebug(AliLog::kDebug + 5, Form("->"));
465 //_____________________________________________________________________________
466 Bool_t AliMixInputEventHandler::MixEventsMoreTimesWithBuffer()
469 // Mix more events in buffer with mixing with history
471 AliWarning("Not implemented");
475 //_____________________________________________________________________________
476 Bool_t AliMixInputEventHandler::FinishEvent()
479 // FinishEvent() is called for all mix input handlers
481 AliDebug(AliLog::kDebug + 5, Form("<-"));
482 AliMultiInputEventHandler::FinishEvent();
484 AliDebug(AliLog::kDebug + 5, Form("->"));
488 //_____________________________________________________________________________
489 void AliMixInputEventHandler::AddInputEventHandler(AliVEventHandler *)
492 // AddInputEventHandler will not be used
494 AliWarning("Function AddInputEventHandler is disabled for AliMixEventInputHandler !!!");
495 AliWarning("Use AliMixEventInputHandler::SetInputHandlerForMixing instead. Exiting ...");
498 //_____________________________________________________________________________
499 void AliMixInputEventHandler::UserExecMixAllTasks(Long64_t entryCounter, Int_t idEntryList, Long64_t entryMainReal, Long64_t entryMixReal, Int_t numMixed)
502 // Execute all task and sets mixing parameters
504 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
505 AliAnalysisTaskSE *mixTask = 0;
506 TObjArrayIter next(mgr->GetTasks());
507 while ((mixTask = dynamic_cast<AliAnalysisTaskSE *>(next()))) {
508 AliDebug(AliLog::kDebug, Form("%s %lld %d [%lld,%lld] %d", mixTask->GetName(), entryCounter, numMixed, entryMainReal, entryMixReal, idEntryList));
509 fCurrentEntry = entryCounter;
510 fCurrentEntryMain = entryMainReal;
511 fCurrentEntryMix = entryMixReal;
512 fCurrentBinIndex = idEntryList;
513 if (entryMixReal >= 0) mixTask->UserExecMix("");
517 //_____________________________________________________________________________
518 void AliMixInputEventHandler::SetMixNumber(const Int_t mixNum)
523 if (fMixNumber > 1 && fBufferSize > 1) {
524 AliWarning("Sleeping 10 sec to show Warning Message ...");
525 AliWarning("=========================================================================================");
526 AliWarning(Form("BufferSize(%d) higher > 1 and fMixNumber(%d) > 1, which is not supported", fBufferSize, mixNum));
528 AliWarning("\tBufferSize will be set to 1");
530 AliWarning("Hints:");
532 AliWarning("\t1.If you want to use buffer do:");
533 AliWarning(Form("\t\tAliMixInputEventHandler *mixH = new AliMixInputEventHandler(%d,1)", fBufferSize));
535 AliWarning("\t2.If you want to use mix more time with buffer size 1, then do:");
536 AliWarning(Form("\t\tAliMixInputEventHandler *mixH = new AliMixInputEventHandler(1,%d)", mixNum));
538 AliWarning("=========================================================================================");
539 gSystem->Sleep(10000);
545 Bool_t AliMixInputEventHandler::IsEventCurrentSelected()
547 AliDebug(AliLog::kDebug + 5, Form("<-"));
548 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
549 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
550 Bool_t isSelected = kTRUE;
552 if (fOfflineTriggerMask && mh->GetEventSelection()) {
553 isSelected = fOfflineTriggerMask & mh->IsEventSelected();
556 AliDebug(AliLog::kDebug + 1, Form("isSelected=%d", isSelected));
557 AliDebug(AliLog::kDebug + 5, Form("-> %d", isSelected));