]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/RESONANCES/AliRsnPIDIndex.cxx
fix for bug #70582 (change from L. Molnar)
[u/mrichter/AliRoot.git] / PWG2 / RESONANCES / AliRsnPIDIndex.cxx
1 //
2 // Class AliRsnPIDIndex
3 //
4 // Sorts the indexes of all tracks in an AliRsnEvent
5 // for a fast retrieval of them according to charge and PID.
6 //
7 // authors: Martin Vala (martin.vala@cern.ch)
8 //          Alberto Pulvirenti (alberto.pulvirenti@ct.infn.it)
9 //
10
11 #include "AliLog.h"
12
13 #include "AliRsnEvent.h"
14 #include "AliRsnPIDIndex.h"
15
16 ClassImp(AliRsnPIDIndex)
17
18 //_____________________________________________________________________________
19 AliRsnPIDIndex::AliRsnPIDIndex(Int_t num)
20 {
21 //
22 // Default constructor.
23 // Reset all arrays to the size specified in the argument (default = 1000).
24 //
25
26   Int_t imethod, icharge, ipid, i;
27
28   for (imethod = 0; imethod < AliRsnDaughter::kMethods; imethod++)
29   {
30     for (icharge = 0; icharge < 2; icharge++)
31     {
32       for (ipid = 0; ipid <= AliPID::kSPECIES; ipid++)
33       {
34         fNumOfIndex[imethod][icharge][ipid] = 0;
35         fIndex[imethod][icharge][ipid].Set(num);
36         for (i = 0; i < num; i++) fIndex[imethod][icharge][ipid].AddAt(-1, i);
37       }
38     }
39   }
40 }
41
42 //_____________________________________________________________________________
43 AliRsnPIDIndex::AliRsnPIDIndex(const AliRsnPIDIndex & copy) :
44   TObject(copy)
45 {
46 //
47 // Copy constructor.
48 // Duplicates all arrays.
49 //
50
51   Int_t imethod, icharge, ipid, i, size;
52
53   for (imethod = 0; imethod < AliRsnDaughter::kMethods; imethod++)
54   {
55     for (icharge = 0; icharge < 2; icharge++)
56     {
57       for (ipid = 0; ipid <= AliPID::kSPECIES; ipid++)
58       {
59         fNumOfIndex[imethod][icharge][ipid] = copy.fNumOfIndex[imethod][icharge][ipid];
60         size = copy.fIndex[imethod][icharge][ipid].GetSize();
61         fIndex[imethod][icharge][ipid].Set(size);
62         for (i = 0; i < size; i++) {
63           fIndex[imethod][icharge][ipid].AddAt(copy.fIndex[imethod][icharge][ipid].At(i), i);
64         }
65       }
66     }
67   }
68 }
69
70 //_____________________________________________________________________________
71 AliRsnPIDIndex& AliRsnPIDIndex::operator= (const AliRsnPIDIndex & copy)
72 {
73 //
74 // Assignment operator.
75 // Duplicates all arrays.
76 //
77
78   Int_t imethod, icharge, ipid, k, size;
79
80   for (imethod = 0; imethod < AliRsnDaughter::kMethods; imethod++)
81   {
82     for (icharge = 0; icharge < 2; icharge++)
83     {
84       for (ipid = 0; ipid <= AliPID::kSPECIES; ipid++)
85       {
86         fNumOfIndex[imethod][icharge][ipid] = copy.fNumOfIndex[imethod][icharge][ipid];
87         size = copy.fIndex[imethod][icharge][ipid].GetSize();
88         fIndex[imethod][icharge][ipid].Set(size);
89         for (k = 0; k < size; k++) {
90           fIndex[imethod][icharge][ipid].AddAt(copy.fIndex[imethod][icharge][ipid].At(k), k);
91         }
92       }
93     }
94   }
95
96   // return this object
97   return (*this);
98 }
99
100 //_____________________________________________________________________________
101 AliRsnPIDIndex::~AliRsnPIDIndex()
102 {
103 //
104 // Destructor.
105 // Does nothing.
106 //
107 }
108
109 //_____________________________________________________________________________
110 void AliRsnPIDIndex::ResetAll(Int_t num)
111 {
112 //
113 // Resets all arrays to a given size, and storing '-1' in all values
114 //
115
116   Int_t imethod, icharge, ipid, i;
117
118   for (imethod = 0; imethod < AliRsnDaughter::kMethods; imethod++)
119   {
120     for (icharge = 0; icharge < 2; icharge++)
121     {
122       for (ipid = 0; ipid <= AliPID::kSPECIES; ipid++)
123       {
124         fNumOfIndex[imethod][icharge][ipid] = 0;
125         fIndex[imethod][icharge][ipid].Set(num);
126         for (i = 0; i < num; i++) fIndex[imethod][icharge][ipid].AddAt(-1, i);
127       }
128     }
129   }
130 }
131
132 //_____________________________________________________________________________
133 void AliRsnPIDIndex::Print(Option_t* /*option*/) const
134 {
135 //
136 // Prints AliRsnPIDIndex info
137 //
138
139   Int_t i, j, l;
140
141   for (l = 0; l < AliRsnDaughter::kMethods; l++) {
142     for (i = 0; i < 2; i++) {
143       for (j = 0; j <= AliPID::kSPECIES; j++) {
144         AliInfo(Form(" [%d][%d][%d] %d %d",l, i, j, fIndex[l][i][j].GetSize(), fNumOfIndex[l][i][j]));
145       }
146     }
147   }
148 }
149
150 //_____________________________________________________________________________
151 void AliRsnPIDIndex::AddIndex
152 (const Int_t index, AliRsnDaughter::EPIDMethod meth, Char_t sign, AliPID::EParticleType type)
153 {
154 //
155 // Adds one index (1st arg) to the corresponding array defined by:
156 // - PID method (2nd arg)
157 // - charge     (3rd arg)
158 // - pid type   (4th arg)
159 //
160
161   Int_t iMethod = (Int_t)meth;
162   Int_t iCharge = ChargeIndex(sign);
163   Int_t iType   = (Int_t)type;
164
165   // since AliPID kUnknown = 10 and kSPECIES = 5, the PID is "unknown"
166   // a correction in the storage index must be done
167   if (type == AliPID::kUnknown) iType = (Int_t)AliPID::kSPECIES;
168
169   // debug message (can this be removed?)
170   AliDebug(AliLog::kDebug+1,Form("Adding index=%d method=%d sign='%c', pidType=%d",index,meth,sign,type));
171
172   // check indexes before storing
173   if (iMethod < 0 || iMethod >= AliRsnDaughter::kMethods) return;
174   if (iCharge < 0 || iCharge > 1) return;
175   if (iType < 0 || iType > AliPID::kSPECIES) return;
176
177   // debug to know if adding was successful
178   AliDebug(AliLog::kDebug+1,Form("Adding succeeded for index=%d method=%d sign='%c', pidType=%d",index,meth,sign,type));
179
180   // insert the index in the array and increment the counter of occupied slots
181   fIndex[iMethod][iCharge][iType].AddAt(index, fNumOfIndex[iMethod][iCharge][iType]);
182   fNumOfIndex[iMethod][iCharge][iType]++;
183 }
184
185 //_____________________________________________________________________________
186 void AliRsnPIDIndex::SetCorrectIndexSize()
187 {
188 //
189 // Removes in every array the unused slots, to compress its size.
190 //
191
192   Int_t i, j, l;
193   for (l = 0; l < AliRsnDaughter::kMethods; l++) {
194     for (i = 0; i < 2; i++) {
195       for (j = 0; j <= AliPID::kSPECIES; j++) {
196         fIndex[l][i][j].Set(fNumOfIndex[l][i][j]);
197       }
198     }
199   }
200 }
201
202 //_____________________________________________________________________________
203 TArrayI* AliRsnPIDIndex::GetTracksArray
204 (AliRsnDaughter::EPIDMethod meth, Char_t sign, AliPID::EParticleType type)
205 {
206 //
207 // Returns the array of indexes of tracks whose charge
208 // and PID correspond to the passed arguments:
209 //   1) PID method
210 //   2) sign of particle ('+' or '-')
211 //   3) PID of particle (from AliRsnPID::EType)
212 // Otherwise returns null pointer.
213 //
214
215   Int_t iMethod = (Int_t)meth;
216   Int_t iCharge = ChargeIndex(sign);
217   Int_t iType   = (Int_t)type;
218
219   // since AliPID kUnknown = 10 and kSPECIES = 5, the PID is "unknown"
220   // a correction in the storage index must be done
221   if (type == AliPID::kUnknown) iType = (Int_t)AliPID::kSPECIES;
222
223   // check indexes
224   if (iMethod < 0 || iMethod >= AliRsnDaughter::kMethods)
225   {
226     AliError("Unrecognized PID method");
227     return 0x0;
228   }
229   if (iCharge < 0 || iCharge > 1)
230   {
231     AliError(Form("Charge index (%d) out of range", iCharge));
232     return 0x0;
233   }
234   if (iType < 0 || iType > AliPID::kSPECIES)
235   {
236     AliError(Form("PID index(%d) out of range", iType));
237     return 0x0;
238   }
239
240   return &fIndex[iMethod][iCharge][iType];
241 }
242
243 //_____________________________________________________________________________
244 TArrayI* AliRsnPIDIndex::GetCharged(Char_t sign)
245 {
246 //
247 // Returns the array of indexes of tracks whose charge
248 // corresponds to the passed argument
249 // Otherwise returns a null pointer.
250 //
251
252   return GetTracksArray(AliRsnDaughter::kNoPID, sign, AliPID::kUnknown);
253 }
254
255 //_____________________________________________________________________________
256 Int_t AliRsnPIDIndex::ChargeIndex(Char_t sign) const
257 {
258 //
259 // Returns the array index corresponding to charge.
260 // The inverse operation (returning '+' or '-' depending on charge)
261 // is done by 'AliRsnDaughter::GetChargeC()' method.
262 //
263
264   if (sign == '+') return 0;
265   else if (sign == '-') return 1;
266   else if (sign == '0') return 2;
267   else {
268     AliError(Form("Character '%c' not recognized as charge sign", sign));
269     return -1;
270   }
271 }
272
273 //_____________________________________________________________________________
274 void AliRsnPIDIndex::FillFromEvent(AliRsnEvent* const event)
275 {
276 //
277 // Scans a whole event and fills the arrays depending
278 // on the identification of all tracks with all methods.
279 //
280
281   Int_t numOfTracks = event->GetMultiplicity();
282
283   // first, reset all arrays to the multiplicity,
284   // in order to avoid 'out of range' errors
285   ResetAll(numOfTracks);
286
287   // now loop on tracks using a unique AliRsnDaughter object
288   // which will point to one track at a time, and allows
289   // to recovery the informations to classify it
290   Int_t i;
291   AliRsnDaughter daughter;
292   for (i = 0; i < numOfTracks; i++)
293   {
294     // link the cursor to current track
295     // this method recoveries the true PID and
296     // combines weights with prior probabilities (defined in AliRsnEvent)
297     // to obtain realistic PID
298     event->SetDaughter(daughter, i);
299
300     // assign current index to right slot for NoPID (essentially the charge)
301     AddIndex(i,AliRsnDaughter::kNoPID,daughter.ChargeC(),AliPID::kUnknown);
302
303     // assign current index to right slot for realistic PID
304     AddIndex(i, AliRsnDaughter::kRealistic, daughter.ChargeC(), daughter.RealisticPID());
305
306     // if MC info is present, assign current index to right slot for true PID
307     if (daughter.GetParticle())
308       AddIndex(i, AliRsnDaughter::kPerfect, daughter.ChargeC(), daughter.PerfectPID());
309   }
310
311   // at the end, compress all arrays to the correct size
312   SetCorrectIndexSize();
313 }