]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliPID.cxx
Modification of the trigger fields in the event tag (Panos)
[u/mrichter/AliRoot.git] / STEER / AliPID.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // particle id probability densities                                         //
21 //                                                                           //
22 // The AliPID class stores the probability densities for the different       //
23 // particle type hypotheses electron, muon, pion, kaon, proton, photon,      //
24 // pi0, neutron, K0 and electron conversion. These probability densities     //
25 // are determined from the detector response functions.                      //
26 // The * and *= operators are overloaded for AliPID to combine the PIDs      //
27 // from different detectors.                                                 //
28 //                                                                           //
29 // The Bayesian probability to be a particle of a given type can be          //
30 // calculated from the probability densities, if the a priori probabilities  //
31 // (or abundences, concentrations) of particle species are known. These      //
32 // priors can be given as argument to the GetProbability or GetMostProbable  //
33 // method or they can be set globally by calling the static method           //
34 // SetPriors().                                                              //
35 //                                                                           //
36 // The implementation of this class is based on the note ...                 //
37 // by Iouri Belikov and Karel Safarik.                                       //
38 //                                                                           //
39 ///////////////////////////////////////////////////////////////////////////////
40
41 #include <TClass.h>
42 #include <TDatabasePDG.h>
43 #include <TPDGCode.h>
44
45 #include "AliLog.h"
46 #include "AliPID.h"
47
48 #define M(PID) TDatabasePDG::Instance()->GetParticle(fgkParticleCode[(PID)])->Mass()
49
50 ClassImp(AliPID)
51
52 const char* AliPID::fgkParticleName[AliPID::kSPECIESN+1] = {
53   "electron",
54   "muon",
55   "pion",
56   "kaon",
57   "proton",
58   "photon",
59   "pi0",
60   "neutron",
61   "kaon0",
62   "eleCon",
63   "unknown"
64 };
65
66 const Int_t AliPID::fgkParticleCode[AliPID::kSPECIESN+1] = {
67   ::kElectron, 
68   ::kMuonMinus, 
69   ::kPiPlus, 
70   ::kKPlus, 
71   ::kProton,
72   ::kGamma,
73   ::kPi0,
74   ::kNeutron,
75   ::kK0,
76   ::kGamma,
77   0
78 };
79
80 /*const*/ Float_t AliPID::fgkParticleMass[AliPID::kSPECIESN+1] = {
81   0,0,0,0,0,0,0,0,0,0,0
82   /*
83   M(kElectron),  // electron
84   M(kMuon), // muon
85   M(kPion),    // pion
86   M(kKaon),     // kaon
87   M(kProton),    // proton
88   M(kPhoton),     // photon
89   M(kPi0),       // pi0
90   M(kNeutron),   // neutron
91   M(kKaon0),        // kaon0
92   M(kEleCon),     // electron conversion
93   0.00000        // unknown
94   */
95 };
96
97 Double_t AliPID::fgPrior[kSPECIESN] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
98
99
100 //_______________________________________________________________________
101 AliPID::AliPID() :
102   TObject(),
103   fCharged(0)
104 {
105   //
106   // Default constructor
107   //
108   Init();
109   // set default values (= equal probabilities)
110   for (Int_t i = 0; i < kSPECIESN; i++)
111     fProbDensity[i] = 1./kSPECIESN;
112 }
113
114 //_______________________________________________________________________
115 AliPID::AliPID(const Double_t* probDensity, Bool_t charged) : 
116   TObject(),
117   fCharged(charged)
118 {
119   //
120   // Standard constructor
121   //
122   Init();
123   // set given probability densities
124   for (Int_t i = 0; i < kSPECIES; i++) 
125     fProbDensity[i] = probDensity[i];
126
127   for (Int_t i = kSPECIES; i < kSPECIESN; i++) 
128     fProbDensity[i] = ((charged) ? 0 : probDensity[i]);
129 }
130
131 //_______________________________________________________________________
132 AliPID::AliPID(const Float_t* probDensity, Bool_t charged) :
133   TObject(),
134   fCharged(charged)
135 {
136   //
137   // Standard constructor
138   //
139   Init();
140   // set given probability densities
141   for (Int_t i = 0; i < kSPECIES; i++) 
142     fProbDensity[i] = probDensity[i];
143
144   for (Int_t i = kSPECIES; i < kSPECIESN; i++) 
145     fProbDensity[i] = ((charged) ? 0 : probDensity[i]);
146 }
147
148 //_______________________________________________________________________
149 AliPID::AliPID(const AliPID& pid) : 
150   TObject(pid),
151   fCharged(pid.fCharged)
152 {
153   //
154   // copy constructor
155   //
156   // We do not call init here, MUST already be done
157   for (Int_t i = 0; i < kSPECIESN; i++) 
158     fProbDensity[i] = pid.fProbDensity[i];
159 }
160
161 //_______________________________________________________________________
162 AliPID& AliPID::operator = (const AliPID& pid)
163 {
164 // assignment operator
165
166   fCharged = pid.fCharged;
167   for (Int_t i = 0; i < kSPECIESN; i++) {
168     fProbDensity[i] = pid.fProbDensity[i];
169   }
170   return *this;
171 }
172
173 //_______________________________________________________________________
174 void AliPID::Init() 
175 {
176   //
177   // Initialise the masses
178   //
179   // Initialise only once... 
180   if(!fgkParticleMass[0]) 
181     for (Int_t i = 0; i < kSPECIESN; i++) 
182       fgkParticleMass[i] = M(i);
183 }
184
185 //_____________________________________________________________________________
186 Double_t AliPID::GetProbability(EParticleType iType,
187                                 const Double_t* prior) const
188 {
189   //
190   // Get the probability to be a particle of type "iType"
191   // assuming the a priori probabilities "prior"
192   //
193   Double_t sum = 0.;
194   Int_t nSpecies = ((fCharged) ? kSPECIES : kSPECIESN);
195   for (Int_t i = 0; i < nSpecies; i++) {
196     sum += fProbDensity[i] * prior[i];
197   }
198   if (sum <= 0) {
199     AliError("Invalid probability densities or priors");
200     return -1;
201   }
202   return fProbDensity[iType] * prior[iType] / sum;
203 }
204
205 //_____________________________________________________________________________
206 Double_t AliPID::GetProbability(EParticleType iType) const
207 {
208 // get the probability to be a particle of type "iType"
209 // assuming the globaly set a priori probabilities
210
211   return GetProbability(iType, fgPrior);
212 }
213
214 //_____________________________________________________________________________
215 void AliPID::GetProbabilities(Double_t* probabilities,
216                               const Double_t* prior) const
217 {
218 // get the probabilities to be a particle of given type
219 // assuming the a priori probabilities "prior"
220
221   Double_t sum = 0.;
222   Int_t nSpecies = ((fCharged) ? kSPECIES : kSPECIESN);
223   for (Int_t i = 0; i < nSpecies; i++) {
224     sum += fProbDensity[i] * prior[i];
225   }
226   if (sum <= 0) {
227     AliError("Invalid probability densities or priors");
228     for (Int_t i = 0; i < nSpecies; i++) probabilities[i] = -1;
229     return;
230   }
231   for (Int_t i = 0; i < nSpecies; i++) {
232     probabilities[i] = fProbDensity[i] * prior[i] / sum;
233   }
234 }
235
236 //_____________________________________________________________________________
237 void AliPID::GetProbabilities(Double_t* probabilities) const
238 {
239 // get the probabilities to be a particle of given type
240 // assuming the globaly set a priori probabilities
241
242   GetProbabilities(probabilities, fgPrior);
243 }
244
245 //_____________________________________________________________________________
246 AliPID::EParticleType AliPID::GetMostProbable(const Double_t* prior) const
247 {
248 // get the most probable particle id hypothesis
249 // assuming the a priori probabilities "prior"
250
251   Double_t max = 0.;
252   EParticleType id = kPion;
253   Int_t nSpecies = ((fCharged) ? kSPECIES : kSPECIESN);
254   for (Int_t i = 0; i < nSpecies; i++) {
255     Double_t prob = fProbDensity[i] * prior[i];
256     if (prob > max) {
257       max = prob;
258       id = EParticleType(i);
259     }
260   }
261   if (max == 0) {
262     AliError("Invalid probability densities or priors");
263   }
264   return id;
265 }
266
267 //_____________________________________________________________________________
268 AliPID::EParticleType AliPID::GetMostProbable() const
269 {
270 // get the most probable particle id hypothesis
271 // assuming the globaly set a priori probabilities
272
273   return GetMostProbable(fgPrior);
274 }
275
276
277 //_____________________________________________________________________________
278 void AliPID::SetPriors(const Double_t* prior, Bool_t charged)
279 {
280 // use the given priors as global a priori probabilities
281
282   Double_t sum = 0;
283   for (Int_t i = 0; i < kSPECIESN; i++) {
284     if (charged && (i >= kSPECIES)) {
285       fgPrior[i] = 0;      
286     } else {
287       if (prior[i] < 0) {
288         AliWarningClass(Form("negative prior (%g) for %ss. "
289                              "Using 0 instead.", prior[i], 
290                              fgkParticleName[i]));
291         fgPrior[i] = 0;
292       } else {
293         fgPrior[i] = prior[i];
294       }
295     }
296     sum += prior[i];
297   }
298   if (sum == 0) {
299     AliWarningClass("all priors are zero.");
300   }
301 }
302
303 //_____________________________________________________________________________
304 void AliPID::SetPrior(EParticleType iType, Double_t prior)
305 {
306 // use the given prior as global a priori probability for particles
307 // of type "iType"
308
309   if (prior < 0) {
310     AliWarningClass(Form("negative prior (%g) for %ss. Using 0 instead.", 
311                          prior, fgkParticleName[iType]));
312     prior = 0;
313   }
314   fgPrior[iType] = prior;
315 }
316
317
318 //_____________________________________________________________________________
319 AliPID& AliPID::operator *= (const AliPID& pid)
320 {
321 // combine this probability densities with the one of "pid"
322
323   for (Int_t i = 0; i < kSPECIESN; i++) {
324     fProbDensity[i] *= pid.fProbDensity[i];
325   }
326   return *this;
327 }
328
329 //_____________________________________________________________________________
330 AliPID operator * (const AliPID& pid1, const AliPID& pid2)
331 {
332 // combine the two probability densities
333
334   AliPID result;
335   result *= pid1;
336   result *= pid2;
337   return result;
338 }