]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSPIDv1.cxx
Compilation warnings fixed by T.P.
[u/mrichter/AliRoot.git] / PHOS / AliPHOSPIDv1.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 /* History of cvs commits:
19  *
20  * $Log$
21  */
22
23 //_________________________________________________________________________
24 // Implementation version v1 of the PHOS particle identifier 
25 // Particle identification based on the 
26 //     - RCPV: distance from CPV recpoint to EMCA recpoint.
27 //     - TOF 
28 //     - PCA: Principal Components Analysis..
29 // The identified particle has an identification number corresponding 
30 // to a 9 bits number:
31 //     -Bit 0 to 2: bit set if RCPV > CpvEmcDistance (each bit corresponds
32 //      to a different efficiency-purity point of the photon identification) 
33 //     -Bit 3 to 5: bit set if TOF  < TimeGate (each bit corresponds
34 //      to a different efficiency-purity point of the photon identification) 
35 //     -Bit 6 to 9: bit set if Principal Components are 
36 //      inside an ellipse defined by the parameters a, b, c, x0 and y0.
37 //      (each bit corresponds to a different efficiency-purity point of the 
38 //      photon identification)
39 //      The PCA (Principal components analysis) needs a file that contains
40 //      a previous analysis of the correlations between the particles. This 
41 //      file is $ALICE_ROOT/PHOS/PCA8pa15_0.5-100.root. Analysis done for 
42 //      energies between 0.5 and 100 GeV.
43 //      A calibrated energy is calculated. The energy of the reconstructed
44 //      cluster is corrected with the formula A + B * E  + C * E^2, whose 
45 //      parameters where obtained through the study of the reconstructed 
46 //      energy distribution of monoenergetic photons. 
47 //
48 //      All the parameters (RCPV(2 rows-3 columns),TOF(1r-3c),PCA(5r-4c) 
49 //      and calibration(1r-3c))are stored in a file called 
50 //      $ALICE_ROOT/PHOS/Parameters.dat. Each time that AliPHOSPIDv1 is 
51 //      initialized, this parameters are copied to a Matrix (9,4), a 
52 //      TMatrixD object.  
53 //
54 // use case:
55 //  root [0] AliPHOSPIDv1 * p = new AliPHOSPIDv1("galice1.root")
56 //  Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
57 //          // reading headers from file galice1.root and create  RecParticles 
58             // TrackSegments and RecPoints are used 
59 //          // set file name for the branch RecParticles
60 //  root [1] p->ExecuteTask("deb all time")
61 //          // available options
62 //          // "deb" - prints # of reconstructed particles
63 //          // "deb all" -  prints # and list of RecParticles
64 //          // "time" - prints benchmarking results
65 //                  
66 //  root [2] AliPHOSPIDv1 * p2 = new AliPHOSPIDv1("galice1.root","v1",kTRUE)
67 //  Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
68 //                //Split mode.  
69 //  root [3] p2->ExecuteTask()
70 //
71
72
73 //*-- Author: Yves Schutz (SUBATECH)  & Gines Martinez (SUBATECH) & 
74 //            Gustavo Conesa April 2002
75 //            PCA redesigned by Gustavo Conesa October 2002:
76 //            The way of using the PCA has changed. Instead of 2
77 //            files with the PCA, each one with different energy ranges 
78 //            of application, we use the wide one (0.5-100 GeV), and instead
79 //            of fixing 3 ellipses for different ranges of energy, it has been
80 //            studied the dependency of the ellipses parameters with the 
81 //            energy, and they are implemented in the code as a funtion 
82 //            of the energy. 
83 //
84 //
85 //
86 // --- ROOT system ---
87
88
89 // --- Standard library ---
90 #include "TFormula.h"
91 #include "TBenchmark.h"
92 #include "TPrincipal.h"
93 #include "TFile.h" 
94 #include "TSystem.h"
95
96 // --- AliRoot header files ---
97               //#include "AliLog.h"
98 #include "AliGenerator.h"
99 #include "AliPHOS.h"
100 #include "AliPHOSPIDv1.h"
101 #include "AliPHOSGetter.h"
102
103 ClassImp( AliPHOSPIDv1) 
104
105 //____________________________________________________________________________
106 AliPHOSPIDv1::AliPHOSPIDv1():AliPHOSPID()
107
108   // default ctor
109  
110   InitParameters() ; 
111   fDefaultInit = kTRUE ; 
112 }
113
114 //____________________________________________________________________________
115 AliPHOSPIDv1::AliPHOSPIDv1(const AliPHOSPIDv1 & pid ):AliPHOSPID(pid)
116
117   // ctor
118   InitParameters() ; 
119   Init() ;
120
121 }
122
123 //____________________________________________________________________________
124 AliPHOSPIDv1::AliPHOSPIDv1(const TString alirunFileName, const TString eventFolderName):AliPHOSPID(alirunFileName, eventFolderName)
125
126   //ctor with the indication on where to look for the track segments
127  
128   InitParameters() ; 
129   Init() ;
130   fDefaultInit = kFALSE ; 
131 }
132
133 //____________________________________________________________________________
134 AliPHOSPIDv1::~AliPHOSPIDv1()
135
136   // dtor
137   fPrincipalPhoton = 0;
138   fPrincipalPi0 = 0;
139
140   delete [] fX ;       // Principal input 
141   delete [] fPPhoton ; // Photon Principal components
142   delete [] fPPi0 ;    // Pi0 Principal components
143
144   delete fParameters;
145   delete fTFphoton;
146   delete fTFpiong;
147   delete fTFkaong;
148   delete fTFkaonl;
149   delete fTFhhadrong;
150   delete fTFhhadronl;
151   delete fDFmuon;
152 }
153 //____________________________________________________________________________
154 const TString AliPHOSPIDv1::BranchName() const 
155 {  
156
157   return GetName() ;
158 }
159  
160 //____________________________________________________________________________
161 void AliPHOSPIDv1::Init()
162 {
163   // Make all memory allocations that are not possible in default constructor
164   // Add the PID task to the list of PHOS tasks
165
166   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
167   if(!gime)
168     gime = AliPHOSGetter::Instance(GetTitle(), fEventFolderName.Data()) ; 
169
170   if ( !gime->PID() ) 
171     gime->PostPID(this) ;
172 }
173
174 //____________________________________________________________________________
175 void AliPHOSPIDv1::InitParameters()
176 {
177   // Initialize PID parameters
178   fWrite                   = kTRUE ;
179   fRecParticlesInRun = 0 ; 
180   fNEvent            = 0 ;            
181   fRecParticlesInRun = 0 ;
182   fBayesian          = kTRUE ;
183   SetParameters() ; // fill the parameters matrix from parameters file
184   SetEventRange(0,-1) ;
185
186   // initialisation of response function parameters
187   // Tof
188
189 //   // Photons
190 //   fTphoton[0] = 0.218    ;
191 //   fTphoton[1] = 1.55E-8  ; 
192 //   fTphoton[2] = 5.05E-10 ;
193 //   fTFphoton = new TFormula("ToF response to photons" , "gaus") ; 
194 //   fTFphoton->SetParameters( fTphoton[0], fTphoton[1], fTphoton[2]) ; 
195
196 //   // Pions
197 //   //Gaus (0 to max probability)
198 //   fTpiong[0] = 0.0971    ; 
199 //   fTpiong[1] = 1.58E-8  ; 
200 //   fTpiong[2] = 5.69E-10 ;
201 //   fTFpiong = new TFormula("ToF response to pions" , "gaus") ; 
202 //   fTFpiong->SetParameters( fTpiong[0], fTpiong[1], fTpiong[2]) ; 
203
204 //   // Kaons
205 //   //Gaus (0 to max probability)
206 //   fTkaong[0] = 0.0542  ; 
207 //   fTkaong[1] = 1.64E-8 ; 
208 //   fTkaong[2] = 6.07E-10 ;
209 //   fTFkaong = new TFormula("ToF response to kaon" , "gaus") ; 
210 //   fTFkaong->SetParameters( fTkaong[0], fTkaong[1], fTkaong[2]) ; 
211 //   //Landau (max probability to inf) 
212 //   fTkaonl[0] = 0.264   ;
213 //   fTkaonl[1] = 1.68E-8  ; 
214 //   fTkaonl[2] = 4.10E-10 ;
215 //   fTFkaonl = new TFormula("ToF response to kaon" , "landau") ; 
216 //   fTFkaonl->SetParameters( fTkaonl[0], fTkaonl[1], fTkaonl[2]) ; 
217
218 //   //Heavy Hadrons
219 //   //Gaus (0 to max probability)
220 //   fThhadrong[0] = 0.0302   ;  
221 //   fThhadrong[1] = 1.73E-8  ; 
222 //   fThhadrong[2] = 9.52E-10 ;
223 //   fTFhhadrong = new TFormula("ToF response to heavy hadrons" , "gaus") ; 
224 //   fTFhhadrong->SetParameters( fThhadrong[0], fThhadrong[1], fThhadrong[2]) ; 
225 //   //Landau (max probability to inf) 
226 //   fThhadronl[0] = 0.139    ;  
227 //   fThhadronl[1] = 1.745E-8  ; 
228 //   fThhadronl[2] = 1.00E-9  ;
229 //   fTFhhadronl = new TFormula("ToF response to heavy hadrons" , "landau") ; 
230 //   fTFhhadronl->SetParameters( fThhadronl[0], fThhadronl[1], fThhadronl[2]) ; 
231
232   // Photons
233   fTphoton[0] = 7.83E8   ;
234   fTphoton[1] = 1.55E-8  ; 
235   fTphoton[2] = 5.09E-10 ;
236   fTFphoton = new TFormula("ToF response to photons" , "gaus") ; 
237   fTFphoton->SetParameters( fTphoton[0], fTphoton[1], fTphoton[2]) ; 
238
239   // Pions
240   //Gaus (0 to max probability)
241   fTpiong[0] = 6.73E8    ; 
242   fTpiong[1] = 1.58E-8  ; 
243   fTpiong[2] = 5.87E-10 ;
244   fTFpiong = new TFormula("ToF response to pions" , "gaus") ; 
245   fTFpiong->SetParameters( fTpiong[0], fTpiong[1], fTpiong[2]) ; 
246
247   // Kaons
248   //Gaus (0 to max probability)
249   fTkaong[0] = 3.93E8  ; 
250   fTkaong[1] = 1.64E-8 ; 
251   fTkaong[2] = 6.07E-10 ;
252   fTFkaong = new TFormula("ToF response to kaon" , "gaus") ; 
253   fTFkaong->SetParameters( fTkaong[0], fTkaong[1], fTkaong[2]) ; 
254   //Landau (max probability to inf) 
255   fTkaonl[0] = 2.0E9    ;
256   fTkaonl[1] = 1.68E-8  ; 
257   fTkaonl[2] = 4.10E-10 ;
258   fTFkaonl = new TFormula("ToF response to kaon" , "landau") ; 
259   fTFkaonl->SetParameters( fTkaonl[0], fTkaonl[1], fTkaonl[2]) ; 
260
261   //Heavy Hadrons
262   //Gaus (0 to max probability)
263   fThhadrong[0] = 2.02E8   ;  
264   fThhadrong[1] = 1.73E-8  ; 
265   fThhadrong[2] = 9.52E-10 ;
266   fTFhhadrong = new TFormula("ToF response to heavy hadrons" , "gaus") ; 
267   fTFhhadrong->SetParameters( fThhadrong[0], fThhadrong[1], fThhadrong[2]) ; 
268   //Landau (max probability to inf) 
269   fThhadronl[0] = 1.10E9    ;  
270   fThhadronl[1] = 1.74E-8   ; 
271   fThhadronl[2] = 1.00E-9   ;
272   fTFhhadronl = new TFormula("ToF response to heavy hadrons" , "landau") ; 
273   fTFhhadronl->SetParameters( fThhadronl[0], fThhadronl[1], fThhadronl[2]) ; 
274
275
276
277   // Shower shape: dispersion gaussian parameters
278   // Photons
279   
280 //   fDphoton[0] = 4.62e-2;  fDphoton[1] = 1.39e-2 ; fDphoton[2] = -3.80e-2;//constant
281 //   fDphoton[3] = 1.53   ;  fDphoton[4] =-6.62e-2 ; fDphoton[5] = 0.339   ;//mean
282 //   fDphoton[6] = 6.89e-2;  fDphoton[7] =-6.59e-2 ; fDphoton[8] = 0.194   ;//sigma
283   
284 //   fDpi0[0] = 0.0586  ;  fDpi0[1] = 1.06E-3 ; fDpi0[2] = 0.      ;//constant
285 //   fDpi0[3] = 2.67    ;  fDpi0[4] =-2.00E-2 ; fDpi0[5] = 9.37E-5 ;//mean
286 //   fDpi0[6] = 0.153   ;  fDpi0[7] = 9.34E-4 ; fDpi0[8] =-1.49E-5 ;//sigma
287   
288 //   fDhadron[0] = 1.61E-2 ;  fDhadron[1] = 3.03E-3 ; fDhadron[2] = 1.01E-2 ;//constant
289 //   fDhadron[3] = 3.81    ;  fDhadron[4] = 0.232   ; fDhadron[5] =-1.25    ;//mean
290 //   fDhadron[6] = 0.897   ;  fDhadron[7] = 0.0987  ; fDhadron[8] =-0.534   ;//sigma
291   
292   fDphoton[0] = 1.5    ;  fDphoton[1] = 0.49    ; fDphoton[2] =-1.7E-2 ;//constant
293   fDphoton[3] = 1.5    ;  fDphoton[4] = 4.0E-2  ; fDphoton[5] = 0.21   ;//mean
294   fDphoton[6] = 4.8E-2 ;  fDphoton[7] =-0.12    ; fDphoton[8] = 0.27   ;//sigma
295   fDphoton[9] = 16.; //for E>  fDphoton[9] parameters calculated at  fDphoton[9]
296
297   fDpi0[0] = 0.25      ;  fDpi0[1] = 3.3E-2     ; fDpi0[2] =-1.0e-5    ;//constant
298   fDpi0[3] = 1.50      ;  fDpi0[4] = 398.       ; fDpi0[5] = 12.       ;//mean
299   fDpi0[6] =-7.0E-2    ;  fDpi0[7] =-524.       ; fDpi0[8] = 22.       ;//sigma
300   fDpi0[9] = 110.; //for E>  fDpi0[9] parameters calculated at  fDpi0[9]
301
302   fDhadron[0] = 6.5    ;  fDhadron[1] =-5.3     ; fDhadron[2] = 1.5    ;//constant
303   fDhadron[3] = 3.8    ;  fDhadron[4] = 0.23    ; fDhadron[5] =-1.2    ;//mean
304   fDhadron[6] = 0.88   ;  fDhadron[7] = 9.3E-2  ; fDhadron[8] =-0.51   ;//sigma
305   fDhadron[9] = 2.; //for E>  fDhadron[9] parameters calculated at  fDhadron[9]
306
307   fDmuon[0] = 0.0631 ;
308   fDmuon[1] = 1.4    ; 
309   fDmuon[2] = 0.0557 ;
310   fDFmuon = new TFormula("Shower shape response to muons" , "landau") ; 
311   fDFmuon->SetParameters( fDmuon[0], fDmuon[1], fDmuon[2]) ; 
312
313
314   // x(CPV-EMC) distance gaussian parameters
315   
316 //   fXelectron[0] = 8.06e-2 ;  fXelectron[1] = 1.00e-2; fXelectron[2] =-5.14e-2;//constant
317 //   fXelectron[3] = 0.202   ;  fXelectron[4] = 8.15e-3; fXelectron[5] = 4.55   ;//mean
318 //   fXelectron[6] = 0.334   ;  fXelectron[7] = 0.186  ; fXelectron[8] = 4.32e-2;//sigma
319   
320 //   //charged hadrons gaus
321 //   fXcharged[0] = 6.43e-3 ;  fXcharged[1] =-4.19e-5; fXcharged[2] = 1.42e-3;//constant
322 //   fXcharged[3] = 2.75    ;  fXcharged[4] =-0.40   ; fXcharged[5] = 1.68   ;//mean
323 //   fXcharged[6] = 3.135   ;  fXcharged[7] =-9.41e-2; fXcharged[8] = 1.31e-2;//sigma
324   
325 //   // z(CPV-EMC) distance gaussian parameters
326   
327 //   fZelectron[0] = 8.22e-2 ;  fZelectron[1] = 5.11e-3; fZelectron[2] =-3.05e-2;//constant
328 //   fZelectron[3] = 3.09e-2 ;  fZelectron[4] = 5.87e-2; fZelectron[5] =-9.49e-2;//mean
329 //   fZelectron[6] = 0.263   ;  fZelectron[7] =-9.02e-3; fZelectron[8] = 0.151 ;//sigma
330   
331 //   //charged hadrons gaus
332   
333 //   fZcharged[0] = 1.00e-2 ;  fZcharged[1] = 2.82E-4 ; fZcharged[2] = 2.87E-3 ;//constant
334 //   fZcharged[3] =-4.68e-2 ;  fZcharged[4] =-9.21e-3 ; fZcharged[5] = 4.91e-2 ;//mean
335 //   fZcharged[6] = 1.425   ;  fZcharged[7] =-5.90e-2 ; fZcharged[8] = 5.07e-2 ;//sigma
336
337
338   fXelectron[0] =-1.6E-2 ;  fXelectron[1] = 0.77  ; fXelectron[2] =-0.15 ;//constant
339   fXelectron[3] = 0.35   ;  fXelectron[4] = 0.25  ; fXelectron[5] = 4.12 ;//mean
340   fXelectron[6] = 0.30   ;  fXelectron[7] = 0.11  ; fXelectron[8] = 0.16 ;//sigma
341   fXelectron[9] = 3.; //for E>  fXelectron[9] parameters calculated at  fXelectron[9]
342
343   //charged hadrons gaus
344   fXcharged[0] = 0.14    ;  fXcharged[1] =-3.0E-2 ; fXcharged[2] = 0     ;//constant
345   fXcharged[3] = 1.4     ;  fXcharged[4] =-9.3E-2 ; fXcharged[5] = 1.4   ;//mean
346   fXcharged[6] = 5.7     ;  fXcharged[7] = 0.27   ; fXcharged[8] =-1.8   ;//sigma
347   fXcharged[9] = 1.2; //for E>  fXcharged[9] parameters calculated at  fXcharged[9]
348
349   // z(CPV-EMC) distance gaussian parameters
350   
351   fZelectron[0] = 0.49   ;  fZelectron[1] = 0.53   ; fZelectron[2] =-9.8E-2 ;//constant
352   fZelectron[3] = 2.8E-2 ;  fZelectron[4] = 5.0E-2 ; fZelectron[5] =-8.2E-2 ;//mean
353   fZelectron[6] = 0.25   ;  fZelectron[7] =-1.7E-2 ; fZelectron[8] = 0.17   ;//sigma
354   fZelectron[9] = 3.; //for E>  fZelectron[9] parameters calculated at  fZelectron[9]
355
356   //charged hadrons gaus
357   
358   fZcharged[0] = 0.46    ;  fZcharged[1] =-0.65    ; fZcharged[2] = 0.52    ;//constant
359   fZcharged[3] = 1.1E-2  ;  fZcharged[4] = 0.      ; fZcharged[5] = 0.      ;//mean
360   fZcharged[6] = 0.60    ;  fZcharged[7] =-8.2E-2  ; fZcharged[8] = 0.45    ;//sigma
361   fZcharged[9] = 1.2; //for E>  fXcharged[9] parameters calculated at  fXcharged[9]
362
363   //Threshold to differentiate between charged and neutral
364   fChargedNeutralThreshold = 1e-5;
365   fTOFEnThreshold          = 2;          //Maximum energy to use TOF
366   fDispEnThreshold         = 0.5;       //Minimum energy to use shower shape
367   fDispMultThreshold       = 3;       //Minimum multiplicity to use shower shape
368
369   //Weight to hadrons recontructed energy
370
371   fERecWeightPar[0] = 0.32 ; 
372   fERecWeightPar[1] = 3.8  ;
373   fERecWeightPar[2] = 5.4E-3 ; 
374   fERecWeightPar[3] = 5.6E-2 ;
375   fERecWeight = new TFormula("Weight for hadrons" , "[0]*exp(-x*[1])+[2]*exp(-x*[3])") ; 
376   fERecWeight ->SetParameters(fERecWeightPar[0],fERecWeightPar[1] ,fERecWeightPar[2] ,fERecWeightPar[3]) ; 
377
378
379   for (Int_t i =0; i<  AliPID::kSPECIESN ; i++)
380     fInitPID[i] = 1.;
381  
382 }
383
384 //________________________________________________________________________
385 void  AliPHOSPIDv1::Exec(Option_t *option)
386 {
387   // Steering method to perform particle reconstruction and identification
388   // for the event range from fFirstEvent to fLastEvent.
389   // This range is optionally set by SetEventRange().
390   // if fLastEvent=-1 (by default), then process events until the end.
391   
392   if(strstr(option,"tim"))
393     gBenchmark->Start("PHOSPID");
394   
395   if(strstr(option,"print")) {
396     Print() ; 
397     return ; 
398   }
399
400
401   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
402  
403   if (fLastEvent == -1) 
404     fLastEvent = gime->MaxEvent() - 1 ;
405   else 
406     fLastEvent = TMath::Min(fLastEvent,gime->MaxEvent());
407   Int_t nEvents   = fLastEvent - fFirstEvent + 1;
408
409   Int_t ievent ; 
410   for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
411     gime->Event(ievent,"TR") ;
412     if(gime->TrackSegments() && //Skip events, where no track segments made
413        gime->TrackSegments()->GetEntriesFast()) {
414
415       MakeRecParticles() ;
416
417       if(fBayesian)
418         MakePID() ; 
419       
420       WriteRecParticles();
421       if(strstr(option,"deb"))
422         PrintRecParticles(option) ;
423       //increment the total number of rec particles per run 
424       fRecParticlesInRun += gime->RecParticles()->GetEntriesFast() ; 
425     }
426   }
427   if(strstr(option,"deb"))
428       PrintRecParticles(option);
429   if(strstr(option,"tim")){
430     gBenchmark->Stop("PHOSPID");
431     AliInfo(Form("took %f seconds for PID %f seconds per event", 
432          gBenchmark->GetCpuTime("PHOSPID"),  
433          gBenchmark->GetCpuTime("PHOSPID")/nEvents)) ;
434   }
435   if(fWrite)
436     Unload();
437 }
438
439 //________________________________________________________________________
440 Double_t  AliPHOSPIDv1::GausF(Double_t  x, Double_t  y, Double_t * par)
441 {
442   //Given the energy x and the parameter y (tof, shower dispersion or cpv-emc distance), 
443   //this method returns a density probability of this parameter, given by a gaussian 
444   //function whose parameters depend with the energy  with a function: a/(x*x)+b/x+b
445   //Float_t xorg = x;
446   if (x > par[9]) x = par[9];
447   
448   //Double_t cnt    = par[1] / (x*x) + par[2] / x + par[0] ;
449   Double_t cnt    = par[0] + par[1] * x + par[2] * x * x ;
450   Double_t mean   = par[4] / (x*x) + par[5] / x + par[3] ;
451   Double_t sigma  = par[7] / (x*x) + par[8] / x + par[6] ;
452  
453 //   if(xorg > 30)
454 //     cout<<"En_in = "<<xorg<<"; En_out = "<<x<<"; cnt = "<<cnt
455 //      <<"; mean = "<<mean<<"; sigma = "<<sigma<<endl;
456       
457   //  Double_t arg    = - (y-mean) * (y-mean) / (2*sigma*sigma) ;
458   //  return cnt * TMath::Exp(arg) ;
459   if(TMath::Abs(sigma) > 1.e-10){
460     return cnt*TMath::Gaus(y,mean,sigma);
461   }
462   else
463     return 0.;
464  
465 }
466 //________________________________________________________________________
467 Double_t  AliPHOSPIDv1::GausPol2(Double_t  x, Double_t y, Double_t * par)
468 {
469   //Given the energy x and the parameter y (tof, shower dispersion or cpv-emc distance), 
470   //this method returns a density probability of this parameter, given by a gaussian 
471   //function whose parameters depend with the energy like second order polinomial
472
473   Double_t cnt    = par[0] + par[1] * x + par[2] * x * x ;
474   Double_t mean   = par[3] + par[4] * x + par[5] * x * x ;
475   Double_t sigma  = par[6] + par[7] * x + par[8] * x * x ;
476
477   if(TMath::Abs(sigma) > 1.e-10){
478     return cnt*TMath::Gaus(y,mean,sigma);
479   }
480   else
481     return 0.;
482  
483
484
485 }
486
487 //____________________________________________________________________________
488 const TString AliPHOSPIDv1::GetFileNamePrincipal(TString particle) const
489 {
490   //Get file name that contains the PCA for a particle ("photon or pi0")
491   particle.ToLower();
492   TString name;
493   if      (particle=="photon") 
494     name = fFileNamePrincipalPhoton ;
495   else if (particle=="pi0"   ) 
496     name = fFileNamePrincipalPi0    ;
497   else    
498     AliError(Form("Wrong particle name: %s (choose from pi0/photon)\n",
499                   particle.Data()));
500   return name;
501 }
502
503 //____________________________________________________________________________
504 Float_t  AliPHOSPIDv1::GetParameterCalibration(Int_t i) const 
505 {
506   // Get the i-th parameter "Calibration"
507   Float_t param = 0.;
508   if (i>2 || i<0) { 
509     AliError(Form("Invalid parameter number: %d",i));
510   } else
511     param = (*fParameters)(0,i);
512   return param;
513 }
514
515 //____________________________________________________________________________
516 Float_t  AliPHOSPIDv1::GetCalibratedEnergy(Float_t e) const
517 {
518 //      It calibrates Energy depending on the recpoint energy.
519 //      The energy of the reconstructed cluster is corrected with 
520 //      the formula A + B* E  + C* E^2, whose parameters where obtained 
521 //      through the study of the reconstructed energy distribution of 
522 //      monoenergetic photons.
523  
524   Float_t p[]={0.,0.,0.};
525   for (Int_t i=0; i<3; i++) p[i] = GetParameterCalibration(i);
526   Float_t enerec = p[0] +  p[1]*e + p[2]*e*e;
527   return enerec ;
528
529 }
530
531 //____________________________________________________________________________
532 Float_t  AliPHOSPIDv1::GetParameterCpv2Emc(Int_t i, TString axis) const 
533 {
534   // Get the i-th parameter "CPV-EMC distance" for the specified axis
535   Float_t param = 0.;
536   if(i>2 || i<0) {
537     AliError(Form("Invalid parameter number: %d",i));
538   } else {
539     axis.ToLower();
540     if      (axis == "x") 
541       param = (*fParameters)(1,i);
542     else if (axis == "z") 
543       param = (*fParameters)(2,i);
544     else { 
545       AliError(Form("Invalid axis name: %s",axis.Data()));
546     }
547   }
548   return  param;
549 }
550
551 //____________________________________________________________________________
552 Float_t  AliPHOSPIDv1::GetCpv2EmcDistanceCut(TString axis, Float_t e) const
553 {
554   // Get CpvtoEmcDistance Cut depending on the cluster energy, axis and 
555   // Purity-Efficiency point 
556
557   axis.ToLower();
558   Float_t p[]={0.,0.,0.};
559   for (Int_t i=0; i<3; i++) p[i] = GetParameterCpv2Emc(i,axis);
560   Float_t sig = p[0] + TMath::Exp(p[1] - p[2]*e);
561   return sig;
562 }
563
564 //____________________________________________________________________________
565 Float_t  AliPHOSPIDv1::GetEllipseParameter(TString particle, TString param, Float_t e) const 
566 {
567   // Calculates the parameter param of the ellipse
568
569   particle.ToLower();
570   param.   ToLower();
571   Float_t p[4]={0.,0.,0.,0.};
572   Float_t value = 0.0;
573   for (Int_t i=0; i<4; i++) p[i] = GetParameterToCalculateEllipse(particle,param,i);
574   if (particle == "photon") {
575     if      (param.Contains("a"))  e = TMath::Min((Double_t)e,70.);
576     else if (param.Contains("b"))  e = TMath::Min((Double_t)e,70.);
577     else if (param.Contains("x0")) e = TMath::Max((Double_t)e,1.1);
578   }
579
580  if (particle == "photon")
581     value = p[0]/TMath::Sqrt(e) + p[1]*e + p[2]*e*e + p[3];
582   else if (particle == "pi0")
583     value = p[0] + p[1]*e + p[2]*e*e;
584
585   return value;
586 }
587
588 //_____________________________________________________________________________
589 Float_t  AliPHOSPIDv1::GetParameterPhotonBoundary (Int_t i) const
590
591   // Get the parameter "i" to calculate the boundary on the moment M2x
592   // for photons at high p_T
593   Float_t param = 0;
594   if (i>3 || i<0) {
595     AliError(Form("Wrong parameter number: %d\n",i));
596   } else
597     param = (*fParameters)(14,i) ;
598   return param;
599 }
600
601 //____________________________________________________________________________
602 Float_t  AliPHOSPIDv1::GetParameterPi0Boundary (Int_t i) const
603
604   // Get the parameter "i" to calculate the boundary on the moment M2x
605   // for pi0 at high p_T
606   Float_t param = 0;
607   if (i>2 || i<0) {
608     AliError(Form("Wrong parameter number: %d\n",i));
609   } else
610     param = (*fParameters)(15,i) ;
611   return param;
612 }
613
614 //____________________________________________________________________________
615 Float_t  AliPHOSPIDv1::GetParameterTimeGate(Int_t i) const
616 {
617   // Get TimeGate parameter depending on Purity-Efficiency i:
618   // i=0 - Low purity, i=1 - Medium purity, i=2 - High purity
619   Float_t param = 0.;
620   if(i>2 || i<0) {
621     AliError(Form("Invalid Efficiency-Purity choice %d",i));
622   } else
623     param = (*fParameters)(3,i) ; 
624   return param;
625 }
626
627 //_____________________________________________________________________________
628 Float_t  AliPHOSPIDv1::GetParameterToCalculateEllipse(TString particle, TString param, Int_t i) const
629
630   // Get the parameter "i" that is needed to calculate the ellipse 
631   // parameter "param" for the particle "particle" ("photon" or "pi0")
632
633   particle.ToLower();
634   param.   ToLower();
635   Int_t offset = -1;
636   if      (particle == "photon") 
637     offset=0;
638   else if (particle == "pi0")    
639     offset=5;
640   else
641     AliError(Form("Wrong particle name: %s (choose from pi0/photon)\n",
642                   particle.Data()));
643
644   Int_t p= -1;
645   Float_t par = 0;
646
647   if     (param.Contains("a")) p=4+offset; 
648   else if(param.Contains("b")) p=5+offset; 
649   else if(param.Contains("c")) p=6+offset; 
650   else if(param.Contains("x0"))p=7+offset; 
651   else if(param.Contains("y0"))p=8+offset;
652
653   if      (i>4 || i<0) {
654     AliError(Form("No parameter with index %d", i)) ; 
655   } else if (p==-1) {
656     AliError(Form("No parameter with name %s", param.Data() )) ; 
657   } else
658     par = (*fParameters)(p,i) ;
659   
660   return par;
661 }
662
663
664 //____________________________________________________________________________
665 Float_t  AliPHOSPIDv1::GetDistance(AliPHOSEmcRecPoint * emc,AliPHOSCpvRecPoint * cpv, Option_t *  axis)const
666 {
667   // Calculates the distance between the EMC RecPoint and the PPSD RecPoint
668   
669   const AliPHOSGeometry * geom = AliPHOSGetter::Instance()->PHOSGeometry() ; 
670   TVector3 vecEmc ;
671   TVector3 vecCpv ;
672   if(cpv){
673     emc->GetLocalPosition(vecEmc) ;
674     cpv->GetLocalPosition(vecCpv) ; 
675     
676     if(emc->GetPHOSMod() == cpv->GetPHOSMod()){      
677       // Correct to difference in CPV and EMC position due to different distance to center.
678       // we assume, that particle moves from center
679       Float_t dCPV = geom->GetIPtoOuterCoverDistance();
680       Float_t dEMC = geom->GetIPtoCrystalSurface() ;
681       dEMC         = dEMC / dCPV ;
682       vecCpv = dEMC * vecCpv  - vecEmc ; 
683       if (axis == "X") return vecCpv.X();
684       if (axis == "Y") return vecCpv.Y();
685       if (axis == "Z") return vecCpv.Z();
686       if (axis == "R") return vecCpv.Mag();
687     }
688     return 100000000 ;
689   }
690   return 100000000 ;
691 }
692 //____________________________________________________________________________
693 Int_t  AliPHOSPIDv1::GetCPVBit(AliPHOSEmcRecPoint * emc,AliPHOSCpvRecPoint * cpv, Int_t effPur, Float_t e) const
694 {
695   //Calculates the pid bit for the CPV selection per each purity.
696   if(effPur>2 || effPur<0)
697     AliError(Form("Invalid Efficiency-Purity choice %d",effPur));
698   
699   Float_t sigX = GetCpv2EmcDistanceCut("X",e);
700   Float_t sigZ = GetCpv2EmcDistanceCut("Z",e);
701   
702   Float_t deltaX = TMath::Abs(GetDistance(emc, cpv,  "X"));
703   Float_t deltaZ = TMath::Abs(GetDistance(emc, cpv,  "Z"));
704   //Info("GetCPVBit"," xdist %f, sigx %f, zdist %f, sigz %f",deltaX, sigX, deltaZ,sigZ) ;
705  
706   //if(deltaX>sigX*(effPur+1))
707   //if((deltaX>sigX*(effPur+1)) || (deltaZ>sigZ*(effPur+1)))
708   if((deltaX>sigX*(effPur+1)) && (deltaZ>sigZ*(effPur+1)))
709     return 1;//Neutral
710   else
711     return 0;//Charged
712 }
713
714 //____________________________________________________________________________
715 Int_t  AliPHOSPIDv1::GetPrincipalBit(TString particle, const Double_t* p, Int_t effPur, Float_t e)const
716 {
717   //Is the particle inside de PCA ellipse?
718   
719   particle.ToLower();
720   Int_t    prinbit  = 0 ;
721   Float_t a  = GetEllipseParameter(particle,"a" , e); 
722   Float_t b  = GetEllipseParameter(particle,"b" , e);
723   Float_t c  = GetEllipseParameter(particle,"c" , e);
724   Float_t x0 = GetEllipseParameter(particle,"x0", e); 
725   Float_t y0 = GetEllipseParameter(particle,"y0", e);
726   
727   Float_t r = TMath::Power((p[0] - x0)/a,2) + 
728               TMath::Power((p[1] - y0)/b,2) +
729             c*(p[0] -  x0)*(p[1] - y0)/(a*b) ;
730   //3 different ellipses defined
731   if((effPur==2) && (r<1./2.)) prinbit= 1;
732   if((effPur==1) && (r<2.   )) prinbit= 1;
733   if((effPur==0) && (r<9./2.)) prinbit= 1;
734
735   if(r<0)
736     AliError("Negative square?") ;
737
738   return prinbit;
739
740 }
741 //____________________________________________________________________________
742 Int_t  AliPHOSPIDv1::GetHardPhotonBit(AliPHOSEmcRecPoint * emc) const
743 {
744   // Set bit for identified hard photons (E > 30 GeV)
745   // if the second moment M2x is below the boundary
746
747   Float_t e   = emc->GetEnergy();
748   if (e < 30.0) return 0;
749   Float_t m2x = emc->GetM2x();
750   Float_t m2xBoundary = GetParameterPhotonBoundary(0) *
751     TMath::Exp(-TMath::Power(e-GetParameterPhotonBoundary(1),2)/2.0/
752                 TMath::Power(GetParameterPhotonBoundary(2),2)) +
753     GetParameterPhotonBoundary(3);
754   AliDebug(1, Form("GetHardPhotonBit","E=%f, m2x=%f, boundary=%f",
755                        e,m2x,m2xBoundary));
756   if (m2x < m2xBoundary)
757     return 1;// A hard photon
758   else
759     return 0;// Not a hard photon
760 }
761
762 //____________________________________________________________________________
763 Int_t  AliPHOSPIDv1::GetHardPi0Bit(AliPHOSEmcRecPoint * emc) const
764 {
765   // Set bit for identified hard pi0  (E > 30 GeV)
766   // if the second moment M2x is above the boundary
767
768   Float_t e   = emc->GetEnergy();
769   if (e < 30.0) return 0;
770   Float_t m2x = emc->GetM2x();
771   Float_t m2xBoundary = GetParameterPi0Boundary(0) +
772                     e * GetParameterPi0Boundary(1);
773   AliDebug(1,Form("E=%f, m2x=%f, boundary=%f",e,m2x,m2xBoundary));
774   if (m2x > m2xBoundary)
775     return 1;// A hard pi0
776   else
777     return 0;// Not a hard pi0
778 }
779
780 //____________________________________________________________________________
781 TVector3 AliPHOSPIDv1::GetMomentumDirection(AliPHOSEmcRecPoint * emc, AliPHOSCpvRecPoint * )const 
782
783   // Calculates the momentum direction:
784   //   1. if only a EMC RecPoint, direction is given by IP and this RecPoint
785   //   2. if a EMC RecPoint and CPV RecPoint, direction is given by the line through the 2 recpoints 
786   //  However because of the poor position resolution of PPSD the direction is always taken as if we were 
787   //  in case 1.
788
789   TVector3 dir(0,0,0) ; 
790   TMatrix  dummy ;
791   
792   emc->GetGlobalPosition(dir, dummy) ;
793
794   //account correction to the position of IP
795   Float_t xo,yo,zo ; //Coordinates of the origin
796   if(gAlice && gAlice->GetMCApp() && gAlice->Generator()){
797     gAlice->Generator()->GetOrigin(xo,yo,zo) ;
798   }
799   else{
800     xo=yo=zo=0.;
801   }
802   TVector3 origin(xo,yo,zo);
803   dir = dir - origin ;
804   dir.SetMag(1.) ;
805
806   return dir ;  
807 }
808
809 //________________________________________________________________________
810 Double_t  AliPHOSPIDv1::LandauF(Double_t  x, Double_t y, Double_t * par)
811 {
812   //Given the energy x and the parameter y (tof, shower dispersion or cpv-emc distance), 
813   //this method returns a density probability of this parameter, given by a landau 
814   //function whose parameters depend with the energy  with a function: a/(x*x)+b/x+b
815
816   if (x > par[9]) x = par[9];
817
818   //Double_t cnt    = par[1] / (x*x) + par[2] / x + par[0] ;
819   Double_t cnt    = par[0] + par[1] * x + par[2] * x * x ;
820   Double_t mean   = par[4] / (x*x) + par[5] / x + par[3] ;
821   Double_t sigma  = par[7] / (x*x) + par[8] / x + par[6] ;
822
823   if(TMath::Abs(sigma) > 1.e-10){
824     return cnt*TMath::Landau(y,mean,sigma);
825   }
826   else
827     return 0.;
828
829 }
830 //________________________________________________________________________
831 Double_t  AliPHOSPIDv1::LandauPol2(Double_t  x, Double_t y, Double_t * par)
832 {
833
834   //Given the energy x and the parameter y (tof, shower dispersion or cpv-emc distance), 
835   //this method returns a density probability of this parameter, given by a landau 
836   //function whose parameters depend with the energy like second order polinomial
837
838   Double_t cnt    = par[2] * (x*x) + par[1] * x + par[0] ;
839   Double_t mean   = par[5] * (x*x) + par[4] * x + par[3] ;
840   Double_t sigma  = par[8] * (x*x) + par[7] * x + par[6] ;
841
842    if(TMath::Abs(sigma) > 1.e-10){
843     return cnt*TMath::Landau(y,mean,sigma);
844   }
845   else
846     return 0.;
847
848
849 }
850 // //________________________________________________________________________
851 // Double_t  AliPHOSPIDv1::ChargedHadronDistProb(Double_t  x, Double_t y, Double_t * parg, Double_t * parl)
852 // {
853 //   Double_t cnt   = 0.0 ;
854 //   Double_t mean  = 0.0 ;
855 //   Double_t sigma = 0.0 ;
856 //   Double_t arg   = 0.0 ;
857 //   if (y < parl[4] / (x*x) + parl[5] / x + parl[3]){
858 //     cnt    = parg[1] / (x*x) + parg[2] / x + parg[0] ;
859 //     mean   = parg[4] / (x*x) + parg[5] / x + parg[3] ;
860 //     sigma  = parg[7] / (x*x) + parg[8] / x + parg[6] ;
861 //     TF1 * f = new TF1("gaus","gaus",0.,100.);
862 //     f->SetParameters(cnt,mean,sigma);
863 //     arg  = f->Eval(y) ;
864 //   }
865 //   else{
866 //     cnt    = parl[1] / (x*x) + parl[2] / x + parl[0] ;
867 //     mean   = parl[4] / (x*x) + parl[5] / x + parl[3] ;
868 //     sigma  = parl[7] / (x*x) + parl[8] / x + parl[6] ;
869 //     TF1 * f = new TF1("landau","landau",0.,100.);
870 //     f->SetParameters(cnt,mean,sigma);
871 //     arg  = f->Eval(y) ;
872 //   }
873 //   //  Double_t mean   = par[3] + par[4] * x + par[5] * x * x ;
874 //   //   Double_t sigma  = par[6] + par[7] * x + par[8] * x * x ;
875   
876 //   //Double_t arg    = -(y-mean)*(y-mean)/(2*sigma*sigma) ;
877 //   //return cnt * TMath::Exp(arg) ;
878   
879 //   return arg;
880   
881 // }
882 //____________________________________________________________________________
883 void  AliPHOSPIDv1::MakePID()
884 {
885   // construct the PID weight from a Bayesian Method
886   
887   const Int_t kSPECIES = AliPID::kSPECIESN ;
888  
889   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
890
891   Int_t nparticles = gime->RecParticles()->GetEntriesFast() ;
892
893   TObjArray * emcRecPoints = gime->EmcRecPoints() ; 
894   TObjArray * cpvRecPoints = gime->CpvRecPoints() ; 
895   TClonesArray * trackSegments = gime->TrackSegments() ;
896   if ( !emcRecPoints || !cpvRecPoints || !trackSegments ) {
897     AliFatal("RecPoints or TrackSegments not found !") ;  
898   }
899   TIter next(trackSegments) ; 
900   AliPHOSTrackSegment * ts ; 
901   Int_t index = 0 ; 
902
903   Double_t * stof[kSPECIES] ;
904   Double_t * sdp [kSPECIES]  ;
905   Double_t * scpv[kSPECIES] ;
906   Double_t * sw  [kSPECIES] ;
907   //Info("MakePID","Begin MakePID"); 
908   
909   for (Int_t i =0; i< kSPECIES; i++){
910     stof[i] = new Double_t[nparticles] ;
911     sdp [i] = new Double_t[nparticles] ;
912     scpv[i] = new Double_t[nparticles] ;
913     sw  [i] = new Double_t[nparticles] ;
914   }
915   
916
917   while ( (ts = (AliPHOSTrackSegment *)next()) ) {
918     
919     //cout<<">>>>>> Bayesian Index "<<index<<endl;
920
921     AliPHOSEmcRecPoint * emc = 0 ;
922     if(ts->GetEmcIndex()>=0)
923       emc = (AliPHOSEmcRecPoint *) emcRecPoints->At(ts->GetEmcIndex()) ;
924     
925     AliPHOSCpvRecPoint * cpv = 0 ;
926     if(ts->GetCpvIndex()>=0)
927       cpv = (AliPHOSCpvRecPoint *) cpvRecPoints->At(ts->GetCpvIndex()) ;
928     
929 //     Int_t track = 0 ; 
930 //     track = ts->GetTrackIndex() ; //TPC tracks ?
931     
932     if (!emc) {
933       AliFatal(Form("-> emc(%d) = %d", ts->GetEmcIndex(), emc )) ;
934     }
935
936
937     // ############Tof#############################
938
939     //    Info("MakePID", "TOF");
940     Float_t  en   = emc->GetEnergy();    
941     Double_t time = emc->GetTime() ;
942     //    cout<<">>>>>>>Energy "<<en<<"Time "<<time<<endl;
943    
944     // now get the signals probability
945     // s(pid) in the Bayesian formulation
946     
947     stof[AliPID::kPhoton][index]   = 1.; 
948     stof[AliPID::kElectron][index] = 1.;
949     stof[AliPID::kEleCon][index]   = 1.;
950     //We assing the same prob to charged hadrons, sum is 1
951     stof[AliPID::kPion][index]     = 1./3.; 
952     stof[AliPID::kKaon][index]     = 1./3.; 
953     stof[AliPID::kProton][index]   = 1./3.;
954     //We assing the same prob to neutral hadrons, sum is 1
955     stof[AliPID::kNeutron][index]  = 1./2.;
956     stof[AliPID::kKaon0][index]    = 1./2.;
957     stof[AliPID::kMuon][index]     = 1.; 
958  
959     if(en <  fTOFEnThreshold) {
960
961       Double_t pTofPion = fTFpiong ->Eval(time) ; //gaus distribution
962       Double_t pTofKaon = 0;
963
964       if(time < fTkaonl[1])
965         pTofKaon = fTFkaong  ->Eval(time) ; //gaus distribution
966       else 
967         pTofKaon = fTFkaonl  ->Eval(time) ; //landau distribution
968
969       Double_t pTofNucleon = 0;
970
971       if(time < fThhadronl[1])
972         pTofNucleon = fTFhhadrong   ->Eval(time) ; //gaus distribution
973       else
974         pTofNucleon = fTFhhadronl   ->Eval(time) ; //landau distribution
975       //We assing the same prob to neutral hadrons, sum is the average prob
976       Double_t pTofNeHadron =  (pTofKaon + pTofNucleon)/2. ;
977       //We assing the same prob to charged hadrons, sum is the average prob
978       Double_t pTofChHadron =  (pTofPion + pTofKaon + pTofNucleon)/3. ;
979
980       stof[AliPID::kPhoton][index]   = fTFphoton     ->Eval(time) ; 
981       //gaus distribution
982       stof[AliPID::kEleCon][index]   = stof[AliPID::kPhoton][index] ; 
983       //a conversion electron has the photon ToF
984       stof[AliPID::kMuon][index]     = stof[AliPID::kPhoton][index] ;
985  
986       stof[AliPID::kElectron][index] = pTofPion  ;                             
987
988       stof[AliPID::kPion][index]     =  pTofChHadron ; 
989       stof[AliPID::kKaon][index]     =  pTofChHadron ;
990       stof[AliPID::kProton][index]   =  pTofChHadron ;
991
992       stof[AliPID::kKaon0][index]    =  pTofNeHadron ;     
993       stof[AliPID::kNeutron][index]  =  pTofNeHadron ;            
994     } 
995     
996     //    Info("MakePID", "Dispersion");
997     
998     // ###########Shower shape: Dispersion####################
999     Float_t dispersion = emc->GetDispersion();
1000     //dispersion is not well defined if the cluster is only in few crystals
1001     
1002     sdp[AliPID::kPhoton][index]   = 1. ;
1003     sdp[AliPID::kElectron][index] = 1. ;
1004     sdp[AliPID::kPion][index]     = 1. ; 
1005     sdp[AliPID::kKaon][index]     = 1. ; 
1006     sdp[AliPID::kProton][index]   = 1. ;
1007     sdp[AliPID::kNeutron][index]  = 1. ;
1008     sdp[AliPID::kEleCon][index]   = 1. ; 
1009     sdp[AliPID::kKaon0][index]    = 1. ; 
1010     sdp[AliPID::kMuon][index]     = 1. ; 
1011     
1012     if(en > fDispEnThreshold && emc->GetMultiplicity() >  fDispMultThreshold){
1013       sdp[AliPID::kPhoton][index]   = GausF(en , dispersion, fDphoton) ;
1014       sdp[AliPID::kElectron][index] = sdp[AliPID::kPhoton][index] ;
1015       sdp[AliPID::kPion][index]     = LandauF(en , dispersion, fDhadron ) ; 
1016       sdp[AliPID::kKaon][index]     = sdp[AliPID::kPion][index]  ; 
1017       sdp[AliPID::kProton][index]   = sdp[AliPID::kPion][index]  ;
1018       sdp[AliPID::kNeutron][index]  = sdp[AliPID::kPion][index]  ;
1019       sdp[AliPID::kEleCon][index]   = sdp[AliPID::kPhoton][index]; 
1020       sdp[AliPID::kKaon0][index]    = sdp[AliPID::kPion][index]  ; 
1021       sdp[AliPID::kMuon][index]     = fDFmuon ->Eval(dispersion) ; 
1022       //landau distribution
1023     }
1024     
1025 //      Info("MakePID","multiplicity %d, dispersion %f", emc->GetMultiplicity(), dispersion);
1026 //      Info("MakePID","ss: photon %f, hadron %f ",  sdp[AliPID::kPhoton][index],  sdp[AliPID::kPion][index]);
1027 //       cout<<">>>>>multiplicity "<<emc->GetMultiplicity()<<", dispersion "<< dispersion<<endl ;
1028 //       cout<<"<<<<<ss: photon   "<<sdp[AliPID::kPhoton][index]<<", hadron    "<<sdp[AliPID::kPion][index]<<endl;
1029
1030     //########## CPV-EMC  Distance#######################
1031     //     Info("MakePID", "Distance");
1032
1033     Float_t x             = TMath::Abs(GetDistance(emc, cpv,  "X")) ;
1034     Float_t z             = GetDistance(emc, cpv,  "Z") ;
1035    
1036     Double_t pcpv         = 0 ;
1037     Double_t pcpvneutral  = 0. ;
1038    
1039     Double_t elprobx      = GausF(en , x, fXelectron) ;
1040     Double_t elprobz      = GausF(en , z, fZelectron) ;
1041     Double_t chprobx      = GausF(en , x, fXcharged)  ;
1042     Double_t chprobz      = GausF(en , z, fZcharged)  ;
1043     Double_t pcpvelectron = elprobx * elprobz;
1044     Double_t pcpvcharged  = chprobx * chprobz;
1045   
1046 //     cout<<">>>>energy "<<en<<endl;
1047 //     cout<<">>>>electron : x "<<x<<" xprob "<<elprobx<<" z "<<z<<" zprob "<<elprobz<<endl;
1048 //     cout<<">>>>hadron   : x "<<x<<" xprob "<<chprobx<<" z "<<z<<" zprob "<<chprobz<<endl;
1049 //     cout<<">>>>electron : px*pz "<<pcpvelectron <<" hadron: px*pz "<<pcpvcharged<<endl;  
1050
1051     // Is neutral or charged?
1052     if(pcpvelectron >= pcpvcharged)  
1053       pcpv = pcpvelectron ;
1054     else
1055       pcpv = pcpvcharged ;
1056     
1057     if(pcpv < fChargedNeutralThreshold)
1058       {
1059         pcpvneutral  = 1. ;
1060         pcpvcharged  = 0. ;
1061         pcpvelectron = 0. ;
1062       }
1063     //    else
1064     //      cout<<">>>>>>>>>>>CHARGED>>>>>>>>>>>"<<endl;
1065     
1066     scpv[AliPID::kPion][index]     =  pcpvcharged  ; 
1067     scpv[AliPID::kKaon][index]     =  pcpvcharged  ; 
1068     scpv[AliPID::kProton][index]   =  pcpvcharged  ;
1069
1070     scpv[AliPID::kMuon][index]     =  pcpvelectron ; 
1071     scpv[AliPID::kElectron][index] =  pcpvelectron ;
1072     scpv[AliPID::kEleCon][index]   =  pcpvelectron ; 
1073
1074     scpv[AliPID::kPhoton][index]   =  pcpvneutral  ;
1075     scpv[AliPID::kNeutron][index]  =  pcpvneutral  ; 
1076     scpv[AliPID::kKaon0][index]    =  pcpvneutral  ; 
1077
1078     
1079     //   Info("MakePID", "CPV passed");
1080
1081     //############## Pi0 #############################
1082     stof[AliPID::kPi0][index]      = 0. ;  
1083     scpv[AliPID::kPi0][index]      = 0. ;
1084     sdp [AliPID::kPi0][index]      = 0. ;
1085
1086     if(en > 30.){
1087       // pi0 are detected via decay photon
1088       stof[AliPID::kPi0][index]  =   stof[AliPID::kPhoton][index];
1089       scpv[AliPID::kPi0][index]  = pcpvneutral  ;
1090       if(emc->GetMultiplicity() >  fDispMultThreshold)
1091         sdp [AliPID::kPi0][index]  = GausF(en , dispersion, fDpi0) ;
1092         //sdp [AliPID::kPi0][index]  = GausPol2(en , dispersion, fDpi0) ;
1093 //       cout<<"E = "<<en<<" GeV; disp = "<<dispersion<<"; mult = "
1094 //        <<emc->GetMultiplicity()<<endl;
1095 //       cout<<"PDF: photon = "<<sdp [AliPID::kPhoton][index]<<"; pi0 = "
1096 //        <<sdp [AliPID::kPi0][index]<<endl;
1097     }
1098     
1099   
1100
1101     
1102     //############## muon #############################
1103
1104     if(en > 0.5){
1105       //Muons deposit few energy
1106       scpv[AliPID::kMuon][index]     =  0 ;
1107       stof[AliPID::kMuon][index]     =  0 ;
1108       sdp [AliPID::kMuon][index]     =  0 ;
1109     }
1110
1111     //Weight to apply to hadrons due to energy reconstruction
1112
1113     Float_t weight = fERecWeight ->Eval(en) ;
1114  
1115     sw[AliPID::kPhoton][index]   = 1. ;
1116     sw[AliPID::kElectron][index] = 1. ;
1117     sw[AliPID::kPion][index]     = weight ; 
1118     sw[AliPID::kKaon][index]     = weight ; 
1119     sw[AliPID::kProton][index]   = weight ;
1120     sw[AliPID::kNeutron][index]  = weight ;
1121     sw[AliPID::kEleCon][index]   = 1. ; 
1122     sw[AliPID::kKaon0][index]    = weight ; 
1123     sw[AliPID::kMuon][index]     = weight ; 
1124     sw[AliPID::kPi0][index]      = 1. ;
1125
1126 //     if(en > 0.5){
1127 //       cout<<"######################################################"<<endl;
1128 //       //cout<<"MakePID: energy "<<en<<", tof "<<time<<", distance "<<distance<<", dispersion "<<dispersion<<endl ;
1129 //       cout<<"MakePID: energy "<<en<<", tof "<<time<<", dispersion "<<dispersion<<", x "<<x<<", z "<<z<<endl ;
1130 //       cout<<">>>>>multiplicity "<<emc->GetMultiplicity()<<endl;
1131 //       cout<<">>>>electron : xprob "<<elprobx<<" zprob "<<elprobz<<endl;
1132 //       cout<<">>>>hadron   : xprob "<<chprobx<<" zprob "<<chprobz<<endl;
1133 //       cout<<">>>>electron : px*pz "<<pcpvelectron <<" hadron: px*pz "<<pcpvcharged<<endl;  
1134       
1135 //        cout<<"Photon   , pid "<< fInitPID[AliPID::kPhoton]<<" tof "<<stof[AliPID::kPhoton][index]
1136 //        <<", cpv "<<scpv[AliPID::kPhoton][index]<<", ss "<<sdp[AliPID::kPhoton][index]<<endl;
1137 //       cout<<"EleCon   , pid "<< fInitPID[AliPID::kEleCon]<<", tof "<<stof[AliPID::kEleCon][index]
1138 //        <<", cpv "<<scpv[AliPID::kEleCon][index]<<" ss "<<sdp[AliPID::kEleCon][index]<<endl;
1139 //       cout<<"Electron , pid "<< fInitPID[AliPID::kElectron]<<", tof "<<stof[AliPID::kElectron][index]
1140 //        <<", cpv "<<scpv[AliPID::kElectron][index]<<" ss "<<sdp[AliPID::kElectron][index]<<endl;
1141 //       cout<<"Muon     , pid "<< fInitPID[AliPID::kMuon]<<", tof "<<stof[AliPID::kMuon][index]
1142 //        <<", cpv "<<scpv[AliPID::kMuon][index]<<" ss "<<sdp[AliPID::kMuon][index]<<endl;
1143 //        cout<<"Pi0      , pid "<< fInitPID[AliPID::kPi0]<<", tof "<<stof[AliPID::kPi0][index]
1144 //        <<", cpv "<<scpv[AliPID::kPi0][index]<<" ss "<<sdp[AliPID::kPi0][index]<<endl;
1145 //       cout<<"Pion     , pid "<< fInitPID[AliPID::kPion]<<", tof "<<stof[AliPID::kPion][index]
1146 //        <<", cpv "<<scpv[AliPID::kPion][index]<<" ss "<<sdp[AliPID::kPion][index]<<endl;
1147 //       cout<<"Kaon0    , pid "<< fInitPID[AliPID::kKaon0]<<", tof "<<stof[AliPID::kKaon0][index]
1148 //        <<", cpv "<<scpv[AliPID::kKaon0][index]<<" ss "<<sdp[AliPID::kKaon0][index]<<endl;
1149 //       cout<<"Kaon     , pid "<< fInitPID[AliPID::kKaon]<<", tof "<<stof[AliPID::kKaon][index]
1150 //        <<", cpv "<<scpv[AliPID::kKaon][index]<<" ss "<<sdp[AliPID::kKaon][index]<<endl;
1151 //       cout<<"Neutron  , pid "<< fInitPID[AliPID::kNeutron]<<", tof "<<stof[AliPID::kNeutron][index]
1152 //        <<", cpv "<<scpv[AliPID::kNeutron][index]<<" ss "<<sdp[AliPID::kNeutron][index]<<endl;
1153 //       cout<<"Proton   , pid "<< fInitPID[AliPID::kProton]<<", tof "<<stof[AliPID::kProton][index]
1154 //        <<", cpv "<<scpv[AliPID::kProton][index]<<" ss "<<sdp[AliPID::kProton][index]<<endl;
1155 //       cout<<"######################################################"<<endl;
1156 //     }
1157       index++;
1158   }
1159   
1160   //for (index = 0 ; index < kSPECIES ; index++) 
1161   // pid[index] /= nparticles ; 
1162   
1163
1164   //  Info("MakePID", "Total Probability calculation");
1165   
1166   for(index = 0 ; index < nparticles ; index ++) {
1167     
1168     AliPHOSRecParticle * recpar = gime->RecParticle(index) ;  
1169     
1170     //Conversion electron?
1171     
1172     if(recpar->IsEleCon()){
1173       fInitPID[AliPID::kEleCon]   = 1. ;
1174       fInitPID[AliPID::kPhoton]   = 0. ;
1175       fInitPID[AliPID::kElectron] = 0. ;
1176     }
1177     else{
1178       fInitPID[AliPID::kEleCon]   = 0. ;
1179       fInitPID[AliPID::kPhoton]   = 1. ;
1180       fInitPID[AliPID::kElectron] = 1. ;
1181     }
1182     //  fInitPID[AliPID::kEleCon]   = 0. ;
1183     
1184     
1185     // calculates the Bayesian weight
1186     
1187     Int_t jndex ;
1188     Double_t wn = 0.0 ; 
1189     for (jndex = 0 ; jndex < kSPECIES ; jndex++) 
1190       wn += stof[jndex][index] * sdp[jndex][index]  * scpv[jndex][index] * 
1191         sw[jndex][index] * fInitPID[jndex] ;
1192     
1193     //    cout<<"*************wn "<<wn<<endl;
1194     if (TMath::Abs(wn)>0)
1195       for (jndex = 0 ; jndex < kSPECIES ; jndex++) {
1196         //cout<<"jndex "<<jndex<<" wn "<<wn<<" SetPID * wn"
1197         //<<stof[jndex][index] * sdp[jndex][index] * pid[jndex]  << endl;
1198         //cout<<" tof "<<stof[jndex][index] << " disp " <<sdp[jndex][index] << " pid "<< fInitPID[jndex] << endl;
1199         //      if(jndex ==  AliPID::kPi0 || jndex ==  AliPID::kPhoton){
1200         //        cout<<"Particle "<<jndex<<"  final prob * wn   "
1201         //            <<stof[jndex][index] * sdp[jndex][index] * scpv[jndex][index] * 
1202         //          fInitPID[jndex] <<"  wn  "<< wn<<endl;
1203         //        cout<<"pid "<< fInitPID[jndex]<<", tof "<<stof[jndex][index]
1204         //            <<", cpv "<<scpv[jndex][index]<<" ss "<<sdp[jndex][index]<<endl;
1205         //      }
1206         recpar->SetPID(jndex, stof[jndex][index] * sdp[jndex][index] * 
1207                        sw[jndex][index] * scpv[jndex][index] * 
1208                        fInitPID[jndex] / wn) ; 
1209       }
1210   }
1211   //  Info("MakePID", "Delete");
1212   
1213   for (Int_t i =0; i< kSPECIES; i++){
1214     delete [] stof[i];
1215     delete [] sdp [i];
1216     delete [] scpv[i];
1217     delete [] sw  [i];
1218   }
1219   //  Info("MakePID","End MakePID"); 
1220 }
1221
1222 //____________________________________________________________________________
1223 void  AliPHOSPIDv1::MakeRecParticles()
1224 {
1225   // Makes a RecParticle out of a TrackSegment
1226   
1227   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
1228   TObjArray * emcRecPoints = gime->EmcRecPoints() ; 
1229   TObjArray * cpvRecPoints = gime->CpvRecPoints() ; 
1230   TClonesArray * trackSegments = gime->TrackSegments() ; 
1231   if ( !emcRecPoints || !cpvRecPoints || !trackSegments ) {
1232     AliFatal("RecPoints or TrackSegments not found !") ;  
1233   }
1234   TClonesArray * recParticles  = gime->RecParticles() ; 
1235   recParticles->Clear();
1236
1237   TIter next(trackSegments) ; 
1238   AliPHOSTrackSegment * ts ; 
1239   Int_t index = 0 ; 
1240   AliPHOSRecParticle * rp ; 
1241   while ( (ts = (AliPHOSTrackSegment *)next()) ) {
1242     //  cout<<">>>>>>>>>>>>>>>PCA Index "<<index<<endl;
1243     new( (*recParticles)[index] ) AliPHOSRecParticle() ;
1244     rp = (AliPHOSRecParticle *)recParticles->At(index) ; 
1245     rp->SetTrackSegment(index) ;
1246     rp->SetIndexInList(index) ;
1247         
1248     AliPHOSEmcRecPoint * emc = 0 ;
1249     if(ts->GetEmcIndex()>=0)
1250       emc = (AliPHOSEmcRecPoint *) emcRecPoints->At(ts->GetEmcIndex()) ;
1251     
1252     AliPHOSCpvRecPoint * cpv = 0 ;
1253     if(ts->GetCpvIndex()>=0)
1254       cpv = (AliPHOSCpvRecPoint *) cpvRecPoints->At(ts->GetCpvIndex()) ;
1255     
1256     Int_t track = 0 ; 
1257     track = ts->GetTrackIndex() ; 
1258       
1259     // Now set type (reconstructed) of the particle
1260
1261     // Choose the cluster energy range
1262     
1263     if (!emc) {
1264       AliFatal(Form("-> emc(%d) = %d", ts->GetEmcIndex(), emc )) ;
1265     }
1266
1267     Float_t e = emc->GetEnergy() ;   
1268     
1269     Float_t  lambda[2] ;
1270     emc->GetElipsAxis(lambda) ;
1271     
1272     if((lambda[0]>0.01) && (lambda[1]>0.01)){
1273       // Looking PCA. Define and calculate the data (X),
1274       // introduce in the function X2P that gives the components (P).  
1275
1276       Float_t  spher = 0. ;
1277       Float_t  emaxdtotal = 0. ; 
1278       
1279       if((lambda[0]+lambda[1])!=0) 
1280         spher=fabs(lambda[0]-lambda[1])/(lambda[0]+lambda[1]); 
1281       
1282       emaxdtotal=emc->GetMaximalEnergy()/emc->GetEnergy(); 
1283       
1284       fX[0] = lambda[0] ;  
1285       fX[1] = lambda[1] ; 
1286       fX[2] = emc->GetDispersion() ; 
1287       fX[3] = spher ; 
1288       fX[4] = emc->GetMultiplicity() ;  
1289       fX[5] = emaxdtotal ;  
1290       fX[6] = emc->GetCoreEnergy() ;  
1291       
1292       fPrincipalPhoton->X2P(fX,fPPhoton);
1293       fPrincipalPi0   ->X2P(fX,fPPi0);
1294
1295     }
1296     else{
1297       fPPhoton[0]=-100.0;  //We do not accept clusters with 
1298       fPPhoton[1]=-100.0;  //one cell as a photon-like
1299       fPPi0[0]   =-100.0;
1300       fPPi0[1]   =-100.0;
1301     }
1302     
1303     Float_t time = emc->GetTime() ;
1304     rp->SetTof(time) ; 
1305     
1306     // Loop of Efficiency-Purity (the 3 points of purity or efficiency 
1307     // are taken into account to set the particle identification)
1308     for(Int_t effPur = 0; effPur < 3 ; effPur++){
1309       
1310       // Looking at the CPV detector. If RCPV greater than CpvEmcDistance, 
1311       // 1st,2nd or 3rd bit (depending on the efficiency-purity point )
1312       // is set to 1
1313       if(GetCPVBit(emc, cpv, effPur,e) == 1 ){  
1314         rp->SetPIDBit(effPur) ;
1315         //cout<<"CPV bit "<<effPur<<endl;
1316       }
1317       // Looking the TOF. If TOF smaller than gate,  4th, 5th or 6th 
1318       // bit (depending on the efficiency-purity point )is set to 1             
1319       if(time< (*fParameters)(3,effPur)) 
1320         rp->SetPIDBit(effPur+3) ;                   
1321   
1322       //Photon PCA
1323       //If we are inside the ellipse, 7th, 8th or 9th 
1324       // bit (depending on the efficiency-purity point )is set to 1 
1325       if(GetPrincipalBit("photon",fPPhoton,effPur,e) == 1) 
1326         rp->SetPIDBit(effPur+6) ;
1327
1328       //Pi0 PCA
1329       //If we are inside the ellipse, 10th, 11th or 12th 
1330       // bit (depending on the efficiency-purity point )is set to 1 
1331       if(GetPrincipalBit("pi0"   ,fPPi0   ,effPur,e) == 1) 
1332         rp->SetPIDBit(effPur+9) ;
1333     }
1334     if(GetHardPhotonBit(emc))
1335       rp->SetPIDBit(12) ;
1336     if(GetHardPi0Bit   (emc))
1337       rp->SetPIDBit(13) ;
1338     
1339     if(track >= 0) 
1340       rp->SetPIDBit(14) ; 
1341
1342     //Set momentum, energy and other parameters 
1343     Float_t  encal = GetCalibratedEnergy(e);
1344     TVector3 dir   = GetMomentumDirection(emc,cpv) ; 
1345     dir.SetMag(encal) ;
1346     rp->SetMomentum(dir.X(),dir.Y(),dir.Z(),encal) ;
1347     rp->SetCalcMass(0);
1348     rp->Name(); //If photon sets the particle pdg name to gamma
1349     rp->SetProductionVertex(0,0,0,0);
1350     rp->SetFirstMother(-1);
1351     rp->SetLastMother(-1);
1352     rp->SetFirstDaughter(-1);
1353     rp->SetLastDaughter(-1);
1354     rp->SetPolarisation(0,0,0);
1355     //Set the position in global coordinate system from the RecPoint
1356     AliPHOSGeometry * geom = gime->PHOSGeometry() ; 
1357     AliPHOSTrackSegment * ts  = gime->TrackSegment(rp->GetPHOSTSIndex()) ; 
1358     AliPHOSEmcRecPoint  * erp = gime->EmcRecPoint(ts->GetEmcIndex()) ; 
1359     TVector3 pos ; 
1360     geom->GetGlobal(erp, pos) ; 
1361     rp->SetPos(pos);
1362     index++ ; 
1363   }
1364 }
1365   
1366 //____________________________________________________________________________
1367 void  AliPHOSPIDv1::Print(const Option_t *) const
1368 {
1369   // Print the parameters used for the particle type identification
1370
1371     AliInfo("=============== AliPHOSPIDv1 ================") ;
1372     printf("Making PID\n") ;
1373     printf("    Pricipal analysis file from 0.5 to 100 %s\n", fFileNamePrincipalPhoton.Data() )   ; 
1374     printf("    Name of parameters file     %s\n", fFileNameParameters.Data() )  ;
1375     printf("    Matrix of Parameters: 14x4\n") ;
1376     printf("        Energy Calibration  1x3 [3 parametres to calibrate energy: A + B* E + C * E^2]\n") ;
1377     printf("        RCPV 2x3 rows x and z, columns function cut parameters\n") ;
1378     printf("        TOF  1x3 [High Eff-Low Pur,Medium Eff-Pur, Low Eff-High Pur]\n") ;
1379     printf("        PCA  5x4 [5 ellipse parametres and 4 parametres to calculate them: A/Sqrt(E) + B* E + C * E^2 + D]\n") ;
1380     Printf("    Pi0 PCA  5x3 [5 ellipse parametres and 3 parametres to calculate them: A + B* E + C * E^2]\n") ;
1381     fParameters->Print() ;
1382 }
1383
1384
1385
1386 //____________________________________________________________________________
1387 void AliPHOSPIDv1::PrintRecParticles(Option_t * option)
1388 {
1389   // Print table of reconstructed particles
1390
1391   AliPHOSGetter *gime = AliPHOSGetter::Instance() ; 
1392
1393   TClonesArray * recParticles = gime->RecParticles() ; 
1394
1395   TString message ; 
1396   message  = "\nevent " ;
1397   message += gAlice->GetEvNumber() ; 
1398   message += "       found " ; 
1399   message += recParticles->GetEntriesFast(); 
1400   message += " RecParticles\n" ; 
1401
1402   if(strstr(option,"all")) {  // printing found TS
1403     message += "\n  PARTICLE         Index    \n" ; 
1404     
1405     Int_t index ;
1406     for (index = 0 ; index < recParticles->GetEntries() ; index++) {
1407       AliPHOSRecParticle * rp = (AliPHOSRecParticle * ) recParticles->At(index) ;       
1408       message += "\n" ;
1409       message += rp->Name().Data() ;  
1410       message += " " ;
1411       message += rp->GetIndexInList() ;  
1412       message += " " ;
1413       message += rp->GetType()  ;
1414     }
1415   }
1416   AliInfo(message.Data() ) ; 
1417 }
1418
1419 //____________________________________________________________________________
1420 void  AliPHOSPIDv1::SetParameters() 
1421 {
1422   // PCA : To do the Principal Components Analysis it is necessary 
1423   // the Principal file, which is opened here
1424   fX       = new double[7]; // Data for the PCA 
1425   fPPhoton = new double[7]; // Eigenvalues of the PCA
1426   fPPi0    = new double[7]; // Eigenvalues of the Pi0 PCA
1427
1428   // Read photon principals from the photon file
1429   
1430   fFileNamePrincipalPhoton = "$ALICE_ROOT/PHOS/PCA8pa15_0.5-100.root" ; 
1431   TFile f( fFileNamePrincipalPhoton.Data(), "read" ) ;
1432   fPrincipalPhoton = dynamic_cast<TPrincipal*> (f.Get("principal")) ; 
1433   f.Close() ; 
1434
1435   // Read pi0 principals from the pi0 file
1436
1437   fFileNamePrincipalPi0    = "$ALICE_ROOT/PHOS/PCA_pi0_40-120.root" ;
1438   TFile fPi0( fFileNamePrincipalPi0.Data(), "read" ) ;
1439   fPrincipalPi0    = dynamic_cast<TPrincipal*> (fPi0.Get("principal")) ; 
1440   fPi0.Close() ;
1441
1442   // Open parameters file and initialization of the Parameters matrix. 
1443   // In the File Parameters.dat are all the parameters. These are introduced 
1444   // in a matrix of 16x4  
1445   // 
1446   // All the parameters defined in this file are, in order of row: 
1447   // line   0   : calibration 
1448   // lines  1,2 : CPV rectangular cat for X and Z
1449   // line   3   : TOF cut
1450   // lines  4-8 : parameters to calculate photon PCA ellipse
1451   // lines  9-13: parameters to calculate pi0 PCA ellipse
1452   // lines 14-15: parameters to calculate border for high-pt photons and pi0
1453
1454   fFileNameParameters = gSystem->ExpandPathName("$ALICE_ROOT/PHOS/Parameters.dat");
1455   fParameters = new TMatrix(16,4) ;
1456   const Int_t kMaxLeng=255;
1457   char string[kMaxLeng];
1458
1459   // Open a text file with PID parameters
1460   FILE *fd = fopen(fFileNameParameters.Data(),"r");
1461   if (!fd)
1462     AliFatal(Form("File %s with a PID parameters cannot be opened\n",
1463           fFileNameParameters.Data()));
1464
1465   Int_t i=0;
1466   // Read parameter file line-by-line and skip empty line and comments
1467   while (fgets(string,kMaxLeng,fd) != NULL) {
1468     if (string[0] == '\n' ) continue;
1469     if (string[0] == '!'  ) continue;
1470     sscanf(string, "%f %f %f %f",
1471            &(*fParameters)(i,0), &(*fParameters)(i,1), 
1472            &(*fParameters)(i,2), &(*fParameters)(i,3));
1473     i++;
1474     AliDebug(1, Form("SetParameters", "line %d: %s",i,string));
1475   }
1476   fclose(fd);
1477 }
1478
1479 //____________________________________________________________________________
1480 void  AliPHOSPIDv1::SetParameterCalibration(Int_t i,Float_t param) 
1481 {
1482   // Set parameter "Calibration" i to a value param
1483   if(i>2 || i<0) {
1484     AliError(Form("Invalid parameter number: %d",i));
1485   } else
1486     (*fParameters)(0,i) = param ;
1487 }
1488
1489 //____________________________________________________________________________
1490 void  AliPHOSPIDv1::SetParameterCpv2Emc(Int_t i, TString axis, Float_t cut) 
1491 {
1492   // Set the parameters to calculate Cpv-to-Emc Distance Cut depending on 
1493   // Purity-Efficiency point i
1494
1495   if(i>2 || i<0) {
1496     AliError(Form("Invalid parameter number: %d",i));
1497   } else {
1498     axis.ToLower();
1499     if      (axis == "x") (*fParameters)(1,i) = cut;
1500     else if (axis == "z") (*fParameters)(2,i) = cut;
1501     else { 
1502       AliError(Form("Invalid axis name: %s",axis.Data()));
1503     }
1504   }
1505 }
1506
1507 //____________________________________________________________________________
1508 void  AliPHOSPIDv1::SetParameterPhotonBoundary(Int_t i,Float_t param) 
1509 {
1510   // Set parameter "Hard photon boundary" i to a value param
1511   if(i>4 || i<0) {
1512     AliError(Form("Invalid parameter number: %d",i));
1513   } else
1514     (*fParameters)(14,i) = param ;
1515 }
1516
1517 //____________________________________________________________________________
1518 void  AliPHOSPIDv1::SetParameterPi0Boundary(Int_t i,Float_t param) 
1519 {
1520   // Set parameter "Hard pi0 boundary" i to a value param
1521   if(i>1 || i<0) {
1522     AliError(Form("Invalid parameter number: %d",i));
1523   } else
1524     (*fParameters)(15,i) = param ;
1525 }
1526
1527 //_____________________________________________________________________________
1528 void  AliPHOSPIDv1::SetParameterTimeGate(Int_t i, Float_t gate) 
1529 {
1530   // Set the parameter TimeGate depending on Purity-Efficiency point i 
1531   if (i>2 || i<0) {
1532     AliError(Form("Invalid Efficiency-Purity choice %d",i));
1533   } else
1534     (*fParameters)(3,i)= gate ; 
1535
1536
1537 //_____________________________________________________________________________
1538 void  AliPHOSPIDv1::SetParameterToCalculateEllipse(TString particle, TString param, Int_t i, Float_t par) 
1539 {  
1540   // Set the parameter "i" that is needed to calculate the ellipse 
1541   // parameter "param" for a particle "particle"
1542   
1543   particle.ToLower();
1544   param.   ToLower();
1545   Int_t p= -1;
1546   Int_t offset=0;
1547
1548   if      (particle == "photon") offset=0;
1549   else if (particle == "pi0")    offset=5;
1550   else
1551     AliError(Form("Wrong particle name: %s (choose from pi0/photon)\n",
1552                   particle.Data()));
1553
1554   if     (param.Contains("a")) p=4+offset; 
1555   else if(param.Contains("b")) p=5+offset; 
1556   else if(param.Contains("c")) p=6+offset; 
1557   else if(param.Contains("x0"))p=7+offset; 
1558   else if(param.Contains("y0"))p=8+offset;
1559   if((i>4)||(i<0)) {
1560     AliError(Form("No parameter with index %d", i)) ; 
1561   } else if(p==-1) {
1562     AliError(Form("No parameter with name %s", param.Data() )) ; 
1563   } else
1564     (*fParameters)(p,i) = par ;
1565
1566
1567 //____________________________________________________________________________
1568 void AliPHOSPIDv1::Unload() 
1569 {
1570   //Unloads RecPoints, Tracks and RecParticles
1571   AliPHOSGetter * gime = AliPHOSGetter::Instance() ;  
1572   gime->PhosLoader()->UnloadRecPoints() ;
1573   gime->PhosLoader()->UnloadTracks() ;
1574   gime->PhosLoader()->UnloadRecParticles() ;
1575 }
1576
1577 //____________________________________________________________________________
1578 void  AliPHOSPIDv1::WriteRecParticles()
1579 {
1580   //It writes reconstructed particles and pid to file
1581
1582   AliPHOSGetter *gime = AliPHOSGetter::Instance() ; 
1583
1584   TClonesArray * recParticles = gime->RecParticles() ; 
1585   recParticles->Expand(recParticles->GetEntriesFast() ) ;
1586   if(fWrite){
1587     TTree * treeP =  gime->TreeP();
1588     
1589     //First rp
1590     Int_t bufferSize = 32000 ;
1591     TBranch * rpBranch = treeP->Branch("PHOSRP",&recParticles,bufferSize);
1592     rpBranch->SetTitle(BranchName());
1593     
1594     rpBranch->Fill() ;
1595     
1596     gime->WriteRecParticles("OVERWRITE");
1597     gime->WritePID("OVERWRITE");
1598   }
1599 }
1600
1601
1602 //_______________________________________________________________________
1603 void AliPHOSPIDv1::SetInitPID(const Double_t *p) {
1604   // Sets values for the initial population of each particle type 
1605   for (Int_t i=0; i<AliPID::kSPECIESN; i++) fInitPID[i] = p[i];
1606 }
1607 //_______________________________________________________________________
1608 void AliPHOSPIDv1::GetInitPID(Double_t *p) const {
1609   // Gets values for the initial population of each particle type 
1610   for (Int_t i=0; i<AliPID::kSPECIESN; i++) p[i] = fInitPID[i];
1611 }