FairRoot/PandaRoot
PndPidEmcBayesAssociatorTask.cxx
Go to the documentation of this file.
2 #include "PndPidCandidate.h"
3 #include "PndPidProbability.h"
4 #include "FairRootManager.h"
5 #include "TMath.h"
6 #include "TF1.h"
7 #include "TH2.h"
8 #include "Riostream.h"
9 
10 
11 //___________________________________________________________
13  //
14  FairRootManager *fManager =FairRootManager::Instance();
15  fManager->Write();
16 }
17 
18 //___________________________________________________________
20  //---
21  fPidChargedProb = new TClonesArray("PndPidProbability");
23 }
24 
25 //___________________________________________________________
26 PndPidEmcBayesAssociatorTask::PndPidEmcBayesAssociatorTask(const char *name, const char *title):FairTask(name)
27 {
28  //---
29  fPidChargedProb = new TClonesArray("PndPidProbability");
31  SetTitle(title);
32 }
33 
34 //___________________________________________________________
36 
37  std::cout << "InitStatus PndPidEmcBayesAssociatorTask::Init()" << std::endl;
38 
39  FairRootManager *fManager =FairRootManager::Instance();
40 
41  fPidChargedCand = (TClonesArray *)fManager->GetObject("PidChargedCand"+fTrackBranchNamePidHypo);
42  if ( ! fPidChargedCand) {
43  std::cout << "-I- PndPidEmcBayesAssociatorTask::Init: No PndPidCandidate array PidChargedCand there!" << std::endl;
44  return kERROR;
45  }
46 
47  Register();
48 
49  fevcounter=0;
50 
51  SetupEnvironment((char *)"BayesEMC.root"); // read back of the files
52  std::cout << "-I- PndPidEmcBayesAssociatorTask::Init: Success!!" << std::endl;
53 
54  return kSUCCESS;
55 }
56 
57 //______________________________________________________
59 // read in all pdf files and store
60  TString fPath = getenv("VMCWORKDIR");
61  fPath += "/macro/params/";
62  TString fullName = fPath + filename;
63  printf("-I- PndPidEmcBayesAssociatorTask::SetupEnvironment:File used=%s",filename);
64  TFile* hfile1 = new TFile (fullName);
65 
66  fBayesEP[0] = (TH2D*)hfile1->Get("p.v.EP_proba_elecSpos");
67  fBayesTH[0] = (TH2D*)hfile1->Get("p.v.LAT_proba_elecSpos");
68  fBayesZZ[0] = (TH2D*)hfile1->Get("p.v.Z53_proba_elecSpos");
69 
70  fBayesEP[1] = (TH2D*)hfile1->Get("p.v.EP_proba_muonSpos");
71  fBayesTH[1] = (TH2D*)hfile1->Get("p.v.LAT_proba_muonSpos");
72  fBayesZZ[1] = (TH2D*)hfile1->Get("p.v.Z53_proba_muonSpos");
73 
74  fBayesEP[2] = (TH2D*)hfile1->Get("p.v.EP_proba_pionSpos");
75  fBayesTH[2] = (TH2D*)hfile1->Get("p.v.LAT_proba_pionSpos");
76  fBayesZZ[2] = (TH2D*)hfile1->Get("p.v.Z53_proba_pionSpos");
77 
78  fBayesEP[3] = (TH2D*)hfile1->Get("p.v.EP_proba_kaonSpos");
79  fBayesTH[3] = (TH2D*)hfile1->Get("p.v.LAT_proba_kaonSpos");
80  fBayesZZ[3] = (TH2D*)hfile1->Get("p.v.Z53_proba_kaonSpos");
81 
82  fBayesEP[4] = (TH2D*)hfile1->Get("p.v.EP_proba_protSpos");
83  fBayesTH[4] = (TH2D*)hfile1->Get("p.v.LAT_proba_protSpos");
84  fBayesZZ[4] = (TH2D*)hfile1->Get("p.v.Z53_proba_protSpos");
85 
86  fBayesEP[5] = (TH2D*)hfile1->Get("p.v.EP_proba_elecSneg");
87  fBayesTH[5] = (TH2D*)hfile1->Get("p.v.LAT_proba_elecSneg");
88  fBayesZZ[5] = (TH2D*)hfile1->Get("p.v.Z53_proba_elecSneg");
89 
90  fBayesEP[6] = (TH2D*)hfile1->Get("p.v.EP_proba_muonSneg");
91  fBayesTH[6] = (TH2D*)hfile1->Get("p.v.LAT_proba_muonSneg");
92  fBayesZZ[6] = (TH2D*)hfile1->Get("p.v.Z53_proba_muonSneg");
93 
94  fBayesEP[7] = (TH2D*)hfile1->Get("p.v.EP_proba_pionSneg");
95  fBayesTH[7] = (TH2D*)hfile1->Get("p.v.LAT_proba_pionSneg");
96  fBayesZZ[7] = (TH2D*)hfile1->Get("p.v.Z53_proba_pionSneg");
97 
98  fBayesEP[8] = (TH2D*)hfile1->Get("p.v.EP_proba_kaonSneg");
99  fBayesTH[8] = (TH2D*)hfile1->Get("p.v.LAT_proba_kaonSneg");
100  fBayesZZ[8] = (TH2D*)hfile1->Get("p.v.Z53_proba_kaonSneg");
101 
102  fBayesEP[9] = (TH2D*)hfile1->Get("p.v.EP_proba_protSneg");
103  fBayesTH[9] = (TH2D*)hfile1->Get("p.v.LAT_proba_protSneg");
104  fBayesZZ[9] = (TH2D*)hfile1->Get("p.v.Z53_proba_protSneg");
105 
106 
107 }
108 //______________________________________________________
110  //--
111 }
112 //______________________________________________________
114  if (fPidChargedProb->GetEntriesFast() != 0) fPidChargedProb->Clear();
115  if(fVerbose>1) std::cout << "-I- Start PndPidEmcBayesAssociatorTask. "<<std::endl;
116  if(fVerbose>1) std::cout << "-I- counter: "<< fevcounter <<std::endl;
117  fevcounter++;
118 
119  // Get the Candidates
120  for(Int_t i=0; i<fPidChargedCand->GetEntriesFast(); i++)
121  {
123  TClonesArray& pidRef = *fPidChargedProb;
124  PndPidProbability* prob = new(pidRef[i]) PndPidProbability();// initializes with zeros
125  prob->SetIndex(i);
126  if (pidcand->GetEmcIndex()==-1) continue;
127  DoPidMatch(pidcand,prob);
128  }
129 
130 }
131 
133 {
134 // Double_t mom = pidcand->GetMomentum().Mag();
135  Double_t radeg=57.29578;
136 
137  Int_t Charge = pidcand-> GetCharge();
138  Double_t emc = pidcand->GetEmcRawEnergy();
139  Double_t z20 = pidcand->GetEmcClusterZ20();
140  Double_t z53 = pidcand->GetEmcClusterZ53();
141  Double_t lat = pidcand->GetEmcClusterLat();
142  TLorentzVector pidTrack = pidcand->GetLorentzVector();
143  Double_t pidx = pidTrack.Px();
144  Double_t pidy = pidTrack.Py();
145  Double_t pidz = pidTrack.Pz();
146  Double_t pidth = radeg*pidTrack.Theta();
147  Double_t pidph = radeg*pidTrack.Phi();
148 
149  Double_t pidp = TMath::Sqrt(pidx*pidx+pidy*pidy+pidz*pidz);
150  Double_t EP = emc/pidp;
151 
152 // Get the probabilities
153  Double_t proba[5];
154  GetPdf(pidp,pidth,pidph,z20,z53,lat,EP,Charge,proba);
155  if(fVerbose>1) {
156  std::cout <<" proba in Pidmatch: " << proba[0] << " ";
157  std::cout << proba[1] << " " << proba[2] << " ";
158  std::cout << proba[3] << " " << proba[4] << std::endl;
159  }
160 
161 // And store them
162  // electron
163  {
164  prob->SetElectronPdf(proba[0]);
165  }
166 
167  // muon
168  {
169  prob->SetMuonPdf(proba[1]);
170  }
171 
172  // pion
173  {
174  prob->SetPionPdf(proba[2]);
175  }
176 
177  // kaon
178  {
179  prob->SetKaonPdf(proba[3]);
180  }
181 
182  // proton
183  {
184  prob->SetProtonPdf(proba[4]);
185  }
186 }
187 
189  Double_t z20in, Double_t z53in, Double_t LATin, Double_t EPin,
190  Int_t charge, Double_t *proba)
191 {
192 // variables: pp,th,ph, z20, z53, LAT, E/P
193  Double_t lRange[7]={0.2, 5,-180, 0, 0, 0, 0};
194  Double_t uRange[7]={10 ,140, 180, 4, 5, 6, 2};
195  //Int_t nRange[7] ={14 , 7, 1, 1, 1,120}; //[R.K. 01/2017] unused variable
196  Double_t rangePconst0= 4.2318; // Two constants to ajust the momenta
197  Double_t rangePconst1= 5.7682; // calculated from the nominal range
198 
199  Double_t pp = ppin;
200  Double_t th = thin;
201  Double_t ph = phin;
202  Double_t Z20 = z20in;
203  Double_t Z53 = z53in;
204  Double_t LAT = LATin;
205  Double_t EP = EPin;
206  if(fVerbose>1) std::cout << "ppin: " << pp << std::endl;
207 
208 // trafo that spreads out the parameters
209  if( ppin<0.000001) ppin=0.000001;
210  if(z20in>0.999999) z20in=0.999999;
211  if(z53in<0.000001) z53in=0.000001;
212  if(LATin<0.000001) LATin=0.000001;
213 
214  pp = rangePconst0 + rangePconst1 * TMath::Log10(ppin);
215  Z20 = -TMath::Log10(1-z20in);
216  Z53 = -TMath::Log10(z53in);
217  LAT = -TMath::Log10(LATin);
218  if(fVerbose>1) std::cout << "pp: " << pp << std::endl;
219 
220 // if the values are outside the range, get them back in
221  Double_t pplook=ppin;
222 
223  if(pplook<lRange[0]) pplook=1.0001*lRange[0];
224  if(pplook>uRange[0]) pplook=0.9999*uRange[0];
225  if(th <lRange[1]) th =1.0001*lRange[1];
226  if(th >uRange[1]) th =0.9999*uRange[1];
227  if(ph <lRange[2]) ph =1.0001*lRange[2];
228  if(ph >uRange[2]) ph =0.9999*uRange[2];
229  if(Z20 <lRange[3]) Z20 =1.0001*lRange[3];
230  if(Z20 >uRange[3]) Z20 =0.9999*uRange[3];
231  if(Z53 <lRange[4]) Z53 =1.0001*lRange[4];
232  if(Z53 >uRange[4]) Z53 =0.9999*uRange[4];
233  if(LAT <lRange[5]) LAT =1.0001*lRange[5];
234  if(LAT >uRange[5]) LAT =0.9999*uRange[5];
235  if(EP <lRange[6]) EP =1.0001*lRange[6];
236  if(EP >uRange[6]) EP =0.9999*uRange[6];
237  if(fVerbose>1) std::cout << "pplook: " << pplook << std::endl;
238 
239 // Get the bins in the histograms
240  Int_t binxP = (fBayesEP[0]->GetXaxis())->FindBin(pplook);
241  Int_t binyP = (fBayesEP[0]->GetYaxis())->FindBin(EP);
242  Int_t binxT = (fBayesTH[0]->GetXaxis())->FindBin(pplook);
243  Int_t binyT = (fBayesTH[0]->GetYaxis())->FindBin(LAT);
244  Int_t binxZ = (fBayesZZ[0]->GetXaxis())->FindBin(pplook);
245  Int_t binyZ = (fBayesZZ[0]->GetYaxis())->FindBin(Z53);
246  Double_t PBayesE[5];
247  Double_t PBayesL[5];
248  Double_t PBayesZ[5];
249  Double_t PBayesB[5];
250 
251  if(fVerbose>1) {
252  std::cout << "pp: " << pp << " EP " << EP << std::endl;
253  std::cout << "probas: ";
254  }
255 
256 
257  Double_t probasum=0; // used for normalisation
258 
259  for (Int_t k=0; k<5; k++){
260  if(charge>0) {
261  PBayesE[k]=fBayesEP[k]->GetBinContent(binxP,binyP);
262  PBayesL[k]=fBayesTH[k]->GetBinContent(binxT,binyT);
263  PBayesZ[k]=fBayesZZ[k]->GetBinContent(binxZ,binyZ);
264  PBayesB[k]=PBayesE[k]*PBayesL[k]*PBayesZ[k];
265  probasum+=PBayesB[k];
266  } else {
267  PBayesE[k]=fBayesEP[k+5]->GetBinContent(binxP,binyP);
268  PBayesL[k]=fBayesTH[k+5]->GetBinContent(binxT,binyT);
269  PBayesZ[k]=fBayesZZ[k+5]->GetBinContent(binxZ,binyZ);
270  PBayesB[k]=PBayesE[k]*PBayesL[k]*PBayesZ[k];
271  probasum+=PBayesB[k];
272  }
273  PBayesB[k]=PBayesE[k]*PBayesL[k]*PBayesZ[k]/
274  (1-PBayesE[k])*(1-PBayesL[k])*(1-PBayesZ[k]);
275  if(fVerbose>1) std::cout << PBayesZ[k] << " ";
276  }
277 
278 
279 // Normalise the probabilities to one
280  for (Int_t k=0; k<5 ; k++){
281  if(probasum>0) {
282  proba[k]=PBayesB[k]/(1+PBayesB[k]);
283  } else {
284  proba[k]=1.;
285  }
286  }
287 }
288 
289 //_________________________________________________________________
291  //---
292  FairRootManager::Instance()->
293  Register("PidAlgoEmcBayes"+fTrackBranchNamePidHypo,"Pid", fPidChargedProb, kTRUE);
294 }
295 
296 //_________________________________________________________________
298 }
299 
300 //_________________________________________________________________
302  //---
303 }
304 
305 
int fVerbose
Definition: poormantracks.C:24
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
Int_t i
Definition: run_full.C:25
Float_t GetEmcRawEnergy() const
void SetPionPdf(Double_t val)
void emc(Int_t nEvents=10, Char_t part[]="e-", Double_t momentum_min=1.0, Double_t momentum_max=1.0, Double_t theta_min=0, Double_t theta_max=180, Double_t phi_min=0, Double_t phi_max=360, Char_t OutputSimFile[]="sim_emc.root", Char_t OutputDatabaseFile[]="simparams.root", Char_t TransportModel[]="TGeant3", UInt_t seed=0, Bool_t savepoints=kFALSE, Bool_t savehits=kFALSE, Bool_t savewaves=kFALSE, Bool_t savedigis=kFALSE, Bool_t saveclusters=kTRUE, Bool_t savebumps=kTRUE)
Definition: emc.C:1
static T Sqrt(const T &x)
Definition: PndCAMath.h:37
TClonesArray * fPidChargedProb
PndPidCandidate TCA for charged particles.
TLorentzVector GetLorentzVector() const
void SetKaonPdf(Double_t val)
void GetPdf(Double_t pp1, Double_t th1, Double_t ph1, Double_t z20, Double_t z53, Double_t LAT, Double_t EP1, Int_t charge, Double_t *proba)
static T Log10(const T &x)
Definition: PndCAMath.h:41
void SetElectronPdf(Double_t val)
Int_t GetEmcIndex() const
void SetMuonPdf(Double_t val)
h_MC_angle SetTitle("MC truth: opening angle of #pi^{0}")
void DoPidMatch(PndPidCandidate *pidcand, PndPidProbability *prob)
Double_t
Double_t GetEmcClusterZ53() const
Double_t GetEmcClusterLat() const
virtual void Exec(Option_t *option)
float EP[1000]
Double_t GetEmcClusterZ20() const
TString fTrackBranchNamePidHypo
PndPidProbability TCA for charged particles.
TString name
void SetProtonPdf(Double_t val)
ClassImp(PndAnaContFact)
void SetIndex(Int_t idx)
const string filename