-update
[u/mrichter/AliRoot.git] / ANALYSIS / AliEventPoolManager.cxx
CommitLineData
2a910c25 1#include "AliEventPoolManager.h"
2
c82bb898 3using std::cout;
4using std::endl;
2a910c25 5ClassImp(AliEventPool)
6
7void AliEventPool::PrintInfo() const
8{
9 cout << Form("%20s: %d events", "Pool capacity", fMixDepth) << endl;
10 cout << Form("%20s: %d events, %d tracks", "Current size",
11 GetCurrentNEvents(), NTracksInPool()) << endl;
12 cout << Form("%20s: %.1f to %.1f", "Sub-event mult.", fMultMin, fMultMax) << endl;
13 cout << Form("%20s: %.1f to %.1f", "Z-vtx range", fZvtxMin, fZvtxMax) << endl;
14
15 return;
16}
17
18Bool_t AliEventPool::EventMatchesBin(Int_t mult, Double_t zvtx) const
19{
20 return EventMatchesBin((Double_t) mult, zvtx);
21}
22
23Bool_t AliEventPool::EventMatchesBin(Double_t mult, Double_t zvtx) const
24{
25 // Lower bin limit included; upper limit excluded.
26
27 Bool_t multOK = (mult >= fMultMin && mult < fMultMax);
28 Bool_t zvtxOK = (zvtx >= fZvtxMin && zvtx < fZvtxMax);
29 return (multOK && zvtxOK);
30}
31
32Int_t AliEventPool::NTracksInPool() const
33{
34 // Number of tracks for this cent, zvtx bin; possibly includes many
35 // events.
36
37 Int_t ntrk=0;
38 for (Int_t i=0; i<(Int_t)fEvents.size(); ++i) {
39 ntrk += fNTracksInEvent.at(i);
40 }
41 return ntrk;
42}
43
44Int_t AliEventPool::SetEventMultRange(Int_t multMin, Int_t multMax)
45{
46 fMultMin = (Double_t)multMin;
47 fMultMax = (Double_t)multMax;
48 return 0;
49}
50
51Int_t AliEventPool::SetEventMultRange(Double_t multMin, Double_t multMax)
52{
53 fMultMin = multMin;
54 fMultMax = multMax;
55 return 0;
56}
57
58Int_t AliEventPool::SetEventZvtxRange(Double_t zvtxMin, Double_t zvtxMax)
59{
60 fZvtxMin = zvtxMin;
61 fZvtxMax = zvtxMax;
62 return 0;
63}
64
65Int_t AliEventPool::GlobalEventIndex(Int_t j) const
66{
67 // Index returned from passing local pool event index.
68
69 if (j < 0 || j >= (Int_t)fEventIndex.size()) {
70 cout << "ERROR in AliEventPool::GlobalEventIndex(): "
71 << " Invalid index " << j << endl;
72 return -99;
73 }
74 return fEventIndex.at(j);
75}
76
77Int_t AliEventPool::UpdatePool(TObjArray *trk)
78{
79 // A rolling buffer (a double-ended queue) is updated by removing
80 // the oldest event, and appending the newest.
c05ff6be 81 //
82 // the ownership of <trk> is delegated to this class
2a910c25 83
84 static Int_t iEvent = -1;
85 iEvent++;
86
87 Int_t mult = trk->GetEntries();
fa67180e 88 Int_t nTrk = NTracksInPool();
2a910c25 89
fa67180e 90 if (nTrk < fTargetTrackDepth && ((nTrk + mult) >= fTargetTrackDepth))
2a910c25 91 fNTimes++;
fa67180e 92
2a910c25 93 // remove 0th element before appending this event
fa67180e 94 Bool_t removeFirstEvent = 0;
95 if (nTrk>fTargetTrackDepth) {
96 Int_t nTrksFirstEvent= fNTracksInEvent.front();
97 Int_t diff = nTrk - nTrksFirstEvent + mult;
98 if (diff>fTargetTrackDepth)
99 removeFirstEvent = 1;
100 }
101 if (removeFirstEvent) {
2a910c25 102 TObjArray *fa = fEvents.front();
103 delete fa;
104 fEvents.pop_front(); // remove first track array
105 fNTracksInEvent.pop_front(); // remove first int
106 fEventIndex.pop_front();
2a910c25 107 }
108
109 fNTracksInEvent.push_back(mult);
110 fEvents.push_back(trk);
111 fEventIndex.push_back(iEvent);
2a910c25 112
113 if (fNTimes==1) {
fa67180e 114 fFirstFilled = kTRUE;
db48ae1a 115 if (AliEventPool::fDebug) {
d94cc49b 116 cout << "\nPool " << MultBinIndex() << ", " << ZvtxBinIndex()
117 << " ready at event "<< iEvent;
118 PrintInfo();
119 cout << endl;
120 }
2a910c25 121 fNTimes++; // See this message exactly once/pool
fa67180e 122 } else {
123 fFirstFilled = kFALSE;
2a910c25 124 }
125
126 fWasUpdated = true;
127
128 if (AliEventPool::fDebug) {
129 cout << " Event " << fEventIndex.back();
130 cout << " PoolDepth = " << GetCurrentNEvents();
131 cout << " NTracksInCurrentEvent = " << NTracksInCurrentEvent();
132 }
133
134 return fEvents.size();
135}
136
137TObject* AliEventPool::GetRandomTrack() const
138{
139 // Get any random track from the pool, sampled with uniform probability.
140
141 UInt_t ranEvt = gRandom->Integer(fEvents.size());
142 TObjArray *tca = fEvents.at(ranEvt);
143 UInt_t ranTrk = gRandom->Integer(tca->GetEntries());
144 TObject *trk = (TObject*)tca->At(ranTrk);
145 return trk;
146}
147
148TObjArray* AliEventPool::GetEvent(Int_t i) const
149{
150 if (i<0 || i>=(Int_t)fEvents.size()) {
151 cout << "AliEventPool::GetEvent("
152 << i << "): Invalid index" << endl;
153 return 0x0;
154 }
155
156 TObjArray *tca = fEvents.at(i);
157 return tca;
158}
159
160TObjArray* AliEventPool::GetRandomEvent() const
161{
162 UInt_t ranEvt = gRandom->Integer(fEvents.size());
163 TObjArray *tca = fEvents.at(ranEvt);
164 return tca;
165}
166
167Int_t AliEventPool::NTracksInEvent(Int_t iEvent) const
168{
169 // Return number of tracks in iEvent, which is the local pool index.
170
171 Int_t n = -1;
172 Int_t curEvent = fEventIndex.back();
173 Int_t offset = curEvent - iEvent;
174 Int_t pos = fEventIndex.size() - offset - 1;
175
176 if (offset==0)
177 n = fNTracksInEvent.back();
178 else if (offset < 0 || iEvent < 0) {
179 n = 0;
180 }
181 else if (offset > 0 && offset <= (int)fEventIndex.size()) {
182 n = fNTracksInEvent.at(pos);
183 }
184 else
185 cout << "Event info no longer in memory" << endl;
186 return n;
187}
188
189ClassImp(AliEventPoolManager)
190
191AliEventPoolManager::AliEventPoolManager(Int_t depth, Int_t minNTracks,
192 Int_t nMultBins, Double_t *multbins,
193 Int_t nZvtxBins, Double_t *zvtxbins) :
194fDebug(0), fNMultBins(0), fNZvtxBins(0), fEvPool(0), fTargetTrackDepth(minNTracks)
195{
196 // Constructor.
197
198 InitEventPools(depth, nMultBins, multbins, nZvtxBins, zvtxbins);
199 cout << "AliEventPoolManager initialized." << endl;
200}
201
202Int_t AliEventPoolManager::InitEventPools(Int_t depth,
203 Int_t nMultBins, Double_t *multbin,
204 Int_t nZvtxBins, Double_t *zvtxbin)
205{
206 // Assign AliEventPoolManager members.
207
208 fNMultBins = nMultBins;
209 fNZvtxBins = nZvtxBins;
210
211 for (Int_t iM=0; iM<nMultBins; iM++) {
212 std::vector<AliEventPool*> evp;
213 for (Int_t iZ=0; iZ<nZvtxBins; iZ++) {
214 evp.push_back(new AliEventPool(depth,
215 multbin[iM], multbin[iM+1],
216 zvtxbin[iZ], zvtxbin[iZ+1] ));
217 }
218 fEvPool.push_back(evp);
219 }
220
221 for (Int_t iM=0; iM<nMultBins; iM++) {
222 for (Int_t iZ=0; iZ<nZvtxBins; iZ++) {
223 fEvPool.at(iM).at(iZ)->SetMultBinIndex(iM);
224 fEvPool.at(iM).at(iZ)->SetZvtxBinIndex(iZ);
225 fEvPool.at(iM).at(iZ)->SetTargetTrackDepth(fTargetTrackDepth);
226 }
227 }
228
229 if (0) {
230 cout << "fEvPool outer size: " << fEvPool.size() << endl;
231 for (Int_t iM=0; iM<nMultBins; iM++) {
232 for (Int_t iZ=0; iZ<nZvtxBins; iZ++) {
233 if(fEvPool.at(iM).at(iZ)) {
234 cout << "multiplicity bin: " << iM;
235 cout << ", z-vertex bin: " << iZ;
236 fEvPool.at(iM).at(iZ)->PrintInfo();
237 }
238 }
239 }
240 }
241
242 return fEvPool.size();
243}
244
245AliEventPool *AliEventPoolManager::GetEventPool(Int_t iMult, Int_t iZvtx) const
246{
247 if (iMult < 0 || iMult >= fNMultBins)
248 return 0x0;
249 if (iZvtx < 0 || iZvtx >= fNZvtxBins)
250 return 0x0;
251 return fEvPool.at(iMult).at(iZvtx);
252}
253
254AliEventPool *AliEventPoolManager::GetEventPool(Int_t centVal, Double_t zVtxVal) const
255{
256 return GetEventPool((Double_t)centVal, zVtxVal);
257}
258
259AliEventPool *AliEventPoolManager::GetEventPool(Double_t centVal, Double_t zVtxVal) const
260{
261 // Return appropriate pool for this centrality and z-vertex value.
262
263 for (Int_t iM=0; iM<fNMultBins; iM++) {
264 for (Int_t iZ=0; iZ<fNZvtxBins; iZ++) {
265 AliEventPool* pool = GetEventPool(iM, iZ);
266 if (pool->EventMatchesBin(centVal, zVtxVal))
267 return pool;
268 }
269 }
270 return 0x0;
271}
272
273Int_t AliEventPoolManager::UpdatePools(TObjArray *trk)
274{
275 // Call UpdatePool for all bins.
276
277 for (Int_t iM=0; iM<fNMultBins; iM++) {
278 for (Int_t iZ=0; iZ<fNZvtxBins; iZ++) {
279 if (fEvPool.at(iM).at(iZ)->UpdatePool(trk) > -1)
280 break;
281 }
282 }
283 return 0;
284}