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 fMixIntupHandlerInfoTmp->AddTreeToChain(path);
133 Int_t lastIndex = fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->GetEntries();
134 TChainElement *che = (TChainElement *)fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->At(lastIndex - 1);
135 AliMixInputHandlerInfo *mixIHI = 0;
136 for (Int_t i = 0; i < fInputHandlers.GetEntries(); i++) {
137 AliDebug(AliLog::kDebug + 5, Form("fInputHandlers[%d]", i));
138 mixIHI = new AliMixInputHandlerInfo(fMixIntupHandlerInfoTmp->GetName(), fMixIntupHandlerInfoTmp->GetTitle());
139 mixIHI->PrepareEntry(che, -1, (AliInputEventHandler*)InputEventHandler(i), fAnalysisType);
140 AliDebug(AliLog::kDebug + 5, Form("chain[%d]->GetEntries() = %lld", i, mixIHI->GetChain()->GetEntries()));
141 fMixTrees.Add(mixIHI);
143 AliDebug(AliLog::kDebug + 5, Form("fEntryCounter=%lld", fEntryCounter));
144 if (fEventPool && fEventPool->NeedInit())
146 if (fUseDefautProcess) {
147 AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
148 return AliMultiInputEventHandler::Notify(path);
150 AliDebug(AliLog::kDebug + 5, Form("->"));
154 //_____________________________________________________________________________
155 Bool_t AliMixInputEventHandler::BeginEvent(Long64_t entry)
158 // BeginEvent(Long64_t entry) is called for all mix input handlers
160 AliDebug(AliLog::kDebug + 5, Form("-> %lld", entry));
161 if (fUseDefautProcess) {
162 AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
163 AliMultiInputEventHandler::BeginEvent(entry);/* return GetEntry();*/
165 AliDebug(AliLog::kDebug + 5, Form("->"));
168 //_____________________________________________________________________________
169 Bool_t AliMixInputEventHandler::GetEntry()
172 // All mixed events are set
174 AliDebug(AliLog::kDebug + 5, Form("<-"));
179 // if buffer size is higher then 1
180 else if (fBufferSize > 1) {
183 // if mix number is higher then 0 and buffer size is 1
184 else if (fMixNumber > 0) {
185 MixEventsMoreTimesWithOneEvent();
187 AliWarning("Not supported Mixing !!!");
190 AliDebug(AliLog::kDebug + 5, Form("->"));
194 //_____________________________________________________________________________
195 Bool_t AliMixInputEventHandler::MixStd()
198 // Mix std - No event pool
200 AliDebug(AliLog::kDebug + 5, Form("<-"));
201 AliDebug(AliLog::kDebug + 1, "Mix method");
202 // get correct handler
203 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
204 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
205 AliInputEventHandler *inEvHMain = 0;
206 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
207 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
208 if (!inEvHMain) return kFALSE;
210 // check for PhysSelection
211 if (!IsEventCurrentSelected()) return kFALSE;
213 // return in case of 0 entry in full chain
214 if (!fEntryCounter) {
215 AliDebug(AliLog::kDebug + 3, Form("-> fEntryCounter == 0"));
216 // runs UserExecMix for all tasks, if needed
217 UserExecMixAllTasks(fEntryCounter, 1, fEntryCounter, -1, 0);
221 Int_t mixNum = fMixNumber;
223 if (fEntryCounter <= 2 * fMixNumber) mixNum = 2 * fMixNumber + 2;
226 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
229 AliMixInputHandlerInfo *mihi = 0;
230 Long64_t entryMix = 0, entryMixReal = 0;
232 for (counter = 0; counter < mixNum; counter++) {
233 entryMix = fEntryCounter - 1 - counter ;
234 AliDebug(AliLog::kDebug + 5, Form("Handler[%d] entryMix %lld ", counter, entryMix));
235 if (entryMix < 0) break;
236 entryMixReal = entryMix;
237 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(0);
238 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
240 AliError("te is null. this is error. tell to developer (#1)")
242 mihi->PrepareEntry(te, entryMix, (AliInputEventHandler*)InputEventHandler(0), fAnalysisType);
243 // runs UserExecMix for all tasks
245 UserExecMixAllTasks(fEntryCounter, 1, fEntryCounter, entryMixReal, fNumberMixed);
246 InputEventHandler(0)->FinishEvent();
249 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
250 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
251 AliDebug(AliLog::kDebug + 5, Form("->"));
252 AliDebug(AliLog::kDebug + 5, Form("->"));
256 //_____________________________________________________________________________
257 Bool_t AliMixInputEventHandler::MixBuffer()
260 // Mix in event buffer
262 AliDebug(AliLog::kDebug + 5, Form("<-"));
263 AliDebug(AliLog::kDebug + 1, "Mix method");
264 // get correct handler
265 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
266 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
267 AliInputEventHandler *inEvHMain = 0;
268 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
269 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
270 if (!inEvHMain) return kFALSE;
272 // check for PhysSelection
273 if (!IsEventCurrentSelected()) return kFALSE;
275 // find out zero chain entries
276 Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetTree()->GetEntries();
278 Long64_t currentMainEntry = inEvHMain->GetTree()->GetTree()->GetReadEntry() + zeroChainEntries;
280 if (fEventPool && inEvHMain) fEventPool->AddEntry(currentMainEntry, inEvHMain->GetEvent());
282 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
287 Int_t idEntryList = -1;
288 if (fEventPool) el = fEventPool->FindEntryList(inEvHMain->GetEvent(), idEntryList);
289 // return in case of 0 entry in full chain
290 if (!fEntryCounter) {
291 AliDebug(AliLog::kDebug + 3, Form("-> fEntryCounter == 0"));
292 // runs UserExecMix for all tasks, if needed
293 if (el) UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
294 else UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
298 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (el null) +++++++++++++++++++", fEntryCounter));
299 UserExecMixAllTasks(fEntryCounter, -1, fEntryCounter, -1, 0);
303 if (elNum < fBufferSize + 1) {
304 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
305 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (%lld) LESS THEN BUFFER +++++++++++++++++++", fEntryCounter, elNum));
309 AliMixInputHandlerInfo *mihi = 0;
310 Long64_t entryMix = 0, entryMixReal = 0;
312 AliInputEventHandler *eh = 0;
313 TObjArrayIter next(&fInputHandlers);
314 while ((eh = dynamic_cast<AliInputEventHandler *>(next()))) {
315 if (fEventPool && fEventPool->GetListOfEventCuts()->GetEntries() > 0) {
317 if (elNum >= fBufferSize) {
318 Long64_t entryInEntryList = elNum - 2 - counter;
319 if (entryInEntryList < 0) break;
320 entryMix = el->GetEntry(entryInEntryList);
323 AliDebug(AliLog::kDebug + 5, Form("Handler[%d] entryMix %lld ", counter, entryMix));
325 UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
328 entryMixReal = entryMix;
329 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(counter);
330 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
332 AliError("te is null. this is error. tell to developer (#1)")
334 AliDebug(AliLog::kDebug + 3, Form("Preparing InputEventHandler(%d)", counter));
335 mihi->PrepareEntry(te, entryMix, (AliInputEventHandler*)InputEventHandler(counter), fAnalysisType);
336 // runs UserExecMix for all tasks
337 UserExecMixAllTasks(fEntryCounter, idEntryList, fEntryCounter, entryMixReal, counter);
342 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
343 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
344 AliDebug(AliLog::kDebug + 5, Form("->"));
345 AliDebug(AliLog::kDebug + 5, Form("->"));
349 //_____________________________________________________________________________
350 Bool_t AliMixInputEventHandler::MixEventsMoreTimesWithOneEvent()
353 // Mix in history with one event in buffer
355 AliDebug(AliLog::kDebug + 5, "<-");
356 AliDebug(AliLog::kDebug + 1, "Mix method");
357 // get correct handler
358 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
359 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
360 AliInputEventHandler *inEvHMain = 0;
361 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
362 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
363 if (!inEvHMain) return kFALSE;
365 // check for PhysSelection
366 if (!IsEventCurrentSelected()) return kFALSE;
368 // find out zero chain entries
369 Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetTree()->GetEntries();
371 Long64_t currentMainEntry = inEvHMain->GetTree()->GetTree()->GetReadEntry() + zeroChainEntries;
372 if (fEventPool && inEvHMain) fEventPool->AddEntry(currentMainEntry, inEvHMain->GetEvent());
374 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
378 Int_t idEntryList = -1;
380 if (fEventPool) el = fEventPool->FindEntryList(inEvHMain->GetEvent(), idEntryList);
381 // return in case of 0 entry in full chain
382 if (!fEntryCounter) {
383 // runs UserExecMix for all tasks, if needed
384 if (el && fDoMixIfNotEnoughEvents) {
385 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
388 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
390 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (fEntryCounter=0, idEntryList=%d) +++++++++++++++++++", fEntryCounter, idEntryList));
395 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (el null, idEntryList=%d) +++++++++++++++++++", fEntryCounter, idEntryList));
396 UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
401 if (elNum < fBufferSize + 1) {
402 if (fDoMixIfNotEnoughEvents) {
403 // include main event in to counter in this case (so idEntryList>0)
404 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
406 // dont include it in main event counter (idEntryList = -1)
408 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
410 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED [FIRST ENTRY in el] (elnum=%lld, idEntryList=%d) +++++++++++++++++++", fEntryCounter, elNum, idEntryList));
413 if (!fDoMixIfNotEnoughEvents) {
414 if (elNum <= fMixNumber + 1) {
415 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
416 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (%lld) NOT ENOUGH EVENTS TO MIX => NEED=%d +++++++++++++++++++", fEntryCounter, elNum, fMixNumber + 1));
422 Int_t mixNum = fMixNumber;
424 if (elNum <= 2 * fMixNumber + 1) mixNum = elNum + 1;
426 AliMixInputHandlerInfo *mihi = 0;
427 Long64_t entryMix = 0, entryMixReal = 0;
429 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(0);
430 // fills num for main events
431 for (counter = 0; counter < mixNum; counter++) {
432 Long64_t entryInEntryList = elNum - 2 - counter;
433 AliDebug(AliLog::kDebug + 3, Form("entryInEntryList=%lld", entryInEntryList));
434 if (entryInEntryList < 0) break;
435 entryMix = el->GetEntry(entryInEntryList);
436 AliDebug(AliLog::kDebug + 3, Form("entryMix=%lld", entryMix));
437 if (entryMix < 0) break;
438 entryMixReal = entryMix;
439 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
441 AliError("te is null. this is error. tell to developer (#2)")
443 mihi->PrepareEntry(te, entryMix, (AliInputEventHandler*)InputEventHandler(0), fAnalysisType);
444 // runs UserExecMix for all tasks
446 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, entryMixReal, fNumberMixed);
447 InputEventHandler(0)->FinishEvent();
450 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
451 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
452 AliDebug(AliLog::kDebug + 5, Form("->"));
453 AliDebug(AliLog::kDebug + 5, Form("->"));
457 //_____________________________________________________________________________
458 Bool_t AliMixInputEventHandler::MixEventsMoreTimesWithBuffer()
461 // Mix more events in buffer with mixing with history
463 AliWarning("Not implemented");
467 //_____________________________________________________________________________
468 Bool_t AliMixInputEventHandler::FinishEvent()
471 // FinishEvent() is called for all mix input handlers
473 AliDebug(AliLog::kDebug + 5, Form("<-"));
474 AliMultiInputEventHandler::FinishEvent();
476 AliDebug(AliLog::kDebug + 5, Form("->"));
480 //_____________________________________________________________________________
481 void AliMixInputEventHandler::AddInputEventHandler(AliVEventHandler*)
484 // AddInputEventHandler will not be used
486 AliWarning("Function AddInputEventHandler is disabled for AliMixEventInputHandler !!!");
487 AliWarning("Use AliMixEventInputHandler::SetInputHandlerForMixing instead. Exiting ...");
490 //_____________________________________________________________________________
491 void AliMixInputEventHandler::UserExecMixAllTasks(Long64_t entryCounter, Int_t idEntryList, Long64_t entryMainReal, Long64_t entryMixReal, Int_t numMixed)
494 // Execute all task and sets mixing parameters
496 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
497 AliAnalysisTaskSE *mixTask = 0;
498 TObjArrayIter next(mgr->GetTasks());
499 while ((mixTask = (AliAnalysisTaskSE *) next())) {
500 if (dynamic_cast<AliAnalysisTaskSE *>(mixTask)) {
501 AliDebug(AliLog::kDebug, Form("%s %lld %d [%lld,%lld] %d", mixTask->GetName(), entryCounter, numMixed, entryMainReal, entryMixReal, idEntryList));
502 fCurrentEntry = entryCounter;
503 fCurrentEntryMain = entryMainReal;
504 fCurrentEntryMix = entryMixReal;
505 fCurrentBinIndex = idEntryList;
506 if (entryMixReal >= 0) mixTask->UserExecMix("");
511 //_____________________________________________________________________________
512 void AliMixInputEventHandler::SetMixNumber(const Int_t mixNum)
517 if (fMixNumber > 1 && fBufferSize > 1) {
518 AliWarning("Sleeping 10 sec to show Warning Message ...")
519 AliWarning("=========================================================================================");
520 AliWarning(Form("BufferSize(%d) higher > 1 and fMixNumber(%d) > 1, which is not supported", fBufferSize, mixNum));
522 AliWarning("\tBufferSize will be set to 1");
524 AliWarning("Hints:");
526 AliWarning("\t1.If you want to use buffer do:");
527 AliWarning(Form("\t\tAliMixInputEventHandler *mixH = new AliMixInputEventHandler(%d,1)", fBufferSize));
529 AliWarning("\t2.If you want to use mix more time with buffer size 1, then do:");
530 AliWarning(Form("\t\tAliMixInputEventHandler *mixH = new AliMixInputEventHandler(1,%d)", mixNum));
532 AliWarning("=========================================================================================");
533 gSystem->Sleep(10000);
539 Bool_t AliMixInputEventHandler::IsEventCurrentSelected()
541 AliDebug(AliLog::kDebug + 5, Form("<-"));
542 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
543 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
544 Bool_t isSelected = kTRUE;
546 if (fOfflineTriggerMask && mh->GetEventSelection()) {
547 isSelected = fOfflineTriggerMask & mh->IsEventSelected();
550 AliDebug(AliLog::kDebug + 1, Form("isSelected=%d", isSelected));
551 AliDebug(AliLog::kDebug + 5, Form("-> %d", isSelected));