MuonID.cxx
Go to the documentation of this file.
2 
3 #include <algorithm>
4 
5 #include "TSystem.h"
6 
7 namespace ana { namespace muonid_classifier
8 {
9  float TMVAvars[4];
10  TMVA::Reader* readerBDT ;
11 
12  // Fill the muonid and best track variables for this sr
13  float GetMuonIDSR(const caf::SRProxy* sr, const EMuonIDReturnMode return_mode)
14  {
15  if(!readerBDT) InitTMVA();
16 
17  float best_track = -999.0;
18  float muonid = -999.0;
19 
20  float dedxll = -999.0;
21  float scatll = -999.0;
22  float avgdedxlast10cm = -999.0;
23  float avgdedxlast40cm = -999.0;
24 
25  for (unsigned int itrk = 0; itrk < sr->trk.kalman.ntracks; ++itrk){
26  scatll = sr->trk.kalman.tracks[itrk].scatllh;
27  dedxll = sr->trk.kalman.tracks[itrk].dedxllh;
28 
29  if (sr->trk.kalman.tracks[itrk].avedEdxlast10cm < 0 or sr->trk.kalman.tracks[itrk].avedEdxlast40cm < 0) continue;
30 
31  avgdedxlast10cm = std::min(sr->trk.kalman.tracks[itrk].avedEdxlast10cm, float(30.));
32  avgdedxlast40cm = std::min(sr->trk.kalman.tracks[itrk].avedEdxlast40cm, float(30.));
33 
34  TMVAvars[0] = dedxll;
35  TMVAvars[1] = scatll;
36  TMVAvars[2] = avgdedxlast10cm;
37  TMVAvars[3] = avgdedxlast40cm;
38 
39  float newmuonid = readerBDT->EvaluateMVA("BDTG");
40  if (newmuonid > muonid){
41  best_track = itrk;
42  muonid = newmuonid;
43  }
44  }
45  // Check if we're returning the best track id, or the highest MuonID
46  if (return_mode == EMuonIDReturnMode::kGetTrack)
47  return best_track;
48  return muonid;
49  }
50 
51  float MuonID(const caf::SRProxy* sr)
52  {
53  return ana::muonid_classifier::GetMuonIDSR(sr); // Check the MuonID and return it
54  }
55 
56  float BestTrack(const caf::SRProxy* sr)
57  {
58  return ana::muonid_classifier::GetMuonIDSR(sr, ana::muonid_classifier::kGetTrack); // Check the MuonID and return highest scoring track
59  }
60 
61  //---------------------------------------------------
62  float InitTMVA()
63  {
64  // Initialize the static members
65  readerBDT = 0;
66  TMVAvars[0] = -999;
67  TMVAvars[1] = -999;
68  TMVAvars[2] = -999;
69  TMVAvars[3] = -999;
70 
71  if(!readerBDT) readerBDT = new TMVA::Reader();
72  const std::string srt_public = getenv("SRT_PUBLIC_CONTEXT");
73  const std::string srt_private = getenv("SRT_PRIVATE_CONTEXT");
74  const std::string weightfile_loc = "/NDAna/Classifiers/MuonID.weights.xml";
75 
76  // Default to SRT_PRIVATE_CONTEXT, then SRT_PUBLIC_CONTEXT
77  std::string bdt_weight_path;
78  if (!gSystem->AccessPathName((srt_private + weightfile_loc).c_str()))
79  bdt_weight_path = srt_private + weightfile_loc;
80  else
81  bdt_weight_path = srt_public + weightfile_loc;
82 
83  readerBDT->AddVariable("DedxLL", &TMVAvars[0]);
84  readerBDT->AddVariable("ScatLL", &TMVAvars[1]);
85  readerBDT->AddVariable("Avededxlast10cm", &TMVAvars[2]);
86  readerBDT->AddVariable("Avededxlast40cm", &TMVAvars[3]);
87  readerBDT->BookMVA("BDTG", bdt_weight_path);
88  return -5.0f;
89  }
90 } } // namespace ana::MubarClassifier
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
caf::Proxy< size_t > ntracks
Definition: SRProxy.h:1778
Proxy for caf::StandardRecord.
Definition: SRProxy.h:2126
A PID for muons.
Definition: FillPIDs.h:11
caf::Proxy< caf::SRTrackBranch > trk
Definition: SRProxy.h:2145
std::string getenv(std::string const &name)
caf::StandardRecord * sr
caf::Proxy< caf::SRKalman > kalman
Definition: SRProxy.h:1797
float BestTrack(const caf::SRProxy *sr)
Definition: MuonID.cxx:56
float MuonID(const caf::SRProxy *sr)
Definition: MuonID.cxx:51
caf::Proxy< std::vector< caf::SRKalmanTrack > > tracks
Definition: SRProxy.h:1780
T min(const caf::Proxy< T > &a, T b)
float GetMuonIDSR(const caf::SRProxy *sr, const EMuonIDReturnMode return_mode)
Definition: MuonID.cxx:13
TMVA::Reader * readerBDT
Definition: MuonID.cxx:10
enum BeamMode string