All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
ShowerRecoTools::ShowerBayesianTrucatingdEdx Class Reference
Inheritance diagram for ShowerRecoTools::ShowerBayesianTrucatingdEdx:
ShowerRecoTools::IShowerTool

Public Member Functions

 ShowerBayesianTrucatingdEdx (const fhicl::ParameterSet &pset)
 
int CalculateElement (const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
 
- Public Member Functions inherited from ShowerRecoTools::IShowerTool
 IShowerTool (const fhicl::ParameterSet &pset)
 
virtual ~IShowerTool () noexcept=default
 
int RunShowerTool (const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder, std::string evd_display_name_append="")
 
virtual void InitialiseProducers ()
 
void SetPtr (art::ProducesCollector *collector)
 
void InitaliseProducerPtr (reco::shower::ShowerProducedPtrsHolder &uniqueproducerPtrs)
 
virtual int AddAssociations (const art::Ptr< recob::PFParticle > &pfpPtr, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder)
 

Private Member Functions

double CalculatePosterior (std::string priorname, std::vector< double > &values, int &minprob, float &mean, float &likelihood)
 
double CalculatePosterior (std::string priorname, std::vector< double > &values)
 
bool isProbabilityGood (float &old_prob, float &new_prob)
 
bool isPosteriorProbabilityGood (double &prob, double &old_posteior)
 
bool CheckPoint (std::string priorname, double &value)
 
std::vector< double > GetLikelihooddEdxVec (double &electronprob, double &photonprob, std::string prior, std::vector< double > &dEdxVec)
 
std::vector< double > MakeSeed (std::vector< double > &dEdxVec)
 
void ForceSeedToFit (std::vector< double > &SeedTrack, std::string &prior, float &mean, double &posterior)
 
void RecurivelyAddHit (std::vector< double > &SeedTrack, std::vector< double > &dEdxVec, std::string &prior, int &SkippedHitsNum, float &old_mean, double &old_posteior)
 

Private Attributes

TH1F * electronpriorHist
 
TH1F * photonpriorHist
 
int fVerbose
 
std::string fdEdxInputLabel
 
int fNumSeedHits
 
float fProbSeedCut
 
float fProbPointCut
 
float fPostiorCut
 
int fnSkipHits
 
std::string fShowerdEdxOutputLabel
 
bool fDefineBestPlane
 
std::string fShowerBestPlaneOutputLabel
 

Additional Inherited Members

- Protected Member Functions inherited from ShowerRecoTools::IShowerTool
const shower::LArPandoraShowerAlgGetLArPandoraShowerAlg () const
 
template<class T >
art::Ptr< T > GetProducedElementPtr (std::string Name, reco::shower::ShowerElementHolder &ShowerEleHolder, int iter=-1)
 
template<class T >
void InitialiseProduct (std::string Name, std::string InstanceName="")
 
template<class T , class A , class B >
void AddSingle (A &a, B &b, std::string Name)
 
int GetVectorPtrSize (std::string Name)
 
void PrintPtrs ()
 
void PrintPtr (std::string Name)
 

Detailed Description

Definition at line 26 of file ShowerBayesianTrucatingdEdx_tool.cc.

Constructor & Destructor Documentation

ShowerRecoTools::ShowerBayesianTrucatingdEdx::ShowerBayesianTrucatingdEdx ( const fhicl::ParameterSet &  pset)

Definition at line 93 of file ShowerBayesianTrucatingdEdx_tool.cc.

94  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
95  , fVerbose(pset.get<int>("Verbose"))
96  , fdEdxInputLabel(pset.get<std::string>("dEdxInputLabel"))
97  , fNumSeedHits(pset.get<int>("NumSeedHits"))
98  , fProbSeedCut(pset.get<float>("ProbSeedCut"))
99  , fProbPointCut(pset.get<float>("ProbPointCut"))
100  , fPostiorCut(pset.get<float>("PostiorCut"))
101  , fnSkipHits(pset.get<int>("nSkipHits"))
102  , fShowerdEdxOutputLabel(pset.get<std::string>("ShowerdEdxOutputLabel"))
103  , fDefineBestPlane(pset.get<bool>("DefineBestPlane"))
104  , fShowerBestPlaneOutputLabel(pset.get<std::string>("ShowerBestPlaneOutputLabel"))
105  {
106 
107  //Get the prior file name
108  std::string fname;
109  cet::search_path sp("FW_SEARCH_PATH");
110  auto PriorPath = pset.get<std::string>("PriorFname");
111  if (!sp.find_file(PriorPath, fname)) {
112  throw cet::exception("ShowerBayesianTrucatingdEdx") << "Could not find the prior file";
113  }
114  std::string electron_histoname = pset.get<std::string>("PriorElectronHistoName");
115  std::string photon_histoname = pset.get<std::string>("PriorPhotonHistoName");
116 
117  TFile fin(fname.c_str(), "READ");
118  if (!fin.IsOpen()) {
119  throw cet::exception("ShowerBayesianTrucatingdEdx") << "Could read the prior file. Stopping";
120  }
121 
122  //Get the histograms.
123  electronpriorHist = dynamic_cast<TH1F*>(fin.Get(electron_histoname.c_str()));
124  if (!electronpriorHist) {
125  throw cet::exception("ShowerBayesianTrucatingdEdx") << "Could not read the electron hist";
126  }
127  photonpriorHist = dynamic_cast<TH1F*>(fin.Get(photon_histoname.c_str()));
128  if (!photonpriorHist) {
129  throw cet::exception("ShowerBayesianTrucatingdEdx") << "Could not read the photon hist ";
130  }
131 
132  if (electronpriorHist->GetNbinsX() != photonpriorHist->GetNbinsX()) {
133  throw cet::exception("ShowerBayesianTrucatingdEdx") << "Histrogram bins do not match";
134  }
135 
136  //Normalise the histograms.
137  electronpriorHist->Scale(1 / electronpriorHist->Integral());
138  photonpriorHist->Scale(1 / photonpriorHist->Integral());
139  }
string fname
Definition: demo.py:5
IShowerTool(const fhicl::ParameterSet &pset)
Definition: IShowerTool.h:29

Member Function Documentation

int ShowerRecoTools::ShowerBayesianTrucatingdEdx::CalculateElement ( const art::Ptr< recob::PFParticle > &  pfparticle,
art::Event &  Event,
reco::shower::ShowerElementHolder ShowerEleHolder 
)
overridevirtual

Implements ShowerRecoTools::IShowerTool.

Definition at line 142 of file ShowerBayesianTrucatingdEdx_tool.cc.

145  {
146 
147  //The idea , to some peoples distaste, is to attempt to improve the dEdx value by assuming
148  //The particle is either a) electron b) a e-e+ pair.
149  //We will take the start of track and work down until a few hits destory our postier probability.
150 
151  //Note: tried takeing the postior with the highest sum of the probabilitys on all three
152  // planes and on the 2 planes with the most hits. Made things worse.
153 
154  //Get the vectors of the dEdx Elements
155  if (!ShowerEleHolder.CheckElement(fdEdxInputLabel)) {
156  if (fVerbose)
157  mf::LogError("ShowerBayesianTrucatingdEdx")
158  << "Start position not set, returning " << std::endl;
159  return 1;
160  }
161 
162  std::map<int, std::vector<double>> dEdx_plane_final;
163  std::map<int, std::vector<double>> dEdx_vec_planes;
164  ShowerEleHolder.GetElement(fdEdxInputLabel, dEdx_vec_planes);
165 
166  //Do this for each plane;
167  for (auto const& dEdx_vec_plane : dEdx_vec_planes) {
168 
169  //Set up out final value if we don't have any points.
170  if (dEdx_vec_plane.second.size() < 1) {
171  dEdx_plane_final[dEdx_vec_plane.first] = {};
172  continue;
173  }
174 
175  std::vector<double> dEdx_vec = dEdx_vec_plane.second;
176 
177  double electronprob_eprior = 0;
178  double photonprob_eprior = 0;
179 
180  double electronprob_pprior = 0;
181  double photonprob_pprior = 0;
182 
183  std::vector<double> dEdx_electronprior =
184  GetLikelihooddEdxVec(electronprob_eprior, photonprob_eprior, "electron", dEdx_vec);
185  std::vector<double> dEdx_photonprior =
186  GetLikelihooddEdxVec(electronprob_pprior, photonprob_pprior, "photon", dEdx_vec);
187 
188  //Use the vector which maximises both priors.
189  if (electronprob_eprior < photonprob_pprior) {
190  dEdx_plane_final[dEdx_vec_plane.first] = dEdx_photonprior;
191  }
192  else {
193  dEdx_plane_final[dEdx_vec_plane.first] = dEdx_electronprior;
194  }
195 
196  } //Plane Loop
197 
198  //Calculate the median of the of dEdx.
199  std::vector<double> dEdx_final;
200  std::vector<double> dEdx_finalErr;
201 
202  int max_hits = -999;
203  int best_plane = -999;
204 
205  bool check = false;
206  for (auto const& dEdx_plane : dEdx_plane_final) {
207 
208  //Redefine the best plane
209  if ((int)(dEdx_plane.second).size() > max_hits) {
210  best_plane = dEdx_plane.first;
211  max_hits = (dEdx_plane.second).size();
212  }
213 
214  if ((dEdx_plane.second).empty()) {
215  dEdx_final.push_back(-999);
216  dEdx_finalErr.push_back(-999);
217  continue;
218  }
219 
220  dEdx_final.push_back(TMath::Median((dEdx_plane.second).size(), &(dEdx_plane.second)[0]));
221  dEdx_finalErr.push_back(-999);
222  check = true;
223  }
224 
225  //Check at least one plane has the information
226  if (!check) return 1;
227 
228  if (fDefineBestPlane) { ShowerEleHolder.SetElement(best_plane, fShowerBestPlaneOutputLabel); }
229 
230  ShowerEleHolder.SetElement(dEdx_final, dEdx_finalErr, fShowerdEdxOutputLabel);
231 
232  return 0;
233  }
std::vector< double > GetLikelihooddEdxVec(double &electronprob, double &photonprob, std::string prior, std::vector< double > &dEdxVec)
void SetElement(T &dataproduct, const std::string &Name, bool checktag=false)
std::size_t size(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:561
bool CheckElement(const std::string &Name) const
process_name tightIsolTest check
int GetElement(const std::string &Name, T &Element) const
double ShowerRecoTools::ShowerBayesianTrucatingdEdx::CalculatePosterior ( std::string  priorname,
std::vector< double > &  values,
int &  minprob,
float &  mean,
float &  likelihood 
)
private

Definition at line 246 of file ShowerBayesianTrucatingdEdx_tool.cc.

251  {
252 
253  //Posterior prob;
254  float posterior = 1;
255  float meanprob = 0;
256  float likelihood_other = 1;
257  likelihood = 1;
258 
259  //Minimum probability temp
260  float minprob_temp = 9999;
261  minprob_iter = 0;
262 
263  TH1F* prior_hist = nullptr;
264  TH1F* other_hist = nullptr;
265 
266  if (priorname == "electron") {
267  prior_hist = electronpriorHist;
268  other_hist = photonpriorHist;
269  }
270  if (priorname == "photon") {
271  prior_hist = photonpriorHist;
272  other_hist = electronpriorHist;
273  }
274 
275  TAxis* xaxis = prior_hist->GetXaxis();
276 
277  //Loop over the hits and calculate the probability
278  for (int i = 0; i < (int)values.size(); ++i) {
279 
280  float value = values[i];
281 
282  Int_t bin = xaxis->FindBin(value);
283 
284  float prob = -9999;
285  float other_prob = -9999;
286 
287  if (bin != xaxis->GetNbins() || bin == 0) {
288  //Calculate the likelihood
289  prob = prior_hist->GetBinContent(bin);
290  other_prob = other_hist->GetBinContent(bin);
291  }
292  else {
293  prob = 0;
294  other_prob = 0;
295  }
296 
297  if (prob < minprob_temp) {
298  minprob_temp = prob;
299  minprob_iter = i;
300  }
301 
302  if (prob == 0 && other_prob == 0) { continue; }
303 
304  //Calculate the posterior the mean probability and liklihood
305  meanprob += prior_hist->GetBinContent(bin);
306  likelihood *= prob;
307  likelihood_other *= other_prob;
308  }
309 
310  posterior = likelihood / (likelihood + likelihood_other);
311 
312  meanprob /= values.size();
313  mean = meanprob;
314  return posterior;
315  }
constexpr details::BinObj< T > bin(T value)
Returns a wrapper to print the specified data in binary format.
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
temporary value
double ShowerRecoTools::ShowerBayesianTrucatingdEdx::CalculatePosterior ( std::string  priorname,
std::vector< double > &  values 
)
private

Definition at line 236 of file ShowerBayesianTrucatingdEdx_tool.cc.

238  {
239  int minprob_iter = -999;
240  float mean = -999;
241  float likelihood = -999;
242  return CalculatePosterior(priorname, values, minprob_iter, mean, likelihood);
243  }
double CalculatePosterior(std::string priorname, std::vector< double > &values, int &minprob, float &mean, float &likelihood)
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
bool ShowerRecoTools::ShowerBayesianTrucatingdEdx::CheckPoint ( std::string  priorname,
double &  value 
)
private

Definition at line 318 of file ShowerBayesianTrucatingdEdx_tool.cc.

319  {
320 
321  TH1F* prior_hist = nullptr;
322 
323  if (priorname == "electron") { prior_hist = electronpriorHist; }
324  if (priorname == "photon") { prior_hist = photonpriorHist; }
325 
326  TAxis* xaxis = prior_hist->GetXaxis();
327 
328  Int_t bin = xaxis->FindBin(value);
329 
330  float prob = -9999;
331 
332  if (bin != xaxis->GetNbins() + 1 || bin == 0) {
333  //Calculate the likelihood
334  prob = prior_hist->GetBinContent(bin);
335  }
336  else {
337  prob = 0;
338  }
339 
340  //Return the probability of getting that point.
341  return prob > fProbPointCut;
342  }
constexpr details::BinObj< T > bin(T value)
Returns a wrapper to print the specified data in binary format.
temporary value
void ShowerRecoTools::ShowerBayesianTrucatingdEdx::ForceSeedToFit ( std::vector< double > &  SeedTrack,
std::string &  prior,
float &  mean,
double &  posterior 
)
private

Definition at line 399 of file ShowerBayesianTrucatingdEdx_tool.cc.

403  {
404 
405  int minprob_iter = 999;
406  float likelihood = -999;
407  float prob = CalculatePosterior(prior, SeedTrack, minprob_iter, mean, likelihood);
408  while ((mean < fProbSeedCut || prob <= 0) && SeedTrack.size() > 1) {
409 
410  //Remove the the worse point.
411  // std::cout << "removing hit with dEdx: " << SeedTrack.at(minprob_iter) << std::endl;
412  SeedTrack.erase(SeedTrack.begin() + minprob_iter);
413  minprob_iter = 999;
414 
415  //Recalculate
416  prob = CalculatePosterior(prior, SeedTrack, minprob_iter, mean, likelihood);
417  }
418  posterior = prob;
419  // std::cout << "seed has been fit with size: " << SeedTrack.size() << std::endl;
420  return;
421  }
double CalculatePosterior(std::string priorname, std::vector< double > &values, int &minprob, float &mean, float &likelihood)
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
std::vector< double > ShowerRecoTools::ShowerBayesianTrucatingdEdx::GetLikelihooddEdxVec ( double &  electronprob,
double &  photonprob,
std::string  prior,
std::vector< double > &  dEdxVec 
)
private

Definition at line 345 of file ShowerBayesianTrucatingdEdx_tool.cc.

349  {
350 
351  //have a pool
352  std::vector<double> dEdxVec_temp = dEdxVec;
353 
354  //Get The seed track.
355  std::vector<double> SeedTrack = MakeSeed(dEdxVec_temp);
356  // if(SeedTrack.size() < 1){
357  // return SeedTrack;
358  // }
359 
360  //Force the seed the be a good likelihood.
361  float mean = 999;
362  double posterior = 999;
363  ForceSeedToFit(SeedTrack, prior, mean, posterior);
364 
365  //Recursively add dEdx
366  int SkippedHitsNum = 0;
367  RecurivelyAddHit(SeedTrack, dEdxVec_temp, prior, SkippedHitsNum, mean, posterior);
368 
369  //Calculate the likelihood of the vector with the photon and electron priors.
370  electronprob = CalculatePosterior("electron", SeedTrack);
371  photonprob = CalculatePosterior("photon", SeedTrack);
372 
373  return SeedTrack;
374  }
std::vector< double > MakeSeed(std::vector< double > &dEdxVec)
void ForceSeedToFit(std::vector< double > &SeedTrack, std::string &prior, float &mean, double &posterior)
double CalculatePosterior(std::string priorname, std::vector< double > &values, int &minprob, float &mean, float &likelihood)
void RecurivelyAddHit(std::vector< double > &SeedTrack, std::vector< double > &dEdxVec, std::string &prior, int &SkippedHitsNum, float &old_mean, double &old_posteior)
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
bool ShowerRecoTools::ShowerBayesianTrucatingdEdx::isPosteriorProbabilityGood ( double &  prob,
double &  old_posteior 
)
inlineprivate

Definition at line 51 of file ShowerBayesianTrucatingdEdx_tool.cc.

52  {
53  return (old_posteior - prob) < fPostiorCut;
54  }
bool ShowerRecoTools::ShowerBayesianTrucatingdEdx::isProbabilityGood ( float &  old_prob,
float &  new_prob 
)
inlineprivate

Definition at line 45 of file ShowerBayesianTrucatingdEdx_tool.cc.

46  {
47  return (old_prob - new_prob) < fProbSeedCut;
48  }
std::vector< double > ShowerRecoTools::ShowerBayesianTrucatingdEdx::MakeSeed ( std::vector< double > &  dEdxVec)
private

Definition at line 377 of file ShowerBayesianTrucatingdEdx_tool.cc.

378  {
379 
380  std::vector<double> seed_vector;
381 
382  //Add the first hits to the seed
383  int MaxHit = fNumSeedHits;
384  if (fNumSeedHits > (int)dEdxVec.size()) { MaxHit = (int)dEdxVec.size(); }
385 
386  // if(MaxHit == 0){
387  // mf::LogError("ShowerBayesianTrucatingdEdx") << "Size of the vector is 0 cannot perform the dEdx cutting "<< std::endl;
388  //}
389 
390  for (int hit_iter = 0; hit_iter < MaxHit; ++hit_iter) {
391  seed_vector.push_back(dEdxVec[0]);
392  dEdxVec.erase(dEdxVec.begin());
393  }
394 
395  return seed_vector;
396  }
void ShowerRecoTools::ShowerBayesianTrucatingdEdx::RecurivelyAddHit ( std::vector< double > &  SeedTrack,
std::vector< double > &  dEdxVec,
std::string &  prior,
int &  SkippedHitsNum,
float &  old_mean,
double &  old_posteior 
)
private

Definition at line 424 of file ShowerBayesianTrucatingdEdx_tool.cc.

430  {
431 
432  //If we have no more hits to add then lets finish.
433  if (dEdxVec.size() < 1) { return; }
434 
435  bool ok = CheckPoint(prior, dEdxVec[0]);
436  // int minprob_iter=999;
437  // float mean = -999;
438  // float likelihood =999;
439  // if(ok){std::cout << "passed first cut" << std::endl;}
440  // else{std::cout << "failed first cut" << std::endl;}
441  // double prob = CalculatePosterior(prior,SeedTrack,minprob_iter,mean,likelihood);
442  // ok *= isProbabilityGood(mean,old_mean);
443  // if(ok){std::cout << "passed second cut" << std::endl;}
444  // else{std::cout << "failed second cut" << std::endl;}
445  // ok *= isPosteriorProbabilityGood(prob,old_posteior);
446  // if(ok){std::cout << "passed this cut" << std::endl;}
447  // else{std::cout << "failed third cut" << std::endl;}
448 
449  //If we failed lets try the next hits
450  if (!ok) {
451  // std::cout << "failed the pass point: " << dEdxVec[0] << " trying another hit" << SkippedHitsNum << std::endl;
452  //if(SeedTrack.size() > 1){SeedTrack.pop_back();}
453  ++SkippedHitsNum;
454  if (SkippedHitsNum > fnSkipHits) { return; }
455  }
456  else {
457  //Add the next point in question.
458  // std::cout << "adding value: " << dEdxVec[0] << std::endl;
459  //Reset the skip number
460  SkippedHitsNum = 0;
461  SeedTrack.push_back(dEdxVec[0]);
462  }
463 
464  //We have analysed this hit now lets remove it.
465  dEdxVec.erase(dEdxVec.begin());
466 
467  RecurivelyAddHit(SeedTrack, dEdxVec, prior, SkippedHitsNum, old_mean, old_posteior);
468 
469  return;
470  }
void RecurivelyAddHit(std::vector< double > &SeedTrack, std::vector< double > &dEdxVec, std::string &prior, int &SkippedHitsNum, float &old_mean, double &old_posteior)
bool CheckPoint(std::string priorname, double &value)

Member Data Documentation

TH1F* ShowerRecoTools::ShowerBayesianTrucatingdEdx::electronpriorHist
private

Definition at line 77 of file ShowerBayesianTrucatingdEdx_tool.cc.

std::string ShowerRecoTools::ShowerBayesianTrucatingdEdx::fdEdxInputLabel
private

Definition at line 82 of file ShowerBayesianTrucatingdEdx_tool.cc.

bool ShowerRecoTools::ShowerBayesianTrucatingdEdx::fDefineBestPlane
private

Definition at line 89 of file ShowerBayesianTrucatingdEdx_tool.cc.

int ShowerRecoTools::ShowerBayesianTrucatingdEdx::fnSkipHits
private

Definition at line 87 of file ShowerBayesianTrucatingdEdx_tool.cc.

int ShowerRecoTools::ShowerBayesianTrucatingdEdx::fNumSeedHits
private

Definition at line 83 of file ShowerBayesianTrucatingdEdx_tool.cc.

float ShowerRecoTools::ShowerBayesianTrucatingdEdx::fPostiorCut
private

Definition at line 86 of file ShowerBayesianTrucatingdEdx_tool.cc.

float ShowerRecoTools::ShowerBayesianTrucatingdEdx::fProbPointCut
private

Definition at line 85 of file ShowerBayesianTrucatingdEdx_tool.cc.

float ShowerRecoTools::ShowerBayesianTrucatingdEdx::fProbSeedCut
private

Definition at line 84 of file ShowerBayesianTrucatingdEdx_tool.cc.

std::string ShowerRecoTools::ShowerBayesianTrucatingdEdx::fShowerBestPlaneOutputLabel
private

Definition at line 90 of file ShowerBayesianTrucatingdEdx_tool.cc.

std::string ShowerRecoTools::ShowerBayesianTrucatingdEdx::fShowerdEdxOutputLabel
private

Definition at line 88 of file ShowerBayesianTrucatingdEdx_tool.cc.

int ShowerRecoTools::ShowerBayesianTrucatingdEdx::fVerbose
private

Definition at line 81 of file ShowerBayesianTrucatingdEdx_tool.cc.

TH1F* ShowerRecoTools::ShowerBayesianTrucatingdEdx::photonpriorHist
private

Definition at line 78 of file ShowerBayesianTrucatingdEdx_tool.cc.


The documentation for this class was generated from the following file: