FairRoot/PandaRoot
PndDiscTaskPID.cxx
Go to the documentation of this file.
1 //-------------------------------------------------------------------------
2 // Author: Mustafa Schmidt (Mustafa.A.Schmidt@physik.uni-giessen.de)
3 // Changes:
4 // Date: 30.11.2015
5 // Description: Particle Identification
6 //-------------------------------------------------------------------------
7 
8 
10 #include "PndDiscDigitizedHit.h"
11 #include "PndDiscParticleMCPoint.h"
12 #include "PndDiscSensorMCPoint.h"
13 #include "PndDiscReconResult.h"
14 #include "PndDiscTaskPID.h"
15 #include "PndDiscPID.h"
16 
17 #include "FairRunAna.h"
18 #include "FairRootManager.h"
19 #include "FairEventHeader.h"
20 
21 #include "TClonesArray.h"
22 #include "TString.h"
23 #include "TRandom.h"
24 #include "TMath.h"
25 
26 #include <cstdlib>
27 
28 
29 double PndDiscTaskPID::gauss(int x, double mean_value, double rms)
30 {
31  return 1/sqrt(2*TMath::Pi()*rms*rms)*exp(-0.5*((x-mean_value)/rms)*((x-mean_value)/rms));
32 }
33 
34 //---------------------------------------------------------------------------
35 
36 double PndDiscTaskPID::mean(std::vector<double> values)
37 {
38  double average = 0;
39 
40  for(size_t i = 0; i < values.size(); i++)
41  {
42  average += values[i];
43  }
44 
45  return average/double(values.size());
46 }
47 
48 
49 //---------------------------------------------------------------------------
50 
51 double PndDiscTaskPID::deviation(double mean_value, std::vector<double> values)
52 {
53  double average = 0;
54 
55  for(size_t i = 0; i < values.size(); i++)
56  {
57  average += (values[i]-mean_value)*(values[i]-mean_value);
58  }
59 
60  average = sqrt(1/(double(values.size())-1)*average);
61 
62  return average;
63 }
64 
65 
66 //---------------------------------------------------------------------------
67 
69 {
70  branch_name_digits = "DiscPatternPrediction";
71  folder_name_digits = "DiscDIRC";
72 }
73 
74 //---------------------------------------------------------------------------
75 
76 
78 {
79 }
80 
81 
82 //--------------------------------------------------------------------------
83 
84 
86 {
87  LOG(INFO) << "PndDiscTaskPID::ReInit()";
88  return kSUCCESS;
89 }
90 
91 //--------------------------------------------------------------------------
92 
94 {
95  LOG(INFO) << "PndDiscTaskPID::Init()";
96 
97  // Get IO manager instance
98  FairRootManager* io_manager = FairRootManager::Instance();
99  if(!io_manager)
100  {
101  LOG(FATAL) << "FairRootManager instance is NULL !!!";
102  return kFATAL;
103  }
104 
105  // Get branch with digitized hits
106  tclarr_digits = (TClonesArray*) io_manager->GetObject("DiscDigitizedHit");
107  if(!tclarr_digits)
108  {
109  LOG(ERROR) << "Branch " << branch_name_digits.Data() << " is not accessible through FairRootManager.";
110  return kERROR;
111  }
112 
113  // Get branch with hit pattern prediction
114  tclarr_recon_results = (TClonesArray*) io_manager->GetObject("DiscPatternPrediction");
116  {
117  LOG(ERROR) << "No DiscPatternPrediction collection registered with FairRootManager";
118  return kFATAL;
119  }
120 
121  // Get branch with primary particle information
122  tclarr_particles_in = (TClonesArray*) io_manager->GetObject("DiscRealTracks");
124  {
125  LOG(ERROR) << "No DiscRealTracks collection registered with FairRootManager";
126  return kFATAL;
127  }
128 
129  tclarr_pid_results = new TClonesArray("PndDiscPID");
130 
131  io_manager->Register("DiscPID",
132  "DiscDIRC_Detector",
134 
135 
136 
137  static bool recon_initialized = false;
138  if(recon_initialized) return kSUCCESS;
139 
140  return kSUCCESS;
141 }
142 
143 
144 //----------------------------------------------------------------------
145 
146 
147 void PndDiscTaskPID::Exec(Option_t*)
148 {
149  //Reading out entries
150  int particles = tclarr_particles_in->GetEntries();
151  int entries = tclarr_recon_results->GetEntries();
152  int digits = tclarr_digits->GetEntries();
153 
154  std::cout << "Number of particles: " << particles << std::endl;
155 
156  //Information of theoretical hit pattern
157  std::vector<std::vector<std::vector<int> > > pixel_prediction; //Predicted hit pattern
158  std::vector<std::vector<std::vector<double> > > time_prediction; //Predicted time of hit
159  std::vector<std::vector<std::vector<double> > > dtime; //Time difference
160 
161  //Information of simulated hit pattern
162  std::vector<std::vector<int> > pixel_hit;
163  std::vector<std::vector<double> > tdc_time;
164 
165  //Information of primary particle
166  std::vector<double> x0,y0,px,py,pz,p;
167  std::vector<double> theta,phi;
168  std::vector<double> t;
169 
170  std::vector<std::vector<double > > mean_time;
171  std::vector<std::vector<double > > deviation_time;
172 
173  //Creating matrix for hitpattern
174  for(int nparticle = 0; nparticle < particles; nparticle++)
175  {
176  pixel_prediction.push_back(std::vector<std::vector<int> >());
177  time_prediction.push_back(std::vector<std::vector<double> >());
178  dtime.push_back(std::vector<std::vector<double> >());
179  mean_time.push_back(std::vector<double>());
180  deviation_time.push_back(std::vector<double>());
181  pixel_hit.push_back(std::vector<int>());
182  tdc_time.push_back(std::vector<double>());
183 
184  for(int nhypo = 0; nhypo < 3; nhypo++)
185  {
186  pixel_prediction[nparticle].push_back(std::vector<int>());
187  time_prediction[nparticle].push_back(std::vector<double>());
188  dtime[nparticle].push_back(std::vector<double>());
189  }
190  }
191 
192  for(int i = 0; i < particles; i++)
193  {
195 
196  double time = particle_mc_point->GetTime();
197 
198  t.push_back(time);
199  }
200 
201  for(int i = 0; i < entries; i++)
202  {
204 
205  int particle = recon_result->particle;
206  int hypothesis = recon_result->hypothesis;
207  int pixel = recon_result->pixel;
208  double time = recon_result->time;
209 
210  pixel_prediction[particle][hypothesis].push_back(pixel);
211  time_prediction[particle][hypothesis].push_back(time);
212  }
213 
214  //-------------------------------------------------------------------------
215  // Difference between predicted and simulated time
216  //-------------------------------------------------------------------------
217 
218  for(int i = 0; i < particles; i++)
219  {
220  for(int j = 0; j < digits; j++)
221  {
222  PndDiscDigitizedHit* sensor_mc_point = (PndDiscDigitizedHit*)tclarr_digits->At(j);
223 
224  int detector_id = sensor_mc_point->GetDetectorID();
225  int sensor_id = 27*detector_id + sensor_mc_point->GetReadoutID(); //Sensor ID of hit
226  //int pixel = sensor_mc_point->GetPixelNumber(); // Pixel number of hit //[R.K. 01/2017] unused variable?
227  double tdc = sensor_mc_point->GetTdcTime();
228 
229  for(int k = 0; k < 3; k++)
230  {
231  double propagation = tdc - t[i]; //Calculation of photon propagation time
232  double dt = propagation - time_prediction[i][k][sensor_id]; //Calculation of time difference
233  if(dt < 3 && dt > -3)
234  {
235  dtime[i][k].push_back(dt);
236  }
237  else
238  {
239  dtime[i][k].push_back(-1);
240  }
241  }
242  }
243 
244  //Calculating mean of time difference and standard deviation
245  for(int k = 0; k < 3; k++)
246  {
247  mean_time[i].push_back(mean(dtime[i][k]));
248  deviation_time[i].push_back(deviation(mean_time[i][k],dtime[i][k]));
249  std::cout << "Mean time difference: " << mean_time[i][k] << ", deviation: " << deviation_time[i][k] << std::endl;
250  }
251  std::cout << std::endl;
252  }
253 
254  PndDiscPID *pid_result = new PndDiscPID();
255 
256  for(int i = 0; i < particles; i++)
257  {
258  double prob[3] = {0};
259 
260  for(int j = 0; j < digits; j++)
261  {
262  PndDiscDigitizedHit* sensor_mc_point = (PndDiscDigitizedHit*)tclarr_digits->At(j);
263 
264  int detector_id = sensor_mc_point->GetDetectorID();
265  int sensor_id = 27*detector_id + sensor_mc_point->GetReadoutID(); //Sensor ID of hit
266  int pixel = sensor_mc_point->GetPixelNumber(); // Pixel number of hit
267  //double tdc = sensor_mc_point->GetTdcTime(); //[R.K. 01/2017] unused variable?
268 
269  for(int k = 0; k < 3; k++)
270  {
271  //std::cout << "Time differences: " << tdc - t[i] - time_prediction[i][k][sensor_id] - mean_time[i][k] << std::endl;
272  //std::cout << "Pixel difference: " << pixel_prediction[i][k][sensor_id] - pixel << std::endl;
273 
274  if(pixel_prediction[i][0][sensor_id] != -1 && dtime[i][0][j] != -1 && pixel_prediction[i][1][sensor_id] != -1 && dtime[i][1][j] != -1 && pixel_prediction[i][2][sensor_id] != -1 && dtime[i][2][j] != -1)
275  {
276  if((pixel - pixel_prediction[i][0][sensor_id] < 5 && pixel - pixel_prediction[i][0][sensor_id] > -5) || (pixel - pixel_prediction[i][1][sensor_id] < 5 && pixel - pixel_prediction[i][1][sensor_id] > -5) || (pixel - pixel_prediction[i][2][sensor_id] < 5 && pixel - pixel_prediction[i][2][sensor_id] > -5))
277  {
278  //double propagation = tdc - t[i]; //Calculation of photon propagation time //[R.K. 01/2017] unused variable
279  //double dt = propagation - time_prediction[i][k][sensor_id]; //Calculation of time difference //[R.K. 01/2017] unused variable?
280  prob[k] += log(gauss(pixel,pixel_prediction[i][k][sensor_id],4)); //Likelihood calculation
281  }
282  }
283  }
284  }
285 
286  std::cout << std::endl;
287  std::cout << "Log Likelihood: " << std::endl;
288  std::cout << "Pion: " << (prob[0]) << std::endl;
289  std::cout << "Kaon: " << (prob[1]) << std::endl;
290  std::cout << "Proton: " << (prob[2]) << std::endl << std::endl;
291 
292  pid_result->loglikepion = prob[0];
293  pid_result->loglikekaon = prob[1];
294  pid_result->loglikeproton = prob[2];
295 
296  //Save identified particle
297 
298  if(prob[0] < prob[1] && prob[0] < prob[2])
299  {
300  pid_result->pion = 1;
301  pid_result->kaon = 0;
302  pid_result->proton = 0;
303  new ((*tclarr_pid_results)[tclarr_pid_results->GetEntriesFast()]) PndDiscPID(*pid_result);
304  }
305  if(prob[1] < prob[0] && prob[1] < prob[2])
306  {
307  pid_result->pion = 0;
308  pid_result->kaon = 1;
309  pid_result->proton = 0;
310  new ((*tclarr_pid_results)[tclarr_pid_results->GetEntriesFast()]) PndDiscPID(*pid_result);
311  }
312  if(prob[2] < prob[0] && prob[2] < prob[1])
313  {
314  pid_result->pion = 0;
315  pid_result->kaon = 0;
316  pid_result->proton = 1;
317  new ((*tclarr_pid_results)[tclarr_pid_results->GetEntriesFast()]) PndDiscPID(*pid_result);
318  }
319 
320  }
321 
322  std::cout << "PndDiscTaskPID::Exec" << std::endl;
323 }
324 
325 
327 {
328  // called after all Tasks did their Exec() and the data is copied to the file
329 
330  if(tclarr_pid_results != NULL) tclarr_pid_results->Clear();
331 
332  FinishEvents();
333 }
334 
335 
337 {
338 }
339 
340 
342 
Double_t x0
Definition: checkhelixhit.C:70
TClonesArray * tclarr_recon_results
friend F32vec4 exp(const F32vec4 &a)
Definition: P4_F32vec4.h:109
Int_t i
Definition: run_full.C:25
const Int_t & GetReadoutID() const
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:29
double loglikepion
Definition: PndDiscPID.h:21
TClonesArray * tclarr_pid_results
results of reconstruction (pattern matching)
virtual ~PndDiscTaskPID()
TClonesArray * tclarr_digits
friend F32vec4 log(const F32vec4 &a)
Definition: P4_F32vec4.h:110
double pion
Definition: PndDiscPID.h:25
TClonesArray * tclarr_particles_in
const Double_t & GetTdcTime() const
double deviation(double mean, std::vector< double > values)
Double_t p
Definition: anasim.C:58
const int particle
virtual void Exec(Option_t *opt)
virtual InitStatus ReInit()
TString folder_name_digits
Folder name for digits.
double proton
Definition: PndDiscPID.h:27
virtual void FinishEvent()
Double_t y0
Definition: checkhelixhit.C:71
virtual void FinishTask()
double gauss(int x, double mean, double rms)
const Int_t & GetDetectorID() const
TString branch_name_digits
Branch name where digitized hits are stored.
const Int_t & GetPixelNumber() const
double loglikekaon
Definition: PndDiscPID.h:22
double loglikeproton
Definition: PndDiscPID.h:23
Double_t x
ClassImp(PndAnaContFact)
TTree * t
Definition: bump_analys.C:13
virtual InitStatus Init()
double mean(std::vector< double > values)
Double_t Pi
double pz[39]
Definition: pipisigmas.h:14
double kaon
Definition: PndDiscPID.h:26