]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVENTMIX/AliMixEventInputHandler.cxx
Split: Tender*, EventMixing out of ANALYSIS
[u/mrichter/AliRoot.git] / EVENTMIX / AliMixEventInputHandler.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 <TEntryList.h>
13 #include "AliLog.h"
14 #include "AliMixEventPool.h"
15 #include "AliMixEventInputHandler.h"
16 #include "AliAnalysisManager.h"
17
18 #include "AliMixInputHandlerInfo.h"
19 #include <TChainElement.h>
20
21 ClassImp(AliMixEventInputHandler)
22
23 //_____________________________________________________________________________
24 AliMixEventInputHandler::AliMixEventInputHandler(const Int_t size) :
25    AliInputEventHandler(),
26    fBufferSize(size),
27    fInputHandlers(),
28    fMixTrees(),
29    fTreeMap(size),
30    fMixIntupHandlerInfoTmp(0),
31    fEntryCounter(0),
32    fEventPool(0),
33    fMixEventNumber(0) {
34 //
35 // Default constructor.
36 //
37    AliDebug(AliLog::kDebug + 10, "<-");
38    AliDebug(AliLog::kDebug + 10, "->");
39 }
40
41 //_____________________________________________________________________________
42 AliInputEventHandler *AliMixEventInputHandler::InputEventHandler(const Int_t index) {
43    //
44    // Returns input handler
45    //
46    AliDebug(AliLog::kDebug, Form("<-"));
47    if ((index >= 0) && (index < fBufferSize)) {
48       AliDebug(AliLog::kDebug, Form("->"));
49       return (AliInputEventHandler *) fInputHandlers.At(index);
50    }
51    AliDebug(AliLog::kDebug, Form("->"));
52    return 0;
53 }
54 //_____________________________________________________________________________
55 void AliMixEventInputHandler::SetInputHandlerForMixing(const AliInputEventHandler *const inHandler) {
56    //
57    // Create N (fBufferSize) copies of input handler
58    //
59    AliDebug(AliLog::kDebug, Form("<-"));
60    AliDebug(AliLog::kDebug, Form("Creating %d input event handlers ...", fBufferSize));
61    for (Int_t i = 0; i < fBufferSize; i++) {
62       AliDebug(AliLog::kDebug + 5, Form("Adding %d ...", i));
63       fInputHandlers.Add((AliInputEventHandler *) inHandler->Clone());
64    }
65    AliDebug(AliLog::kDebug, Form("->"));
66 }
67 //_____________________________________________________________________________
68 Bool_t AliMixEventInputHandler::Init(Option_t *opt) {
69    //
70    // Init() is called for all mix input handlers.
71    //
72    AliDebug(AliLog::kDebug, Form("<- \"%s\"", opt));
73    AliInputEventHandler *eh = 0;
74    TObjArrayIter next(&fInputHandlers);
75    while ((eh = (AliInputEventHandler *) next())) {
76       eh->Init(opt);
77    }
78    AliDebug(AliLog::kDebug, Form("->"));
79    return kTRUE;
80 }
81 //_____________________________________________________________________________
82 Bool_t AliMixEventInputHandler::Init(TTree *tree, Option_t *) {
83    //
84    // Init(const char*path) is called for all mix input handlers.
85    // Create event pool if needed
86    //
87    AliDebug(AliLog::kDebug, Form("<- %p", tree));
88    if (!tree) {
89       AliDebug(AliLog::kDebug, Form("->"));
90       return kFALSE;
91    }
92    AliDebug(AliLog::kDebug, Form("%s", tree->GetCurrentFile()->GetName()));
93
94    // clears array of input handlers
95    fMixTrees.SetOwner(kTRUE);
96    fMixTrees.Clear();
97
98    // create AliMixInputHandlerInfo
99    if (!fMixIntupHandlerInfoTmp) fMixIntupHandlerInfoTmp = new AliMixInputHandlerInfo(tree->GetName());
100
101    // adds current file
102    fMixIntupHandlerInfoTmp->AddTreeToChain(tree);
103    Int_t lastIndex = fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->GetEntries();
104    TChainElement *che = (TChainElement *)fMixIntupHandlerInfoTmp->GetChain()->GetListOfFiles()->At(lastIndex - 1);
105    AliMixInputHandlerInfo *mixIHI = 0;
106    for (Int_t i = 0; i < fInputHandlers.GetEntries(); i++) {
107
108       AliDebug(AliLog::kDebug, Form("fInputHandlers[%d]", i));
109       mixIHI = new AliMixInputHandlerInfo(fMixIntupHandlerInfoTmp->GetName(), fMixIntupHandlerInfoTmp->GetTitle());
110       mixIHI->PrepareEntry(che, -1, InputEventHandler(i));
111       AliDebug(AliLog::kDebug, Form("chain[%d]->GetEntries() = %lld", i, mixIHI->GetChain()->GetEntries()));
112       fMixTrees.Add(mixIHI);
113    }
114    AliDebug(AliLog::kDebug, Form("fEntryCounter=%lld", fEntryCounter));
115
116    if (fEventPool->NeedInit())
117       fEventPool->Init();
118
119    AliDebug(AliLog::kDebug, Form("->"));
120    return kTRUE;
121 }
122 //_____________________________________________________________________________
123 Bool_t AliMixEventInputHandler::Notify() {
124    //
125    // Notify() is called for all mix input handlers
126    //
127    AliDebug(AliLog::kDebug, Form("<-"));
128    AliInputEventHandler *eh = 0;
129    TObjArrayIter next(&fInputHandlers);
130    while ((eh = (AliInputEventHandler *) next())) {
131       eh->Notify();
132    }
133    AliDebug(AliLog::kDebug, Form("->"));
134    return kTRUE;
135 }
136
137 //_____________________________________________________________________________
138 Bool_t AliMixEventInputHandler::Notify(const char *path) {
139    //
140    // Notify(const char*path) is called for all mix input handlers
141    //
142    AliDebug(AliLog::kDebug, Form("<- %s", path));
143    AliInputEventHandler *eh = 0;
144    TObjArrayIter next(&fInputHandlers);
145    while ((eh = (AliInputEventHandler *) next())) {
146       eh->Notify(path);
147    }
148    AliDebug(AliLog::kDebug, Form("->"));
149    return kTRUE;
150 }
151 //_____________________________________________________________________________
152 Bool_t AliMixEventInputHandler::BeginEvent(Long64_t entry) {
153    //
154    // BeginEvent(Long64_t entry) is called for all mix input handlers
155    //
156
157    AliDebug(AliLog::kDebug, Form("-> %lld", entry));
158    AliInputEventHandler *eh = 0;
159    TObjArrayIter next(&fInputHandlers);
160    while ((eh = (AliInputEventHandler *) next())) {
161       eh->BeginEvent(entry);
162    }
163    AliDebug(AliLog::kDebug, Form("->"));
164    return kTRUE;
165 }
166
167
168 //_____________________________________________________________________________
169 Bool_t AliMixEventInputHandler::GetEntry() {
170    //
171    // Sets correct events to every mix events
172    //
173    AliDebug(AliLog::kDebug, Form("<-"));
174
175    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
176    AliInputEventHandler *inEvHMain = dynamic_cast<AliInputEventHandler *>(mgr->GetInputEventHandler());
177
178    Long64_t zeroChainEntries = fMixIntupHandlerInfoTmp->GetChain()->GetEntries() - inEvHMain->GetTree()->GetEntries();
179
180    // if fEntryCounter is 0 just add entry
181    if (!fEntryCounter) {
182       if (inEvHMain) {
183
184          fEventPool->AddEntry(inEvHMain->GetTree()->GetReadEntry() + zeroChainEntries, inEvHMain->GetEvent());
185       }
186       return kTRUE;
187    }
188
189    AliDebug(AliLog::kDebug, Form("++++++++++++++ BEGIN SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
190
191    fMixEventNumber = 0;
192    TEntryList *el = fEventPool->FindEntryList(inEvHMain->GetEvent());
193    Long64_t elNum = 0;
194    if (el)
195       elNum = el->GetN();
196    else
197       AliDebug(AliLog::kDebug, "el is null");
198
199    AliInputEventHandler *eh = 0;
200    AliMixInputHandlerInfo *mihi = 0;
201    TObjArrayIter next(&fInputHandlers);
202    Int_t counter = 0;
203    Long64_t entryMix = 0;
204    while ((eh = (AliInputEventHandler *) next())) {
205       if (fEventPool->GetListOfEventCuts()->GetEntries() > 0) {
206          entryMix = -1;
207          if (el && el->GetN() >= fBufferSize) {
208             Long64_t entryInEntryList =  elNum - 1 - counter;
209             if (entryInEntryList < 0) break;
210             entryMix = el->GetEntry(entryInEntryList);
211          }
212       } else {
213          entryMix = fEntryCounter - 1 - counter ;
214       }
215
216       AliDebug(AliLog::kDebug, Form("Handler[%d] entryMix %lld ", counter, entryMix));
217       if (entryMix < 0) break;
218
219       mihi = (AliMixInputHandlerInfo *) fMixTrees.At(counter);
220       TChainElement *te = fMixIntupHandlerInfoTmp->GetEntryInTree(entryMix);
221       mihi->PrepareEntry(te, entryMix, InputEventHandler(counter));
222       fMixEventNumber++;
223       counter++;
224    }
225
226    if (inEvHMain) {
227       fEventPool->AddEntry(inEvHMain->GetTree()->GetReadEntry() + zeroChainEntries, inEvHMain->GetEvent());
228    }
229
230    AliDebug(AliLog::kDebug, Form("fEntryCounter=%lld fMixEventNumber=%d", fEntryCounter, fMixEventNumber));
231    AliDebug(AliLog::kDebug, Form("++++++++++++++ END SETUP EVENT %lld +++++++++++++++++++", fEntryCounter));
232    AliDebug(AliLog::kDebug, Form("->"));
233    return kTRUE;
234 }
235 //_____________________________________________________________________________
236 Bool_t AliMixEventInputHandler::FinishEvent() {
237    //
238    // FinishEvent() is called for all mix input handlers
239    //
240    AliDebug(AliLog::kDebug, Form("<-"));
241    AliInputEventHandler *eh = 0;
242    TObjArrayIter next(&fInputHandlers);
243    while ((eh = (AliInputEventHandler *) next())) {
244       eh->FinishEvent();
245    }
246    fEntryCounter++;
247    AliDebug(AliLog::kDebug, Form("->"));
248    return kTRUE;
249 }