]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/EventMixing/AliMixEventPool.cxx
Updates for train running (L. Cunqueiro)
[u/mrichter/AliRoot.git] / ANALYSIS / EventMixing / AliMixEventPool.cxx
1 //
2 // Class AliMixEventPool
3 //
4 // AliMixEventPool is used to find
5 // similar events
6 //
7 // author:
8 //        Martin Vala (martin.vala@cern.ch)
9 //
10
11 #include <TEntryList.h>
12
13 #include "AliLog.h"
14 #include "AliMixEventCutObj.h"
15
16 #include "AliMixEventPool.h"
17
18 ClassImp(AliMixEventPool)
19
20 //_________________________________________________________________________________________________
21 AliMixEventPool::AliMixEventPool(const char *name, const char *title) : TNamed(name, title),
22    fListOfEntryList(),
23    fListOfEventCuts(),
24    fBinNumber(0),
25    fBufferSize(0),
26    fMixNumber(0)
27 {
28    //
29    // Default constructor.
30    //
31    AliDebug(AliLog::kDebug + 5, "<-");
32    AliDebug(AliLog::kDebug + 5, "->");
33 }
34 //_________________________________________________________________________________________________
35 AliMixEventPool::AliMixEventPool(const AliMixEventPool &obj) : TNamed(obj),
36    fListOfEntryList(obj.fListOfEntryList),
37    fListOfEventCuts(obj.fListOfEventCuts),
38    fBinNumber(obj.fBinNumber),
39    fBufferSize(obj.fBufferSize),
40    fMixNumber(obj.fMixNumber)
41 {
42    //
43    // Copy constructor
44    //
45    AliDebug(AliLog::kDebug + 5, "<-");
46    AliDebug(AliLog::kDebug + 5, "->");
47 }
48
49 //_________________________________________________________________________________________________
50 AliMixEventPool &AliMixEventPool::operator=(const AliMixEventPool &obj)
51 {
52    //
53    // Assigned operator
54    //
55    if (&obj != this) {
56       TNamed::operator=(obj);
57       fListOfEntryList = obj.fListOfEntryList;
58       fListOfEventCuts = obj.fListOfEventCuts;
59       fBinNumber = obj.fBinNumber;
60       fBufferSize = obj.fBufferSize;
61       fMixNumber = obj.fMixNumber;
62    }
63    return *this;
64 }
65
66
67 //_________________________________________________________________________________________________
68 AliMixEventPool::~AliMixEventPool()
69 {
70    //
71    // Destructor
72    //
73    AliDebug(AliLog::kDebug + 5, "<-");
74    AliDebug(AliLog::kDebug + 5, "->");
75 }
76 //_________________________________________________________________________________________________
77 void AliMixEventPool::AddCut(AliMixEventCutObj *cut)
78 {
79    //
80    // Adds cut
81    //
82    if (cut) fListOfEventCuts.Add(new AliMixEventCutObj(*cut));
83 }
84 //_________________________________________________________________________________________________
85 void AliMixEventPool::Print(const Option_t *option) const
86 {
87    //
88    // Prints usefull information
89    //
90    TObjArrayIter next(&fListOfEventCuts);
91    //   Int_t c=0;
92    AliMixEventCutObj *cut;
93    while ((cut = (AliMixEventCutObj *) next())) {
94       cut->Print(option);
95    }
96    AliDebug(AliLog::kDebug, Form("NumOfEntryList %d", fListOfEntryList.GetEntries()));
97    TEntryList *el;
98    for (Int_t i = 0; i < fListOfEntryList.GetEntries(); i++) {
99       el = (TEntryList *) fListOfEntryList.At(i);
100       AliDebug(AliLog::kDebug, Form("EntryList[%d] %lld", i, el->GetN()));
101    }
102 }
103 //_________________________________________________________________________________________________
104 Int_t AliMixEventPool::Init()
105 {
106    //
107    // Init event pool
108    //
109    AliDebug(AliLog::kDebug + 5, "<-");
110    CreateEntryListsRecursivly(fListOfEventCuts.GetEntries() - 1);
111    fBinNumber++;
112    AliDebug(AliLog::kDebug, Form("fBinnumber = %d", fBinNumber));
113    AddEntryList();
114    AliDebug(AliLog::kDebug + 5, "->");
115    return 0;
116 }
117
118 //_________________________________________________________________________________________________
119 void AliMixEventPool::CreateEntryListsRecursivly(Int_t index)
120 {
121    //
122    // Helper function which create entrylist recursivly
123    //
124    AliDebug(AliLog::kDebug + 5, "<-");
125    AliMixEventCutObj *cut;
126    if (index >= 0) {
127       AliDebug(AliLog::kDebug + 1, Form("index = %d", index));
128       cut = dynamic_cast<AliMixEventCutObj *>(fListOfEventCuts.At(index));
129       if (!cut) return;
130       cut->Reset();
131       while (cut->HasMore()) {
132          cut->AddStep();
133          CreateEntryListsRecursivly(index - 1);
134          if (cut->HasMore()) {
135             fBinNumber++;
136             AliDebug(AliLog::kDebug + 1, Form("fBinnumber = %d", fBinNumber));
137             AddEntryList();
138             //                 PrintCurrentCutIntervals();
139          }
140       }
141    }
142    AliDebug(AliLog::kDebug + 5, "->");
143 }
144
145 //_________________________________________________________________________________________________
146 TEntryList *AliMixEventPool::AddEntryList()
147 {
148    //
149    // Adds endtry list
150    //
151    AliDebug(AliLog::kDebug + 5, "<-");
152    TObjArrayIter next(&fListOfEventCuts);
153    AliMixEventCutObj *cut;
154    while ((cut = (AliMixEventCutObj *) next())) {
155       if (cut) cut->PrintCurrentInterval();
156    }
157    TEntryList *el = new TEntryList;
158    fListOfEntryList.Add(el);
159    AliDebug(AliLog::kDebug + 1, Form("Number in Entry list -> %lld", el->GetN()));
160    AliDebug(AliLog::kDebug + 5, "->");
161    return el;
162 }
163
164 //_________________________________________________________________________________________________
165 Bool_t AliMixEventPool::AddEntry(Long64_t entry, AliVEvent *ev)
166 {
167    //
168    // Adds entry to correct entry list
169    //
170    AliDebug(AliLog::kDebug + 5, "<-");
171    AliDebug(AliLog::kDebug + 5, Form("AddEntry(%lld,%p)", entry, (void *)ev));
172    if (entry < 0) {
173       AliDebug(AliLog::kDebug, Form("Entry %lld was NOT added !!!", entry));
174       return kFALSE;
175    }
176    Int_t idEntryList = -1;
177    TEntryList *el =  FindEntryList(ev, idEntryList);
178    if (el) {
179       el->Enter(entry);
180       AliDebug(AliLog::kDebug, Form("Entry %lld was added with idEntryList %d !!!", entry, idEntryList));
181       return kTRUE;
182    }
183    AliDebug(AliLog::kDebug, Form("Entry %lld was NOT added !!!", entry));
184    AliDebug(AliLog::kDebug + 5, "->");
185    return kFALSE;
186 }
187
188 //_________________________________________________________________________________________________
189 TEntryList *AliMixEventPool::FindEntryList(AliVEvent *ev, Int_t &idEntryList)
190 {
191    //
192    // Find entrlist in list of entrlist
193    //
194    AliDebug(AliLog::kDebug + 5, "<-");
195    Int_t num = fListOfEventCuts.GetEntriesFast();
196    if (num < 1) return 0;
197    Int_t *indexes = new Int_t[num] ;
198    Int_t *lenght = new Int_t[num];
199    Int_t i = 0;
200    TObjArrayIter next(&fListOfEventCuts);
201    AliMixEventCutObj *cut;
202    while ((cut = (AliMixEventCutObj *) next())) {
203       indexes[i] = cut->GetIndex(ev);
204       if (indexes[i] < 0) {
205          AliDebug(AliLog::kDebug, Form("idEntryList %d", -1));
206          delete [] indexes;
207          delete [] lenght;
208          return 0;
209       }
210       lenght[i] = cut->GetNumberOfBins();
211       AliDebug(AliLog::kDebug + 1, Form("indexes[%d] %d", i, indexes[i]));
212       i++;
213    }
214    idEntryList = 0;
215    SearchIndexRecursive(fListOfEventCuts.GetEntries() - 1, &indexes[0], &lenght[0], idEntryList);
216    AliDebug(AliLog::kDebug, Form("idEntryList %d", idEntryList - 1));
217    // index which start with 0 (idEntryList-1)
218    delete [] indexes;
219    delete [] lenght;
220    AliDebug(AliLog::kDebug + 5, "->");
221    return (TEntryList *) fListOfEntryList.At(idEntryList - 1);
222 }
223
224 //_________________________________________________________________________________________________
225 void AliMixEventPool::SearchIndexRecursive(Int_t num, Int_t *i, Int_t *d, Int_t &index)
226 {
227    //
228    // Search for index of entrylist
229    //
230    AliDebug(AliLog::kDebug + 5, "<-");
231    if (num > 0) {
232       index += (i[num] - 1) * d[num - 1];
233       SearchIndexRecursive(num - 1, i, d, index);
234    } else {
235       index += i[num];
236    }
237    AliDebug(AliLog::kDebug + 5, "->");
238 }
239
240 //_________________________________________________________________________________________________
241 Bool_t AliMixEventPool::SetCutValuesFromBinIndex(Int_t index)
242 {
243    //
244    // Sets cut value from bin index
245    //
246
247    Int_t numCuts = fListOfEventCuts.GetEntriesFast();
248    Int_t *lenght = new Int_t[numCuts];
249    Int_t *indexes = new Int_t[numCuts];
250    Long64_t timesNum = 1;
251    AliMixEventCutObj *cut;
252    Int_t i = 0, j = 0;
253    for (i = 0; i < numCuts; i++) {
254       cut = (AliMixEventCutObj *) fListOfEventCuts.At(i);
255       cut->Reset();
256       lenght[i] = cut->GetNumberOfBins();
257       indexes[i] = 1;
258       timesNum *= lenght[i];
259    }
260
261    if (index < 0 || index >= timesNum) {
262 //       AliError(Form("index=%d is out of range !!!", index));
263       delete [] lenght;
264       delete [] indexes;
265       return kFALSE;
266    }
267
268    Long64_t indexNum = index;
269    for (i = 0; i < numCuts; i++) {
270       timesNum = 1;
271       for (j = 0; j < numCuts - i - 1; j++) timesNum *= lenght[j];
272       indexNum /= timesNum;
273       indexes[numCuts - i - 1] = indexNum + 1;
274       index -= indexNum * timesNum;
275       indexNum = index;
276    }
277
278    for (i = 0; i < numCuts; i++) {
279       cut = (AliMixEventCutObj *) fListOfEventCuts.At(i);
280       for (j = 0; j < indexes[i]; j++) cut->AddStep();
281       cut->PrintCurrentInterval();
282
283    }
284
285    for (i = 0; i < numCuts; i++) AliDebug(AliLog::kDebug, Form("indexes[%d]=%d", i, indexes[i]));
286
287    delete [] lenght;
288    delete [] indexes;
289
290    return kTRUE;
291 }