BPFPIdMaker_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // \file BPFPIdMaker_module.cc
4 // \brief Module to produce the BPF PId object.
5 // \version
6 // \author Michael Baird - mbaird42@fnal.gov & Jonathan Davies jpdavies@sussex.ac.uk
7 //
8 ////////////////////////////////////////////////////////////////////////
9 
10 // C/C++ includes
11 #include <cmath>
12 #include <iostream>
13 #include <vector>
14 
15 // ROOT includes
16 #include "TFile.h"
17 #include "TH2F.h"
18 #include "TMVA/Factory.h"
19 #include "TMVA/Tools.h"
20 #include "TMVA/Reader.h"
21 #include "TMVA/MethodKNN.h"
22 
23 // Framework includes
33 #include "fhiclcpp/ParameterSet.h"
35 
36 // NOvASoft includes
37 #include "RecoBase/CellHit.h"
38 #include "RecoBase/RecoHit.h"
39 #include "RecoBase/Cluster.h"
40 #include "RecoBase/Prong.h"
41 #include "RecoBase/Track.h"
42 #include "RecoBase/FitSum.h"
43 
45 #include "Utilities/AssociationUtil.h"
46 #include "Geometry/Geometry.h"
47 #include "Calibrator/Calibrator.h"
48 
52 
53 #include "cetlib/filesystem.h"
54 
55 
56 // BPFPIdMaker header
57 namespace bpfit {
58  class BPFPIdMaker : public art::EDProducer {
59  public:
60  explicit BPFPIdMaker(fhicl::ParameterSet const& pset);
61  ~BPFPIdMaker();
62 
63  void produce(art::Event& evt);
64  void reconfigure(const fhicl::ParameterSet& pset);
65  void beginRun(art::Run& run);
66  void resetVars();
67 
68  private:
70  std::string fProngLabel; ///< label for module that made the prongs
71  std::string fProngInstance; ///< instance label for the prongs to be used
72  std::string fTrackLabel; ///< label for module that made the tracks
73  std::string fLibPath; ///< location of all BPF library files listed below
74  std::string fHistoFile; ///< name of dE/dx log-likelihood histogram file
75 
76  std::string fMuonPIDMethod; ///< name of the TMVA method for the muon PID, fileName = "tmva_"+method+".xml"
77  std::string fPionPIDMethod; ///< name of the TMVA method for the pion PID, fileName = "tmva_"+method+".xml"
78  std::string fProtonPIDMethod; ///< name of the TMVA method for the proton PID, fileName = "tmva_"+method+".xml"
79  double fdxTOL; ///< lower limit for dx values to be used in dE/dx calculation
80 
81  bool fPIdAsMuon; ///< Create a Muon PID
82  bool fPIdAsPion; ///< Create a Pion PID
83  bool fPIdAsProton; ///< Create a Proton PID
84 
85  std::vector<int> fPartList; ///< List of particle types (by pdg code) to be used for PID creation
86 
87  TH2F *fdEdxVSbg[2][2]; ///< histo of computed dE/dx for cell hits (vs. beta*gamma)
88  bpfit::dEdxCalculator fdEdxCalc; ///< helper class for computing dEdx values
89 
90 
91  bool fBookedKNNs; ///< Have we yet booked the kNNs?
92 
93  TMVA::Reader *fMuonPID; ///< TMVA reader object for the muon PID
94  TMVA::Reader *fPionPID; ///< TMVA reader object for the pion PID -- jpdavies NOT implemented yet
95  TMVA::Reader *fProtonPID; ///< TMVA reader object for the proton PID -- jpdavies NOT implemented yet
96 
97  std::map<int, TMVA::Reader*> fPartListPID; ///< Map of particle pdg codes to TMVA reader object for that PID
98  std::map<int, std::string> fPartListPIDMethod; ///< Map of particle pdg codes to TMVA method for that PID
99 
100  //
101  // Variables used with the kNNs
102  //
103 
104  // muon PID variables:
105  float fLength; ///< track length
106  float fChi2T; ///< total chi^2
107  float fdEdxLL; ///< dEdx LL value
108  float fHitRatio; ///< ratio of track hits to prong hits (used as a track/shower discriminator)
109 
110  };
111 }
112 
113 
114 
115 namespace bpfit
116 {
117  //.......................................................................
119  fSlicerToken(consumes<std::vector<rb::Cluster>>(pset.get<std::string>("SlicerLabel"))),
120  fdEdxCalc()
121  {
122 
123 
124  produces< std::vector<BPFPId> >();
125  produces< art::Assns<BPFPId, rb::Track> >();
126 
127  this->reconfigure(pset);
128 
129  fBookedKNNs = false;
130  }
131 
132  //......................................................................
134  {
135  //======================================================================
136  // Clean up any memory allocated by your module... ...if you dare...
137  //======================================================================
138  }
139 
140  //......................................................................
142  {
143  fProngLabel = pset.get<std::string> ("ProngLabel");
144  fProngInstance = pset.get<std::string> ("ProngInstance");
145  fTrackLabel = pset.get<std::string> ("TrackLabel");
146  fLibPath = pset.get<std::string> ("LibPath");
147  fHistoFile = pset.get<std::string> ("HistoFile");
148  fMuonPIDMethod = pset.get<std::string> ("MuonPIDMethod");
149  fPionPIDMethod = pset.get<std::string> ("PionPIDMethod");
150  fProtonPIDMethod = pset.get<std::string> ("ProtonPIDMethod");
151  fdxTOL = pset.get<double> ("dxTOL");
152 
153  fPIdAsMuon = pset.get<bool> ("PIdAsMuon");
154  fPIdAsPion = pset.get<bool> ("PIdAsPion");
155  fPIdAsProton = pset.get<bool> ("PIdAsProton");
156 
157  fPartList.clear();
158  fPartListPID.clear();
159  fPartListPIDMethod.clear();
160 
161  if(fPIdAsMuon){
162  fPartList.push_back(13);
163  fMuonPID = new TMVA::Reader();
164  fPartListPID.insert(std::pair<int, TMVA::Reader*>(13,fMuonPID));
165  fPartListPIDMethod.insert(std::pair<int, std::string>(13, fMuonPIDMethod));
166  }
167  if(fPIdAsPion){
168  fPartList.push_back(211);
169  fPionPID = new TMVA::Reader();
170  fPartListPID.insert(std::pair<int, TMVA::Reader*>(211,fPionPID));
171  fPartListPIDMethod.insert(std::pair<int, std::string>(211, fPionPIDMethod));
172  }
173  if(fPIdAsProton){
174  fPartList.push_back(2212);
175  fProtonPID = new TMVA::Reader();
176  fPartListPID.insert(std::pair<int, TMVA::Reader*>(2212,fProtonPID));
177  fPartListPIDMethod.insert(std::pair<int, std::string>(2212, fProtonPIDMethod));
178  }
179 
180 
181 
182  }
183 
184  //......................................................................
186  {
187 
188  // Interpret the env variable LibPath
190 
191  //
192  // Read in the log-likelihood histos from the input file
193  //
194  std::cerr << "BPFPIdMaker: Opening dEdX file \"" << (libpath+fHistoFile) << "\"" << std::endl;
195  if(!cet::file_exists(libpath+fHistoFile)){
196  throw cet::exception("BPFPIdMaker")
197  << "dEdX file \"" << (libpath+fHistoFile) << "\" can't be found. Got the correct bpf UPS version?" << std::endl;
198  }
199 
200 
201  TFile infile((libpath+fHistoFile).c_str());
202 
203  fdEdxVSbg[0][0] = (TH2F*)infile.FindObjectAny("fdEdxVSbg_W0_X0");
204  fdEdxVSbg[0][1] = (TH2F*)infile.FindObjectAny("fdEdxVSbg_W0_X1");
205  fdEdxVSbg[1][0] = (TH2F*)infile.FindObjectAny("fdEdxVSbg_W1_X0");
206  fdEdxVSbg[1][1] = (TH2F*)infile.FindObjectAny("fdEdxVSbg_W1_X1");
207 
208  if(fdEdxVSbg[0][0] == 0 || fdEdxVSbg[0][1] == 0 || fdEdxVSbg[1][0] == 0 || fdEdxVSbg[1][1] == 0){
209  std::cerr << "BPFPIdMaker: ERROR: Failed to find dEdX Histograms" << std::endl;
210  }
211 
212  std::cerr << "BPFPIdMaker: Closing dEdX file" << std::endl;
213 
214  infile.Close();
215 
216 
217  if(!fBookedKNNs) {
218  //
219  // Set up the TMVA reader for the muon PID.
220  //
221  if(fPIdAsMuon){
222  fMuonPID->AddVariable("length",&fLength);
223  fMuonPID->AddVariable("chi2T",&fChi2T);
224  fMuonPID->AddVariable("dEdxLL",&fdEdxLL);
225  fMuonPID->AddVariable("hitRatio",&fHitRatio);
226  std::string xmlFileName = libpath+"tmva_"+fMuonPIDMethod+".xml";
227  std::cerr << "BPFPIdMaker: Booking MuonPID MVA of type \"" << fMuonPIDMethod << "\" with weights file File \"" << xmlFileName << "\"" << std::endl;
228  if(!cet::file_exists(xmlFileName)){
229  throw cet::exception("BPFPIdMaker")
230  << "TMVA weight file \"" << (xmlFileName) << "\" can't be found. Got the correct bpf UPS version?" << std::endl;
231  }
232 
233  fMuonPID->BookMVA(fMuonPIDMethod + " Method",xmlFileName);
234  }
235  if(fPIdAsPion){
236  fPionPID->AddVariable("length",&fLength);
237  fPionPID->AddVariable("chi2T",&fChi2T);
238  fPionPID->AddVariable("dEdxLL",&fdEdxLL);
239  std::string xmlFileName = libpath+"tmva_"+fPionPIDMethod+".xml";
240  std::cerr << "BPFPIdMaker: Booking PionPID MVA of type \"" << fPionPIDMethod << "\" with weights file File \"" << xmlFileName << "\"" << std::endl;
241  if(!cet::file_exists(xmlFileName)){
242  throw cet::exception("BPFPIdMaker")
243  << "TMVA weight file \"" << (xmlFileName) << "\" can't be found. Got the correct bpf UPS version?" << std::endl;
244  }
245  fPionPID->BookMVA(fPionPIDMethod + " Method",xmlFileName);
246  }
247  if(fPIdAsProton){
248  fProtonPID->AddVariable("length",&fLength);
249  fProtonPID->AddVariable("chi2T",&fChi2T);
250  fProtonPID->AddVariable("dEdxLL",&fdEdxLL);
251  fProtonPID->AddVariable("hitRatio",&fHitRatio);
252  std::string xmlFileName = libpath+"tmva_"+fProtonPIDMethod+".xml";
253  std::cerr << "BPFPIdMaker: Booking ProtonPID MVA of type \"" << fProtonPIDMethod << "\" with weights file File \"" << xmlFileName << "\"" << std::endl;
254  if(!cet::file_exists(xmlFileName)){
255  throw cet::exception("BPFPIdMaker")
256  << "TMVA weight file \"" << (xmlFileName) << "\" can't be found. Got the correct bpf UPS version?" << std::endl;
257  }
258  fProtonPID->BookMVA(fProtonPIDMethod + " Method",xmlFileName);
259  }
260 
261  fBookedKNNs = true;
262  }
263 
264  }
265 
266  //......................................................................
268  {
269  //
270  // PID variables:
271  //
272  fLength = -5.0;
273  fChi2T = 1.0e9;
274  fdEdxLL = -1.0e9;
275  fHitRatio = -5.0;
276  }
277 
278  //......................................................................
280  {
281 
282  // Get the slices and put them into a more convenient container.
284  evt.getByToken(fSlicerToken, slhandle);
286  for(unsigned int i=0; i < slhandle->size(); ++i) {
287  slices.push_back(art::Ptr<rb::Cluster>(slhandle,i));
288  }
289 
290  // Create the FindMany object for getting prongs.
292 
293  // Make the required service handles...
296 
297  // Make the containers for the BPFPId objects and their associations
298  std::unique_ptr< std::vector<BPFPId> >
299  PIds(new std::vector<BPFPId>);
300  std::unique_ptr< art::Assns<BPFPId,rb::Track> > PIdTrackAssns(new art::Assns<BPFPId,rb::Track>);
301 
302 
303  // loop over all slices and get the fuzzyk 3D prongs
304  for(unsigned int islice = 0; islice < slhandle->size(); ++islice) {
305 
306  // As usual, skip the noise slice...
307  if((*slhandle)[islice].IsNoise()) continue;
308 
309  // NOTE: Currently, the fundamental object of analysis is the slice.
310  // If in the future we change things so that the fundamental object
311  // of analysis becomes the vertex, then we will have to get prongs
312  // associated with the vertex instead.
313 
314  // Reset all variables.
315  this->resetVars();
316 
317  // get the 3D prongs associated with this slice
318  std::vector< art::Ptr< rb::Prong > > prongs;
319  if(prongFMP.isValid()) prongs = prongFMP.at(islice);
320 
321  // create the FindMany object for getting tracks
322  art::FindManyP<rb::Track> trackFMP(prongs, evt, fTrackLabel);
323 
324  //////////
325  //
326  // Step 1: Determine which prong is the most muon-like.
327  //
328  //////////
329 
330  // loop over all prongs and get the tracks
331  for(unsigned int iprong = 0; iprong < prongs.size(); ++iprong) {
332 
333  // get the tracks associated with this prong
334  std::vector< art::Ptr< rb::Track > > tracks;
335  if(trackFMP.isValid()) tracks = trackFMP.at(iprong);
336 
337  // create the FindMany object for getting FitSum objects
338  art::FindManyP<rb::FitSum> fitsumFMP(tracks, evt, fTrackLabel);
339 
340  // Loop over all tracks...
341  for(unsigned int itrack = 0; itrack < tracks.size(); ++itrack) {
342 
343  // get the FitSum object associated with this track
344  std::vector< art::Ptr< rb::FitSum > > fitsums;
345  if(fitsumFMP.isValid()) fitsums = fitsumFMP.at(itrack);
346 
347  // There can be only one!!!
348  // (something went really wrong with BPF if there's not one and
349  // only one of these per track.)
350  if(fitsums.size() != 1) abort();
351 
352 
353  float LLtemp = 0.0;
354 
355  // fill in chi2 and NDOF values
356  float tempH = 0.0;
357  float tempA = 0.0;
358  float tempN = 0.0;
359  tempH = fitsums[0]->Chi2(0)+fitsums[0]->Chi2(1); // chi2 from the hits
360  tempA = fitsums[0]->Chi2(2)+fitsums[0]->Chi2(3); // chi2 from the angles
361  tempN = fitsums[0]->Ndof(0)+fitsums[0]->Ndof(1)-4; // NDOF (NOTE: currently 4 is NOT subtracted from NDOF when the fitsum object is made by BPF)
362 
363  if(tempN <= 0) continue;
364 
365  // Keep track of the relevant info...
366  fChi2T = tempH/tempN + tempA/tempN;
367  fLength = tracks[itrack]->TotalLength();
368 
369  //
370  // Calculate dE/dx for each cell the track passes through using
371  // the helper class.
372  //
373  fdEdxCalc.computeDEDX(tracks[itrack],fitsums[0]->PDG());
374 
375  std::vector<double> dE;
376  std::vector<double> dx;
377  std::vector<double> W;
378  std::vector<double> BG;
379  std::vector<double> s; // this is a throw away variable...
380 
381  fdEdxCalc.getResults(dE,dx,W,BG,s,fdxTOL);
382 
383  //
384  // Compute the de/dx likelihood values for all cells.
385  //
386  for(unsigned int a = 0; a < dE.size(); ++a) {
387 
388  unsigned int w = 0;
389  unsigned int x = 0;
390 
391  if(W[a] > 0.0) w = 1;
392  if(dx[a] > 8.0) x = 1;
393 
394  // For the NearDet, we will just the "close to the readout end" FarDet histos.
395  if(geom->DetId() == novadaq::cnv::kNEARDET) w = 1;
396 
397  double content = fdEdxVSbg[w][x]->GetBinContent(fdEdxVSbg[w][x]->FindBin(log(BG[a]),dE[a]/dx[a]));
398  // We can't take the log of zero, so put a minimum on it.
399  if(content < 1.0e-9) content = 1.0e-9;
400  LLtemp += log(content);
401 
402  }
403 
404  if(dE.size() > 0) {
405  // Keep track of the relevant info...
406  fdEdxLL = LLtemp/(float)dE.size();
407  // NOTE: For this ratio, the number of hits on the track is taken to be the
408  // number of hits for which a value of dE/dx was successfully calculated.
409  fHitRatio = (float)dE.size()/(float)prongs[iprong]->NCell();
410  }
411  // end computing dE/dx LL info...
412 
413  BPFPId this_bpfpid;
414  this_bpfpid.SetPdg(fitsums[0]->PDG());
415  this_bpfpid.SetLength(fLength);
416  this_bpfpid.SetChi2T(fChi2T);
417  this_bpfpid.SetdEdXLL(fdEdxLL);
418  this_bpfpid.SetHitRatio(fHitRatio);
419 
420  // For now, this energy estimator only uses information from the muon track fit.
421  // jpdavies - correction - we will build in the ability to carry out the pion and proton PIDs as well
422  int this_pdg = std::numeric_limits<int>::min();
423  for(size_t part_index=0; part_index<fPartList.size();part_index++){
424  if(fitsums[0]->PDG() == fPartList.at(part_index)) this_pdg = fitsums[0]->PDG();
425  }
426  if(this_pdg==std::numeric_limits<int>::min()){
427  this_bpfpid.SetVal(-5);
428  PIds->push_back(this_bpfpid);
429  util::CreateAssn(*this,evt,*(PIds.get()),tracks[itrack],*(PIdTrackAssns.get()));
430  continue;
431  }
432 
433 
434  // Set the variables for the track with the best muonPID
435  if(fLength > 0.0 && fdEdxLL > -1.0e9 && fChi2T < 1.0e9) {
436  // Get the PID value...
437  TMVA::Reader* this_pid_reader = fPartListPID.at(fitsums[0]->PDG());
438  std::string this_pid_method = fPartListPIDMethod.at(fitsums[0]->PDG()) + " Method";
439  float pid = this_pid_reader->EvaluateMVA(this_pid_method);
440  this_bpfpid.SetVal(pid);
441  }
442 
443  //FIXME - jpdavies - I think this is how to create the PIDs and Associations between PIDs and tracks
444  // (again I think that is the association that we want!)
445  PIds->push_back(this_bpfpid);
446  util::CreateAssn(*this,evt,*(PIds.get()),tracks[itrack],*(PIdTrackAssns.get()));
447 
448  } // end loop over tracks (itrack)
449 
450  } // end loop over prongs (iprong)
451 
452  } // end loop over slhandle (islice)
453 
454  evt.put(std::move(PIds));
455  evt.put(std::move(PIdTrackAssns));
456 
457  }
458 
459 } // end namespace bpfit
460 ////////////////////////////////////////////////////////////////////////
461 
462 
463 
465 
466 namespace bpfit
467 {
469 }
std::map< int, TMVA::Reader * > fPartListPID
Map of particle pdg codes to TMVA reader object for that PID.
static bool CreateAssn(art::EDProducer const &prod, art::Event &evt, std::vector< T > &a, art::Ptr< U > b, art::Assns< T, U > &assn, size_t indx=UINT_MAX, std::string const &instance=std::string())
Create a 1 to 1 association between a new product and one already in the event.
float fdEdxLL
dEdx LL value
std::vector< int > fPartList
List of particle types (by pdg code) to be used for PID creation.
bpfit::dEdxCalculator fdEdxCalc
helper class for computing dEdx values
std::string fMuonPIDMethod
name of the TMVA method for the muon PID, fileName = "tmva_"+method+".xml"
void SetdEdXLL(double dEdXLL)
Definition: BPFPId.h:38
std::string fProtonPIDMethod
name of the TMVA method for the proton PID, fileName = "tmva_"+method+".xml"
TMVA::Reader * fProtonPID
TMVA reader object for the proton PID – jpdavies NOT implemented yet.
std::string EnvExpansion(const std::string &inString)
Function to expand environment variables.
Definition: EnvExpand.cxx:8
OStream cerr
Definition: OStream.cxx:7
void SetHitRatio(double hitRatio)
Definition: BPFPId.h:39
void SetLength(double length)
Definition: BPFPId.h:36
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
void computeDEDX(art::Ptr< rb::Track > &track, int pdg=13)
Compute dE/dx for all cells along this track and the fitsum object that went with it...
bool fPIdAsProton
Create a Proton PID.
const art::ProductToken< std::vector< rb::Cluster > > fSlicerToken
DEFINE_ART_MODULE(TestTMapFile)
std::map< int, std::string > fPartListPIDMethod
Map of particle pdg codes to TMVA method for that PID.
double dE
void reconfigure(const fhicl::ParameterSet &pset)
bool fBookedKNNs
Have we yet booked the kNNs?
Definition: Run.h:31
float fChi2T
total chi^2
Calculates dE/dx in cells passed through by a track.
std::string fTrackLabel
label for module that made the tracks
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
const XML_Char * s
Definition: expat.h:262
void produce(art::Event &evt)
TMVA::Reader * fPionPID
TMVA reader object for the pion PID – jpdavies NOT implemented yet.
std::string fHistoFile
name of dE/dx log-likelihood histogram file
double dx[NP][NC]
TMVA::Reader * fMuonPID
TMVA reader object for the muon PID.
string infile
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
float fLength
track length
const double a
std::string fLibPath
location of all BPF library files listed below
T get(std::string const &key) const
Definition: ParameterSet.h:231
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
std::string fProngLabel
label for module that made the prongs
double fdxTOL
lower limit for dx values to be used in dE/dx calculation
void SetChi2T(double chi2T)
Definition: BPFPId.h:37
Near Detector in the NuMI cavern.
bool fPIdAsMuon
Create a Muon PID.
Perform a "2 point" Hough transform on a collection of hits.
Definition: run.py:1
void beginRun(art::Run &run)
void SetPdg(int pdg)
Definition: PID.h:23
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
float fHitRatio
ratio of track hits to prong hits (used as a track/shower discriminator)
std::string fProngInstance
instance label for the prongs to be used
bool fPIdAsPion
Create a Pion PID.
void geom(int which=0)
Definition: geom.C:163
bool file_exists(std::string const &qualified_filename)
BPFPIdMaker(fhicl::ParameterSet const &pset)
std::string fPionPIDMethod
name of the TMVA method for the pion PID, fileName = "tmva_"+method+".xml"
bool getByToken(ProductToken< PROD > const &token, Handle< PROD > &result) const
Definition: DataViewImpl.h:387
void SetVal(double val)
Definition: PID.h:24
Float_t e
Definition: plot.C:35
TH2F * fdEdxVSbg[2][2]
histo of computed dE/dx for cell hits (vs. beta*gamma)
Float_t w
Definition: plot.C:20
#define W(x)
ProductToken< T > consumes(InputTag const &)
void getResults(std::vector< double > &dEtemp, std::vector< double > &dxtemp, std::vector< double > &Wtemp, std::vector< double > &bgtemp, std::vector< double > &stemp, double dxTol)
Return the results of the dE/dx calculation.
Encapsulate the geometry of one entire detector (near, far, ndos)