]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/EventMixing/AliMixInputEventHandler.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / ANALYSIS / EventMixing / AliMixInputEventHandler.cxx
1 //
2 // Class AliMixEventInputHandler
3 //
4 // Mixing input handler prepare N events before UserExec
5 // TODO example
6 // author:
7 //        Martin Vala (martin.vala@cern.ch)
8 //
9
10 #include <TFile.h>
11 #include <TChain.h>
12 #include <TChainElement.h>
13 #include <TSystem.h>
14
15 #include "AliLog.h"
16 #include "AliAnalysisManager.h"
17 #include "AliInputEventHandler.h"
18
19 #include "AliMixEventPool.h"
20 #include "AliMixInputEventHandler.h"
21 #include "AliMixInputHandlerInfo.h"
22
23 #include "AliAnalysisTaskSE.h"
24
25 ClassImp(AliMixInputEventHandler)
26
27 //_____________________________________________________________________________
28 AliMixInputEventHandler::AliMixInputEventHandler(const Int_t size, const Int_t mixNum): AliMultiInputEventHandler(size),
29    fMixTrees(),
30    fTreeMap(size > 0 ? size : 1),
31    fMixIntupHandlerInfoTmp(0),
32    fEntryCounter(0),
33    fEventPool(0),
34    fNumberMixed(0),
35    fMixNumber(mixNum),
36    fUseDefautProcess(kFALSE),
37    fDoMixExtra(kTRUE),
38    fDoMixIfNotEnoughEvents(kTRUE),
39    fDoMixEventGetEntryAuto(kTRUE),
40    fCurrentEntry(0),
41    fCurrentEntryMain(0),
42    fCurrentEntryMix(0),
43    fCurrentBinIndex(-1),
44    fOfflineTriggerMask(0),
45    fCurrentMixEntry(),
46    fCurrentEntryMainTree(0)
47 {
48    //
49    // Default constructor.
50    //
51    AliDebug(AliLog::kDebug + 10, "<-");
52    SetMixNumber(mixNum);
53    AliDebug(AliLog::kDebug + 10, "->");
54 }
55
56 //_____________________________________________________________________________
57 AliMixInputEventHandler::~AliMixInputEventHandler() {
58    //
59    // Destructor
60    //
61    fMixTrees.Clear();
62 }
63
64 //_____________________________________________________________________________
65 void AliMixInputEventHandler::SetInputHandlerForMixing(const AliInputEventHandler *const inHandler)
66 {
67    //
68    // Create N (fBufferSize) copies of input handler
69    //
70    AliDebug(AliLog::kDebug + 5, Form("<-"));
71    fInputHandlers.Clear();
72    AliDebug(AliLog::kDebug + 5, Form("Creating %d input event handlers ...", fBufferSize));
73    for (Int_t i = 0; i < fBufferSize; i++) {
74       AliDebug(AliLog::kDebug + 5, Form("Adding %d ...", i));
75       fInputHandlers.Add((AliInputEventHandler *) inHandler->Clone());
76    }
77
78    AliDebug(AliLog::kDebug + 5, Form("->"));
79 }
80
81 //_____________________________________________________________________________
82 Bool_t AliMixInputEventHandler::Init(TTree *tree, Option_t *opt)
83 {
84    //
85    // Init(const char*path) is called for all mix input handlers.
86    // Create event pool if needed
87    //
88    AliDebug(AliLog::kDebug + 5, Form("<- %p %s", (void *)tree, opt));
89    fAnalysisType = opt;
90    if (!tree) {
91       AliDebug(AliLog::kDebug + 5, Form("->"));
92       return kFALSE;
93    }
94
95    if (!fDoMixIfNotEnoughEvents&&fDoMixExtra) {
96       fDoMixExtra = kFALSE;
97       AliWarning("fDoMixIfNotEnoughEvents=kFALSE -> setting fDoMixExtra=kFALSE");
98    }
99
100    // clears array of input handlers
101    fMixTrees.Delete();
102    // create AliMixInputHandlerInfo
103    if (!fMixIntupHandlerInfoTmp) {
104       // loads first file TChain (tree)
105       tree->LoadTree(0);
106       fMixIntupHandlerInfoTmp = new AliMixInputHandlerInfo(tree->GetName());
107    }
108
109    AliInputEventHandler *ih = 0;
110    for (Int_t i = 0; i < fInputHandlers.GetEntries(); i++) {
111       ih = (AliInputEventHandler *) fInputHandlers.At(i);
112       ih->SetParentHandler(this);
113    }
114
115    AliDebug(AliLog::kDebug + 5, Form("->"));
116    return kTRUE;
117 }
118
119 //_____________________________________________________________________________
120 Bool_t AliMixInputEventHandler::Notify()
121 {
122    //
123    // Notify() is called for all mix input handlers
124    //
125    AliDebug(AliLog::kDebug + 5, Form("<-"));
126    if (fUseDefautProcess) {
127       AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
128       return AliMultiInputEventHandler::Notify();
129    }
130    AliDebug(AliLog::kDebug + 5, Form("->"));
131    return kTRUE;
132 }
133
134 //_____________________________________________________________________________
135 Bool_t AliMixInputEventHandler::Notify(const char *path)
136 {
137    //
138    // Notify(const char*path) is called for all mix input handlers
139    //
140    AliDebug(AliLog::kDebug + 5, Form("<- %s", path));
141
142    Bool_t doPrepareEntry=kTRUE;
143    TString anType = fAnalysisType;
144
145    // in case of local doPrepareEntry only first time
146    if (anType.CompareTo("proof")) doPrepareEntry = (fMixIntupHandlerInfoTmp->GetChain()->GetEntries()<=0);
147
148    // adds current file
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       if (doPrepareEntry) mixIHI->PrepareEntry(che, -1, (AliInputEventHandler *)InputEventHandler(i), fAnalysisType);
157       AliDebug(AliLog::kDebug + 5, Form("chain[%d]->GetEntries() = %lld", i, mixIHI->GetChain()->GetEntries()));
158       fMixTrees.Add(mixIHI);
159    }
160    AliDebug(AliLog::kDebug + 5, Form("fEntryCounter=%lld", fEntryCounter));
161    if (fEventPool && fEventPool->NeedInit())
162       fEventPool->Init();
163    if (fUseDefautProcess) {
164       AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
165       return AliMultiInputEventHandler::Notify(path);
166    }
167    AliDebug(AliLog::kDebug + 5, Form("->"));
168    return kTRUE;
169 }
170
171 //_____________________________________________________________________________
172 Bool_t AliMixInputEventHandler::BeginEvent(Long64_t entry)
173 {
174    //
175    // BeginEvent(Long64_t entry) is called for all mix input handlers
176    //
177    fCurrentEntryMainTree = entry;
178
179    AliDebug(AliLog::kDebug + 5, Form("-> %lld", entry));
180    if (fUseDefautProcess) {
181       AliDebug(AliLog::kDebug, Form("-> SKIPPED"));
182       AliMultiInputEventHandler::BeginEvent(entry);/* return GetEntry();*/
183    }
184    AliDebug(AliLog::kDebug + 5, Form("->"));
185    return kTRUE;
186 }
187 //_____________________________________________________________________________
188 Bool_t AliMixInputEventHandler::GetEntry()
189 {
190    //
191    // All mixed events are set
192    //
193    AliDebug(AliLog::kDebug + 5, Form("<-"));
194
195    if (!fEventPool) {
196       MixStd();
197    }
198    // if buffer size is higher then 1
199    else if (fBufferSize > 1) {
200       MixBuffer();
201    }
202    // if mix number is higher then 0 and buffer size is 1
203    else if (fMixNumber > 0) {
204       MixEventsMoreTimesWithOneEvent();
205    } else {
206       AliWarning("Not supported Mixing !!!");
207    }
208
209    AliDebug(AliLog::kDebug + 5, Form("->"));
210    return kTRUE;
211 }
212
213 //_____________________________________________________________________________
214 Bool_t AliMixInputEventHandler::MixStd()
215 {
216    //
217    // Mix std - No event pool
218    //
219    AliDebug(AliLog::kDebug + 5, Form("<-"));
220    AliDebug(AliLog::kDebug + 1, "Mix method");
221    // get correct handler
222    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
223    AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
224    AliInputEventHandler *inEvHMain = 0;
225    if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
226    else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
227    if (!inEvHMain) return kFALSE;
228
229    // check for PhysSelection
230    if (!IsEventCurrentSelected()) return kFALSE;
231
232    // return in case of 0 entry in full chain
233    if (!fEntryCounter) {
234       AliDebug(AliLog::kDebug + 3, Form("-> fEntryCounter == 0"));
235       // runs UserExecMix for all tasks, if needed
236       UserExecMixAllTasks(fEntryCounter, 1, fEntryCounter, -1, 0);
237       return kTRUE;
238    }
239    // pre mix evetns
240    Int_t mixNum = fMixNumber;
241    if (fDoMixExtra) {
242       if (fEntryCounter <= 2 * fMixNumber) mixNum = 2 * fMixNumber + 2;
243    }
244    // start of
245    AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
246    // reset mix number
247    fNumberMixed = 0;
248    AliMixInputHandlerInfo *mihi = 0;
249    Long64_t entryMix = 0, entryMixReal = 0;
250    Int_t counter = 0;
251    for (counter = 0; counter < mixNum; counter++) {
252       entryMix = fEntryCounter - 1 - counter ;
253       AliDebug(AliLog::kDebug + 5, Form("Handler[%d] entryMix %lld ", counter, entryMix));
254       if (entryMix < 0) break;
255       entryMixReal = entryMix;
256       mihi = (AliMixInputHandlerInfo *) fMixTrees.At(0);
257       TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
258       if (!te) {
259          AliError("te is null. this is error. tell to developer (#1)");
260       } else {
261          if (fDoMixEventGetEntryAuto) mihi->PrepareEntry(te, entryMix, (AliInputEventHandler *)InputEventHandler(0), fAnalysisType);
262          // runs UserExecMix for all tasks
263          fNumberMixed++;
264          UserExecMixAllTasks(fEntryCounter, 1, fEntryCounter, entryMixReal, fNumberMixed);
265          InputEventHandler(0)->FinishEvent();
266       }
267    }
268    AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
269    AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
270    AliDebug(AliLog::kDebug + 5, Form("->"));
271    AliDebug(AliLog::kDebug + 5, Form("->"));
272    return kTRUE;
273 }
274
275 //_____________________________________________________________________________
276 Bool_t AliMixInputEventHandler::MixBuffer()
277 {
278    //
279    // Mix in event buffer
280    //
281    AliDebug(AliLog::kDebug + 5, Form("<-"));
282    AliDebug(AliLog::kDebug + 1, "Mix method");
283    // get correct handler
284    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
285    AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
286    AliInputEventHandler *inEvHMain = 0;
287    if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
288    else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
289    if (!inEvHMain) return kFALSE;
290
291    // check for PhysSelection
292    if (!IsEventCurrentSelected()) return kFALSE;
293
294    fCurrentMixEntry.Reset();
295
296    // find out zero chain entries
297    Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetTree()->GetEntries();
298    // fill entry
299    Long64_t currentMainEntry = inEvHMain->GetTree()->GetTree()->GetReadEntry() + zeroChainEntries;
300    // fills entry
301    if (fEventPool && inEvHMain) fEventPool->AddEntry(currentMainEntry, inEvHMain->GetEvent());
302    // start of
303    AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
304    // reset mix number
305    fNumberMixed = 0;
306    Long64_t elNum = 0;
307    TEntryList *el = 0;
308    Int_t idEntryList = -1;
309    if (fEventPool) el = fEventPool->FindEntryList(inEvHMain->GetEvent(), idEntryList);
310    // return in case of 0 entry in full chain
311    if (!fEntryCounter) {
312       AliDebug(AliLog::kDebug + 3, Form("-> fEntryCounter == 0"));
313       // runs UserExecMix for all tasks, if needed
314       if (el) UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
315       else UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
316       return kTRUE;
317    }
318    if (!el) {
319       AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (el null) +++++++++++++++++++", fEntryCounter));
320       UserExecMixAllTasks(fEntryCounter, -1, fEntryCounter, -1, 0);
321       return kTRUE;
322    } else {
323       elNum = el->GetN();
324       if (elNum < fBufferSize + 1) {
325          UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
326          AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (%lld) LESS THEN BUFFER +++++++++++++++++++", fEntryCounter, elNum));
327          return kTRUE;
328       }
329    }
330
331    AliMixInputHandlerInfo *mihi = 0;
332    Long64_t entryMix = 0, entryMixReal = 0;
333    Int_t counter = 0;
334    AliInputEventHandler *eh = 0;
335    TObjArrayIter next(&fInputHandlers);
336    while ((eh = dynamic_cast<AliInputEventHandler *>(next()))) {
337       if (fEventPool && fEventPool->GetListOfEventCuts()->GetEntries() > 0) {
338          entryMix = -1;
339          if (elNum >= fBufferSize) {
340             Long64_t entryInEntryList =  elNum - 2 - counter;
341             if (entryInEntryList < 0) break;
342             entryMix = el->GetEntry(entryInEntryList);
343          }
344       }
345       AliDebug(AliLog::kDebug + 5, Form("Handler[%d] entryMix %lld ", counter, entryMix));
346       if (entryMix < 0) {
347          UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
348          break;
349       }
350       entryMixReal = entryMix;
351       mihi = (AliMixInputHandlerInfo *) fMixTrees.At(counter);
352       TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
353       if (!te) {
354          AliError("te is null. this is error. tell to developer (#1)");
355       } else {
356          fCurrentMixEntry.Enter(entryMixReal);
357          AliDebug(AliLog::kDebug + 3, Form("Preparing InputEventHandler(%d)", counter));
358          if (fDoMixEventGetEntryAuto) mihi->PrepareEntry(te, entryMix, (AliInputEventHandler *)InputEventHandler(counter), fAnalysisType);
359          fNumberMixed++;
360       }
361       counter++;
362    }
363
364    if (fNumberMixed == fBufferSize) {
365       // runs UserExecMix for all tasks
366       UserExecMixAllTasks(fEntryCounter, idEntryList, fEntryCounter, entryMixReal, counter);
367    }
368
369    AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
370    AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
371    AliDebug(AliLog::kDebug + 5, Form("->"));
372    AliDebug(AliLog::kDebug + 5, Form("->"));
373    return kTRUE;
374 }
375
376 //_____________________________________________________________________________
377 Bool_t AliMixInputEventHandler::MixEventsMoreTimesWithOneEvent()
378 {
379    //
380    // Mix in history with one event in buffer
381    //
382    AliDebug(AliLog::kDebug + 5, "<-");
383    AliDebug(AliLog::kDebug + 1, "Mix method");
384    // get correct handler
385    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
386    AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
387    AliInputEventHandler *inEvHMain = 0;
388    if (mh) inEvHMain = dynamic_cast<AliInputEventHandler *>(mh->GetFirstInputEventHandler());
389    else inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
390    if (!inEvHMain) return kFALSE;
391
392    // check for PhysSelection
393    if (!IsEventCurrentSelected()) return kFALSE;
394
395    fCurrentMixEntry.Reset();
396
397    // find out zero chain entries
398    Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetTree()->GetEntries();
399    // fill entry
400    Long64_t currentMainEntry = inEvHMain->GetTree()->GetTree()->GetReadEntry() + zeroChainEntries;
401    if (fEventPool && inEvHMain) fEventPool->AddEntry(currentMainEntry, inEvHMain->GetEvent());
402    // start of
403    AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
404    // reset mix number
405    fNumberMixed = 0;
406    Long64_t elNum = 0;
407    Int_t idEntryList = -1;
408    TEntryList *el = 0;
409    if (fEventPool) el = fEventPool->FindEntryList(inEvHMain->GetEvent(), idEntryList);
410    // return in case of 0 entry in full chain
411    if (!fEntryCounter) {
412       // runs UserExecMix for all tasks, if needed
413       if (el && fDoMixIfNotEnoughEvents) {
414          UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
415       } else {
416          idEntryList = -1;
417          UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
418       }
419       AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (fEntryCounter=0, idEntryList=%d) +++++++++++++++++++", fEntryCounter, idEntryList));
420       return kTRUE;
421    }
422    if (!el) {
423       if (fEventPool) {
424          AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (el null, idEntryList=%d) +++++++++++++++++++", fEntryCounter, idEntryList));
425          UserExecMixAllTasks(fEntryCounter, -1, currentMainEntry, -1, 0);
426          return kTRUE;
427       }
428    } else {
429       elNum = el->GetN();
430       if (elNum < fBufferSize + 1) {
431          if (fDoMixIfNotEnoughEvents) {
432             // include main event in to counter in this case (so idEntryList>0)
433             UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
434          }  else {
435             // dont include it in main event counter (idEntryList = -1)
436             idEntryList = -1;
437             UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
438          }
439          AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED [FIRST ENTRY in el] (elnum=%lld, idEntryList=%d) +++++++++++++++++++", fEntryCounter, elNum, idEntryList));
440          return kTRUE;
441       }
442       if (!fDoMixIfNotEnoughEvents) {
443          if (elNum <= fMixNumber + 1) {
444             UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, -1, 0);
445             AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld SKIPPED (%lld) NOT ENOUGH EVENTS TO MIX => NEED=%d +++++++++++++++++++", fEntryCounter, elNum, fMixNumber + 1));
446             return kTRUE;
447          }
448       }
449    }
450    // pre mix evetns
451    Int_t mixNum = fMixNumber;
452    if (fDoMixExtra) {
453       if (elNum <= 2 * fMixNumber + 1) mixNum = elNum + 1;
454    }
455    AliMixInputHandlerInfo *mihi = 0;
456    Long64_t entryMix = 0, entryMixReal = 0;
457    Int_t counter = 0;
458    mihi = (AliMixInputHandlerInfo *) fMixTrees.At(0);
459    // fills num for main events
460    for (counter = 0; counter < mixNum; counter++) {
461       fCurrentMixEntry.Reset();
462       Long64_t entryInEntryList =  elNum - 2 - counter;
463       AliDebug(AliLog::kDebug + 3, Form("entryInEntryList=%lld", entryInEntryList));
464       if (entryInEntryList < 0) break;
465       entryMix = el->GetEntry(entryInEntryList);
466       AliDebug(AliLog::kDebug + 3, Form("entryMix=%lld", entryMix));
467       if (entryMix < 0) break;
468       entryMixReal = entryMix;
469       TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
470       if (!te) {
471          AliError("te is null. this is error. tell to developer (#2)");
472       } else {
473          fCurrentMixEntry.Enter(entryMixReal);
474          if (fDoMixEventGetEntryAuto) mihi->PrepareEntry(te, entryMix, (AliInputEventHandler *)InputEventHandler(0), fAnalysisType);
475          // runs UserExecMix for all tasks
476          fNumberMixed++;
477          UserExecMixAllTasks(fEntryCounter, idEntryList, currentMainEntry, entryMixReal, fNumberMixed);
478          InputEventHandler(0)->FinishEvent();
479       }
480    }
481    AliDebug(AliLog::kDebug + 3, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fNumberMixed));
482    AliDebug(AliLog::kDebug + 3, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
483    AliDebug(AliLog::kDebug + 5, Form("->"));
484    AliDebug(AliLog::kDebug + 5, Form("->"));
485    return kTRUE;
486 }
487
488 //_____________________________________________________________________________
489 Bool_t AliMixInputEventHandler::MixEventsMoreTimesWithBuffer()
490 {
491    //
492    // Mix more events in buffer with mixing with history
493    //
494    AliWarning("Not implemented");
495    return kFALSE;
496 }
497
498 //_____________________________________________________________________________
499 Bool_t AliMixInputEventHandler::FinishEvent()
500 {
501    //
502    // FinishEvent() is called for all mix input handlers
503    //
504    AliDebug(AliLog::kDebug + 5, Form("<-"));
505    AliMultiInputEventHandler::FinishEvent();
506    fEntryCounter++;
507    AliDebug(AliLog::kDebug + 5, Form("->"));
508    return kTRUE;
509 }
510
511 //_____________________________________________________________________________
512 void AliMixInputEventHandler::AddInputEventHandler(AliVEventHandler *)
513 {
514    //
515    // AddInputEventHandler will not be used
516    //
517    AliWarning("Function AddInputEventHandler is disabled for AliMixEventInputHandler !!!");
518    AliWarning("Use AliMixEventInputHandler::SetInputHandlerForMixing instead. Exiting ...");
519 }
520
521 //_____________________________________________________________________________
522 void AliMixInputEventHandler::UserExecMixAllTasks(Long64_t entryCounter, Int_t idEntryList, Long64_t entryMainReal, Long64_t entryMixReal, Int_t numMixed)
523 {
524    //
525    // Execute all task and sets mixing parameters
526    //
527    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
528    AliAnalysisTaskSE *mixTask = 0;
529    TObjArrayIter next(mgr->GetTasks());
530    while ((mixTask = dynamic_cast<AliAnalysisTaskSE *>(next()))) {
531       AliDebug(AliLog::kDebug, Form("%s %lld %d [%lld,%lld] %d", mixTask->GetName(), entryCounter, numMixed, entryMainReal, entryMixReal, idEntryList));
532       fCurrentEntry = entryCounter;
533       fCurrentEntryMain = entryMainReal;
534       fCurrentEntryMix = entryMixReal;
535       fCurrentBinIndex = idEntryList;
536       if (entryMixReal >= 0) mixTask->UserExecMix("");
537    }
538 }
539
540 //_____________________________________________________________________________
541 void AliMixInputEventHandler::SetMixNumber(const Int_t mixNum)
542 {
543    //
544    // Sets mix number
545    //
546    if (fMixNumber > 1 && fBufferSize > 1) {
547       AliWarning("Sleeping 10 sec to show Warning Message ...");
548       AliWarning("=========================================================================================");
549       AliWarning(Form("BufferSize(%d) higher > 1 and fMixNumber(%d) > 1, which is not supported", fBufferSize, mixNum));
550       AliWarning("");
551       AliWarning("\tBufferSize will be set to 1");
552       AliWarning("");
553       AliWarning("Hints:");
554       AliWarning("");
555       AliWarning("\t1.If you want to use buffer do:");
556       AliWarning(Form("\t\tAliMixInputEventHandler *mixH = new AliMixInputEventHandler(%d,1)", fBufferSize));
557       AliWarning("");
558       AliWarning("\t2.If you want to use mix more time with buffer size 1, then do:");
559       AliWarning(Form("\t\tAliMixInputEventHandler *mixH = new AliMixInputEventHandler(1,%d)", mixNum));
560       AliWarning("");
561       AliWarning("=========================================================================================");
562       gSystem->Sleep(10000);
563       fBufferSize = 1;
564    }
565    fMixNumber = mixNum;
566 }
567
568 //_____________________________________________________________________________
569 Bool_t AliMixInputEventHandler::IsEventCurrentSelected()
570 {
571    //
572    // Check if event is selected by Physics selection
573    //
574
575    AliDebug(AliLog::kDebug + 5, Form("<-"));
576    Bool_t isSelected = kTRUE;
577    if (fOfflineTriggerMask && fOfflineTriggerMask != AliVEvent::kAny) {
578       AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
579       AliMultiInputEventHandler *mh = dynamic_cast<AliMultiInputEventHandler *>(mgr->GetInputEventHandler());
580       if (mh) {
581          AliInputEventHandler *ih =  mh->GetFirstInputEventHandler();
582          isSelected = fOfflineTriggerMask & ih->IsEventSelected();
583       }
584    }
585    AliDebug(AliLog::kDebug + 1, Form("isSelected=%d", isSelected));
586    AliDebug(AliLog::kDebug + 5, Form("-> %d", isSelected));
587    return isSelected;
588 }
589
590 //_____________________________________________________________________________
591 Bool_t AliMixInputEventHandler::GetEntryMainEvent() {
592    //
593    // Calling GetEntry for main event in input handler (Should be used in UserExecMix() only)
594    //
595
596    AliInputEventHandler *ih = ((AliMultiInputEventHandler *)ParentHandler())->GetFirstInputEventHandler();
597    ih->GetTree()->GetEntry(fCurrentEntryMainTree);
598    return kTRUE;
599 }
600
601 //_____________________________________________________________________________
602 Bool_t AliMixInputEventHandler::GetEntryMixedEvent(Int_t id) {
603    //
604    // Calling GetEntry for mixed event in input handler with id
605    // (Should be used in UserExecMix() only)
606    //
607
608    AliMixInputHandlerInfo *mihi = (AliMixInputHandlerInfo *) fMixTrees.At(id);
609
610    Long64_t entryMix = fCurrentMixEntry.GetEntry(fCurrentMixEntry.GetN()-id-1);
611    if(entryMix<0) {
612       AliError(Form("GetEntryMixedEvent(%d) => entryMix<0 [1]",id));
613       return kFALSE;
614    }
615    TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
616    if (!te) {
617       AliError("te is null. this is error. tell to developer (#3)");
618       return kFALSE;
619    }
620    if(entryMix<0) {
621       AliError(Form("GetEntryMixedEvent(%d) => entryMix<0 [2]",id));
622       return kFALSE;
623    }
624    mihi->PrepareEntry(te, entryMix, (AliInputEventHandler *)InputEventHandler(id), fAnalysisType);
625
626    return kTRUE;
627 }