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