WSNumu_module.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////
2 /// \brief Wrong-sign BDT estimator for Numu analysis
3 /// \author Bruno Zamorano B.Zamorano@sussex.ac.uk
4 ////////////////////////////////////////////////////////
5 #include <string>
6 #include "TMath.h"
7 
13 #include "fhiclcpp/ParameterSet.h"
15 
17 #include "Geometry/Geometry.h"
18 #include "NovaDAQConventions/DAQConventions.h"
19 #include "RecoBase/CellHit.h"
20 #include "RecoBase/Cluster.h"
21 #include "RecoBase/Track.h"
22 #include "ReMId/ReMId.h"
23 #include "NumuEnergy/NumuE.h"
24 #include "TimingFit/TimingFitAlg.h"
25 #include "Utilities/AssociationUtil.h"
27 #include "CVN/func/Result.h"
28 
29 #include "TFile.h"
30 #include "TMVA/Reader.h"
31 
32 namespace wsnumu {
33 
34  class WSNumu : public art::EDProducer {
35  public:
36  explicit WSNumu(fhicl::ParameterSet const & pset);
37  virtual ~WSNumu();
38 
39  void produce (art::Event & evt);
40  void beginRun(art::Run& run);
41 
42  protected:
43 
50  TMVA::Reader* fReader;
51  float TMVAvars[6];
52  };
53 }
54 
55 
56 namespace wsnumu
57 {
58 
59  //----------------------------------------------------------------------
61  fSlicerLabel (pset.get< std::string >("SlicerLabel")),
62  fTrackLabel (pset.get< std::string >("TrackLabel")),
63  fPIDLabel (pset.get< std::string >("PIDLabel")),
64  fEnergyLabel (pset.get< std::string >("EnergyLabel")),
65  fTMVApath (pset.get< std::string >("tmvapath")),
66  fUseLongestTrack (pset.get< bool >("UseLongestTrack")),
67  fReader (nullptr)
68  {
69  produces< std::vector<wsnumu::WSNumuObj> >();
70  produces< art::Assns<wsnumu::WSNumuObj, rb::Cluster> >();
71 
72  }
73 
74 
75  //----------------------------------------------------------------------
77  {
78  if(fReader) delete fReader;
79  }
80 
81  //-------------------------------------------------------------------
83  {
84 
86 
87  if(geom->DetId() == novadaq::cnv::kNEARDET) {
88 
89  fReader = new TMVA::Reader;
90  fReader->AddVariable("theta",&TMVAvars[0]);
91  fReader->AddVariable("hadEhit",&TMVAvars[1]);
92  fReader->AddVariable("hadEfrac",&TMVAvars[2]);
93  fReader->AddVariable("muonE",&TMVAvars[3]);
94  fReader->AddVariable("nPlaneGap",&TMVAvars[4]);
95  fReader->AddSpectator("PDGcode",&TMVAvars[5]);
97  fReader->BookMVA("BDTG",pidpath+"WSNumu_BDTG_prod3_NDtraining.weights.xml");
98 
99  }
100 
101  } //end beginRun
102 
103  //-------------------------------------------------------------------
105  {
106 
109  evt.getByLabel(fSlicerLabel,slicevec);
110 
111  if(slicevec->empty()) {
112  mf::LogWarning ("No Slices")<<"No Slices in the input file";
113  return;
114  }
115 
116  art::FindManyP<rb::Track> trackAssnList(slicevec, evt, fTrackLabel);
117  art::FindOneP<numue::NumuE> sliceEnergyList(slicevec, evt, fEnergyLabel);
118 
119  std::unique_ptr< std::vector<wsnumu::WSNumuObj> > outputObjects(new std::vector<wsnumu::WSNumuObj>);
120  std::unique_ptr< art::Assns<wsnumu::WSNumuObj, rb::Cluster> > assoc(new art::Assns<wsnumu::WSNumuObj, rb::Cluster>);
121 
122  for(size_t i = 0; i < slicevec->size(); ++i){
123 
124  art::Ptr<rb::Cluster> slice(slicevec, i);
125 
126  if(slice->IsNoise()) {
127  continue;
128  }
129 
130  wsnumu::WSNumuObj wsnumuobj; // initialization in constructor
131 
132  unsigned int bestIdx = 999; // for index in list of the highest ReMId PID track
133 
134  if(!trackAssnList.isValid()) {
135  mf::LogWarning("WSNumu") << "WSNumu: No Kalman Tracks!";
136  outputObjects->push_back(wsnumuobj);
137  util::CreateAssn(*this, evt, *(outputObjects.get()), slice, *(assoc.get()));
138  continue;
139  }
140 
141  const std::vector< art::Ptr<rb::Track> > sliceTracks = trackAssnList.at(i);
142  art::FindOneP<remid::ReMId> remidVec(sliceTracks, evt, fPIDLabel);
143 
144  if(fUseLongestTrack) {
145  bestIdx = 0;
146  if(sliceTracks.size() == 0) {
147  outputObjects->push_back(wsnumuobj);
148  continue;
149  }
150  const art::Ptr<rb::Track> track = sliceTracks[0];
151  if(!track->Is3D()) {
152  outputObjects->push_back(wsnumuobj);
153  continue;
154  }
155  }
156  else if(!remidVec.isValid()) {
157  mf::LogWarning("WSNumu") << "WSNumu: No ReMId information!";
158  outputObjects->push_back(wsnumuobj);
159  util::CreateAssn(*this, evt, *(outputObjects.get()), slice, *(assoc.get()));
160  continue;
161  }
162  else {
163  bestIdx = remid::HighestPIDTrack(sliceTracks, fPIDLabel, evt);
164  }
165 
166 
167  if(geom->DetId() == novadaq::cnv::kNEARDET) { // Only for ND
168  // Main condition
169  if(bestIdx!=999) {
170 
171  float theta = 0.;
172  float hadEhit = 0.;
173  float hadEfrac = 0.;
174  float muonE = 0.;
175  int nPlaneGap = 0;
176 
177  const art::Ptr<rb::Track> track = sliceTracks[bestIdx];
178  const art::Ptr<numue::NumuE> energy = sliceEnergyList.at(i);
179  if (!energy) continue;
180 
181  double tdx = track->Dir().X();
182  double tdy = track->Dir().Y();
183  double tdz = track->Dir().Z();
184 
185  if(!(tdx>=-1&&tdx<=1&&tdy>=-1&&tdy<=1&&tdz>=-1&&tdz<=1)) continue; // mis-reconstructed track
186 
187  theta = TMath::ACos(tdz)*180./TMath::Pi();
188  wsnumuobj.SetTheta(theta);
189 
190  if( energy->HadCluster().NCell() > 0 ) {
191  hadEhit = energy->HadCalE() / energy->HadCluster().NCell();
192  }
193  wsnumuobj.SetHadEhit(hadEhit);
194 
195  if( energy->TrkCCE() > 0 ) {
196  hadEfrac = energy->RecoTrkCCHadE() / energy->TrkCCE();
197  }
198  wsnumuobj.SetHadEfrac(hadEfrac);
199 
200  if( energy->RecoMuonE() > 0 ) {
201  muonE = energy->RecoMuonE();
202  }
203  wsnumuobj.SetMuonE(muonE);
204 
205  nPlaneGap = track->NMissingPlanes(geo::kXorY);
206  wsnumuobj.SetNPlaneGap(nPlaneGap);
207 
208  //---
209  TMVAvars[0] = wsnumuobj.Theta();
210  TMVAvars[1] = wsnumuobj.HadEhit();
211  TMVAvars[2] = wsnumuobj.HadEfrac();
212  TMVAvars[3] = wsnumuobj.MuonE();
213  TMVAvars[4] = wsnumuobj.NPlaneGap();
214  TMVAvars[5] = 0; // Spectator(s)
215 
216  wsnumuobj.SetWSPID(fReader->EvaluateMVA("BDTG"));
217  }
218  } // end if good track
219 
220  outputObjects->push_back(wsnumuobj); // write out object with all information
221  util::CreateAssn(*this, evt, *(outputObjects.get()), slice, *(assoc.get()));
222 
223  } //end loop over slices
224 
225  evt.put(std::move(outputObjects));
226  evt.put(std::move(assoc));
227 
228  } //end produce
229 
230 } //end namespace
231 
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.
unsigned int NCell(geo::View_t view) const
Number of cells in view view.
Definition: Cluster.cxx:134
void SetHadEfrac(float hadefrac)
hadronic energy fraction of the slice
Definition: WSNumuObj.cxx:35
void SetWSPID(float wspid)
numu-focused wrong sign PID
Definition: WSNumuObj.cxx:53
X or Y views.
Definition: PlaneGeo.h:30
Wrong-sign BDT estimator for Numu analysis.
std::string EnvExpansion(const std::string &inString)
Function to expand environment variables.
Definition: EnvExpand.cxx:8
Definition: event.h:19
WSNumu(fhicl::ParameterSet const &pset)
float TrkCCE() const
Definition: NumuE.cxx:254
float HadEhit() const
Definition: WSNumuObj.cxx:65
DEFINE_ART_MODULE(TestTMapFile)
void SetHadEhit(float hadehit)
hadronic energy per hit in the slice
Definition: WSNumuObj.cxx:29
rb::Cluster HadCluster() const
Definition: NumuE.cxx:389
float RecoTrkCCHadE() const
Definition: NumuE.cxx:274
Definition: Run.h:31
float RecoMuonE() const
Definition: NumuE.cxx:269
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
void SetNPlaneGap(int nplanegap)
number of plane gaps in the main track
Definition: WSNumuObj.cxx:47
virtual ~WSNumu()
std::string fPIDLabel
Result for CVN.
void SetMuonE(float muonE)
muon energy
Definition: WSNumuObj.cxx:41
float MuonE() const
Definition: WSNumuObj.cxx:77
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
virtual TVector3 Dir() const
Unit vector describing prong direction.
Definition: Prong.h:77
int evt
double energy
Definition: plottest35.C:25
int NPlaneGap() const
Definition: WSNumuObj.cxx:83
Near Detector in the NuMI cavern.
float TMVAvars[6]
TMVA::Reader * fReader
void SetTheta(float theta)
direction angle of the leading muon (in deg)
Definition: WSNumuObj.cxx:23
Definition: run.py:1
float HadCalE() const
Definition: NumuE.cxx:279
int NMissingPlanes(geo::View_t view) const
Total number of missing planes in cluster.
Definition: Cluster.cxx:693
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
std::string fEnergyLabel
void produce(art::Event &evt)
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
void beginRun(art::Run &run)
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
void geom(int which=0)
Definition: geom.C:163
std::string fSlicerLabel
std::string fTrackLabel
std::string fTMVApath
bool IsNoise() const
Is the noise flag set?
Definition: Cluster.h:163
Encapsulate the geometry of one entire detector (near, far, ndos)
virtual bool Is3D() const
Definition: Prong.h:71
unsigned int HighestPIDTrack(const std::vector< art::Ptr< rb::Track > > &sliceTracks, const std::string &remidModuleLabel, const art::Event &e)
Definition: ReMId.cxx:249
float Theta() const
Definition: WSNumuObj.cxx:59
float HadEfrac() const
Definition: WSNumuObj.cxx:71