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 firstRun=(fMixIntupHandlerInfoTmp->GetChain()->GetEntries()<=0);
134 fMixIntupHandlerInfoTmp->AddTreeToChain(path);
135 Int_t lastIndex = fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->GetEntries();
136 TChainElement *che = (TChainElement *)fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->At(lastIndex - 1);
137 AliMixInputHandlerInfo *mixIHI = 0;
138 for (Int_t i = 0; i < fInputHandlers.GetEntries(); i++) {
139 AliDebug(AliLog::kDebug + 5, Form("fInputHandlers[%d]", i));
140 mixIHI = new AliMixInputHandlerInfo(fMixIntupHandlerInfoTmp->GetName(), fMixIntupHandlerInfoTmp->GetTitle());
141 if (firstRun) mixIHI->PrepareEntry(che, -1, (AliInputEventHandler*)InputEventHandler(i), fAnalysisType);
142 AliDebug(AliLog::kDebug + 5, Form("chain[%d]->GetEntries() = %lld", i, mixIHI->GetChain()->GetEntries()));
143 fMixTrees.Add(mixIHI);
145 AliDebug(AliLog::kDebug + 5, Form("fEntryCounter=%lld", fEntryCounter));
146 if (fEventPool && fEventPool->NeedInit())
148 if (fUseDefautProcess) {
149 AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
150 return AliMultiInputEventHandler::Notify(path);
152 AliDebug(AliLog::kDebug + 5, Form("->"));
156 //_____________________________________________________________________________
157 Bool_t AliMixInputEventHandler::BeginEvent(Long64_t entry)
160 // BeginEvent(Long64_t entry) is called for all mix input handlers
162 AliDebug(AliLog::kDebug + 5, Form("-> %lld", entry));
163 if (fUseDefautProcess) {
164 AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
165 AliMultiInputEventHandler::BeginEvent(entry);/* return GetEntry();*/
167 AliDebug(AliLog::kDebug + 5, Form("->"));
170 //_____________________________________________________________________________
171 Bool_t AliMixInputEventHandler::GetEntry()
174 // All mixed events are set
176 AliDebug(AliLog::kDebug + 5, Form("<-"));
181 // if buffer size is higher then 1
182 else if (fBufferSize > 1) {
185 // if mix number is higher then 0 and buffer size is 1
186 else if (fMixNumber > 0) {
187 MixEventsMoreTimesWithOneEvent();
189 AliWarning("Not supported Mixing !!!");
192 AliDebug(AliLog::kDebug + 5, Form("->"));
196 //_____________________________________________________________________________
197 Bool_t AliMixInputEventHandler::MixStd()
200 // Mix std - No event pool
202 AliDebug(AliLog::kDebug + 5, Form("<-"));
203 AliDebug(AliLog::kDebug + 1, "Mix method");
204 // get correct handler
205 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
206 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
207 AliInputEventHandler *inEvHMain = 0;
208 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
209 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
210 if (!inEvHMain) return kFALSE;
212 // check for PhysSelection
213 if (!IsEventCurrentSelected()) return kFALSE;
215 // return in case of 0 entry in full chain
216 if (!fEntryCounter) {
217 AliDebug(AliLog::kDebug + 3, Form("-> fEntryCounter == 0"));
218 // runs UserExecMix for all tasks, if needed
219 UserExecMixAllTasks(fEntryCounter, 1, fEntryCounter, -1, 0);
223 Int_t mixNum = fMixNumber;
225 if (fEntryCounter <= 2 * fMixNumber) mixNum = 2 * fMixNumber + 2;
228 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
231 AliMixInputHandlerInfo *mihi = 0;
232 Long64_t entryMix = 0, entryMixReal = 0;
234 for (counter = 0; counter < mixNum; counter++) {
235 entryMix = fEntryCounter - 1 - counter ;
236 AliDebug(AliLog::kDebug + 5, Form("Handler[%d] entryMix %lld ", counter, entryMix));
237 if (entryMix < 0) break;
238 entryMixReal = entryMix;
239 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(0);
240 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
242 AliError("te is null. this is error. tell to developer (#1)");
244 mihi->PrepareEntry(te, entryMix, (AliInputEventHandler*)InputEventHandler(0), fAnalysisType);
245 // runs UserExecMix for all tasks
247 UserExecMixAllTasks(fEntryCounter, 1, fEntryCounter, entryMixReal, fNumberMixed);
248 InputEventHandler(0)->FinishEvent();
251 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
252 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
253 AliDebug(AliLog::kDebug + 5, Form("->"));
254 AliDebug(AliLog::kDebug + 5, Form("->"));
258 //_____________________________________________________________________________
259 Bool_t AliMixInputEventHandler::MixBuffer()
262 // Mix in event buffer
264 AliDebug(AliLog::kDebug + 5, Form("<-"));
265 AliDebug(AliLog::kDebug + 1, "Mix method");
266 // get correct handler
267 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
268 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
269 AliInputEventHandler *inEvHMain = 0;
270 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
271 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
272 if (!inEvHMain) return kFALSE;
274 // check for PhysSelection
275 if (!IsEventCurrentSelected()) return kFALSE;
277 // find out zero chain entries
278 Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetTree()->GetEntries();
280 Long64_t currentMainEntry = inEvHMain->GetTree()->GetTree()->GetReadEntry() + zeroChainEntries;
282 if (fEventPool && inEvHMain) fEventPool->AddEntry(currentMainEntry, inEvHMain->GetEvent());
284 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
289 Int_t idEntryList = -1;
290 if (fEventPool) el = fEventPool->FindEntryList(inEvHMain->GetEvent(), idEntryList);
291 // return in case of 0 entry in full chain
292 if (!fEntryCounter) {
293 AliDebug(AliLog::kDebug + 3, Form("-> fEntryCounter == 0"));
294 // runs UserExecMix for all tasks, if needed
295 if (el) UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
296 else UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
300 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (el null) +++++++++++++++++++", fEntryCounter));
301 UserExecMixAllTasks(fEntryCounter, -1, fEntryCounter, -1, 0);
305 if (elNum < fBufferSize + 1) {
306 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
307 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (%lld) LESS THEN BUFFER +++++++++++++++++++", fEntryCounter, elNum));
311 AliMixInputHandlerInfo *mihi = 0;
312 Long64_t entryMix = 0, entryMixReal = 0;
314 AliInputEventHandler *eh = 0;
315 TObjArrayIter next(&fInputHandlers);
316 while ((eh = dynamic_cast<AliInputEventHandler *>(next()))) {
317 if (fEventPool && fEventPool->GetListOfEventCuts()->GetEntries() > 0) {
319 if (elNum >= fBufferSize) {
320 Long64_t entryInEntryList = elNum - 2 - counter;
321 if (entryInEntryList < 0) break;
322 entryMix = el->GetEntry(entryInEntryList);
325 AliDebug(AliLog::kDebug + 5, Form("Handler[%d] entryMix %lld ", counter, entryMix));
327 UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
330 entryMixReal = entryMix;
331 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(counter);
332 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
334 AliError("te is null. this is error. tell to developer (#1)");
336 AliDebug(AliLog::kDebug + 3, Form("Preparing InputEventHandler(%d)", counter));
337 mihi->PrepareEntry(te, entryMix, (AliInputEventHandler*)InputEventHandler(counter), fAnalysisType);
338 // runs UserExecMix for all tasks
339 UserExecMixAllTasks(fEntryCounter, idEntryList, fEntryCounter, entryMixReal, counter);
344 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
345 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
346 AliDebug(AliLog::kDebug + 5, Form("->"));
347 AliDebug(AliLog::kDebug + 5, Form("->"));
351 //_____________________________________________________________________________
352 Bool_t AliMixInputEventHandler::MixEventsMoreTimesWithOneEvent()
355 // Mix in history with one event in buffer
357 AliDebug(AliLog::kDebug + 5, "<-");
358 AliDebug(AliLog::kDebug + 1, "Mix method");
359 // get correct handler
360 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
361 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
362 AliInputEventHandler *inEvHMain = 0;
363 if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
364 else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
365 if (!inEvHMain) return kFALSE;
367 // check for PhysSelection
368 if (!IsEventCurrentSelected()) return kFALSE;
370 // find out zero chain entries
371 Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetTree()->GetEntries();
373 Long64_t currentMainEntry = inEvHMain->GetTree()->GetTree()->GetReadEntry() + zeroChainEntries;
374 if (fEventPool && inEvHMain) fEventPool->AddEntry(currentMainEntry, inEvHMain->GetEvent());
376 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
380 Int_t idEntryList = -1;
382 if (fEventPool) el = fEventPool->FindEntryList(inEvHMain->GetEvent(), idEntryList);
383 // return in case of 0 entry in full chain
384 if (!fEntryCounter) {
385 // runs UserExecMix for all tasks, if needed
386 if (el && fDoMixIfNotEnoughEvents) {
387 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
390 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
392 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (fEntryCounter=0, idEntryList=%d) +++++++++++++++++++", fEntryCounter, idEntryList));
397 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (el null, idEntryList=%d) +++++++++++++++++++", fEntryCounter, idEntryList));
398 UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
403 if (elNum < fBufferSize + 1) {
404 if (fDoMixIfNotEnoughEvents) {
405 // include main event in to counter in this case (so idEntryList>0)
406 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
408 // dont include it in main event counter (idEntryList = -1)
410 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
412 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED [FIRST ENTRY in el] (elnum=%lld, idEntryList=%d) +++++++++++++++++++", fEntryCounter, elNum, idEntryList));
415 if (!fDoMixIfNotEnoughEvents) {
416 if (elNum <= fMixNumber + 1) {
417 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
418 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (%lld) NOT ENOUGH EVENTS TO MIX => NEED=%d +++++++++++++++++++", fEntryCounter, elNum, fMixNumber + 1));
424 Int_t mixNum = fMixNumber;
426 if (elNum <= 2 * fMixNumber + 1) mixNum = elNum + 1;
428 AliMixInputHandlerInfo *mihi = 0;
429 Long64_t entryMix = 0, entryMixReal = 0;
431 mihi = (AliMixInputHandlerInfo *) fMixTrees.At(0);
432 // fills num for main events
433 for (counter = 0; counter < mixNum; counter++) {
434 Long64_t entryInEntryList = elNum - 2 - counter;
435 AliDebug(AliLog::kDebug + 3, Form("entryInEntryList=%lld", entryInEntryList));
436 if (entryInEntryList < 0) break;
437 entryMix = el->GetEntry(entryInEntryList);
438 AliDebug(AliLog::kDebug + 3, Form("entryMix=%lld", entryMix));
439 if (entryMix < 0) break;
440 entryMixReal = entryMix;
441 TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
443 AliError("te is null. this is error. tell to developer (#2)");
445 mihi->PrepareEntry(te, entryMix, (AliInputEventHandler*)InputEventHandler(0), fAnalysisType);
446 // runs UserExecMix for all tasks
448 UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, entryMixReal, fNumberMixed);
449 InputEventHandler(0)->FinishEvent();
452 AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
453 AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
454 AliDebug(AliLog::kDebug + 5, Form("->"));
455 AliDebug(AliLog::kDebug + 5, Form("->"));
459 //_____________________________________________________________________________
460 Bool_t AliMixInputEventHandler::MixEventsMoreTimesWithBuffer()
463 // Mix more events in buffer with mixing with history
465 AliWarning("Not implemented");
469 //_____________________________________________________________________________
470 Bool_t AliMixInputEventHandler::FinishEvent()
473 // FinishEvent() is called for all mix input handlers
475 AliDebug(AliLog::kDebug + 5, Form("<-"));
476 AliMultiInputEventHandler::FinishEvent();
478 AliDebug(AliLog::kDebug + 5, Form("->"));
482 //_____________________________________________________________________________
483 void AliMixInputEventHandler::AddInputEventHandler(AliVEventHandler*)
486 // AddInputEventHandler will not be used
488 AliWarning("Function AddInputEventHandler is disabled for AliMixEventInputHandler !!!");
489 AliWarning("Use AliMixEventInputHandler::SetInputHandlerForMixing instead. Exiting ...");
492 //_____________________________________________________________________________
493 void AliMixInputEventHandler::UserExecMixAllTasks(Long64_t entryCounter, Int_t idEntryList, Long64_t entryMainReal, Long64_t entryMixReal, Int_t numMixed)
496 // Execute all task and sets mixing parameters
498 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
499 AliAnalysisTaskSE *mixTask = 0;
500 TObjArrayIter next(mgr->GetTasks());
501 while ((mixTask = (AliAnalysisTaskSE *) next())) {
502 if (dynamic_cast<AliAnalysisTaskSE *>(mixTask)) {
503 AliDebug(AliLog::kDebug, Form("%s %lld %d [%lld,%lld] %d", mixTask->GetName(), entryCounter, numMixed, entryMainReal, entryMixReal, idEntryList));
504 fCurrentEntry = entryCounter;
505 fCurrentEntryMain = entryMainReal;
506 fCurrentEntryMix = entryMixReal;
507 fCurrentBinIndex = idEntryList;
508 if (entryMixReal >= 0) mixTask->UserExecMix("");
513 //_____________________________________________________________________________
514 void AliMixInputEventHandler::SetMixNumber(const Int_t mixNum)
519 if (fMixNumber > 1 && fBufferSize > 1) {
520 AliWarning("Sleeping 10 sec to show Warning Message ...");
521 AliWarning("=========================================================================================");
522 AliWarning(Form("BufferSize(%d) higher > 1 and fMixNumber(%d) > 1, which is not supported", fBufferSize, mixNum));
524 AliWarning("\tBufferSize will be set to 1");
526 AliWarning("Hints:");
528 AliWarning("\t1.If you want to use buffer do:");
529 AliWarning(Form("\t\tAliMixInputEventHandler *mixH = new AliMixInputEventHandler(%d,1)", fBufferSize));
531 AliWarning("\t2.If you want to use mix more time with buffer size 1, then do:");
532 AliWarning(Form("\t\tAliMixInputEventHandler *mixH = new AliMixInputEventHandler(1,%d)", mixNum));
534 AliWarning("=========================================================================================");
535 gSystem->Sleep(10000);
541 Bool_t AliMixInputEventHandler::IsEventCurrentSelected()
543 AliDebug(AliLog::kDebug + 5, Form("<-"));
544 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
545 AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
546 Bool_t isSelected = kTRUE;
548 if (fOfflineTriggerMask && mh->GetEventSelection()) {
549 isSelected = fOfflineTriggerMask & mh->IsEventSelected();
552 AliDebug(AliLog::kDebug + 1, Form("isSelected=%d", isSelected));
553 AliDebug(AliLog::kDebug + 5, Form("-> %d", isSelected));