RegCNNVertexAna_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////////////////////
2 // Class: RegCNNVertexAna
3 // Module Type: analyzer
4 // File: RegCNNVertexAna_module.cc
5 // Author:
6 ////////////////////////////////////////////////////////////////////////////////////////////////
7 
8 #include <string>
9 #include <vector>
10 #include "TTree.h"
11 #include "TBranch.h"
12 
13 // Framework includes:
18 #include "fhiclcpp/ParameterSet.h"
19 #include "art_root_io/TFileService.h"
20 #include "art_root_io/TFileDirectory.h"
22 
30 
31 const int kMax = 1000;
32 namespace myana {
33  class RegCNNVertexAna;
34 }
35 
37 {
38  public:
40  void analyze(art::Event const& evt);
41  void reconfigure(fhicl::ParameterSet const& p);
42  void reset();
43 
44  private:
45  TTree* fTree;
46  int ievt;
47  double trueEnergy;
49  float CNN_vertex[3];
52  float CNN_vertex2[3];
53 
54  int InDet;
55  int FidCut;
56  int nhits;
61  double nueng_truth[kMax];
62  double nuvtxx_truth[kMax];
63  double nuvtxy_truth[kMax];
64  double nuvtxz_truth[kMax];
65 
66  double ErecoNue;
67  double RecoLepEnNue;
68  double RecoHadEnNue;
70 
71 
76 
77 
81 
83 
86 
91 
96 
97 
99 
100 };
101 
103 {
104  this->reconfigure(pset);
105  fTree = tfs->make<TTree>("anatree", "anatree");
106  fTree->Branch("ievent", &ievt, "ievent/I");
107  fTree->Branch("InDet", &InDet, "InDet/I");
108  fTree->Branch("FidCut", &FidCut, "FidCut/I");
109  fTree->Branch("TrueEnergy", &trueEnergy, "TrueEnergy/D");
110  fTree->Branch("CNNEnergy", &regcnn_energy, "CNNEnergy/F");
111  fTree->Branch("CNNVertex", CNN_vertex, "CNNVertex[3]/F");
112  fTree->Branch("CNNVertex1ststep", CNN_vertex1ststep, "CNNVertex1ststep[3]/F");
113  fTree->Branch("CNNVertex2ndstep", CNN_vertex2ndstep, "CNNVertex2ndstep[3]/F");
114  fTree->Branch("CNNVertex2", CNN_vertex2, "CNNVertex2[3]/F");
115 
116  fTree->Branch("NuTruthN", &nu_truth_N, "NuTruthN/I");
117  fTree->Branch("NuEngTruth", nueng_truth, "NuEngTruth[NuTruthN]/D");
118  fTree->Branch("NuPDGTruth", nupdg_truth, "NuPDGTruth[NuTruthN]/I");
119  fTree->Branch("NuModeTruth", numode_truth, "NuModeTruth[NuTruthN]/I");
120  fTree->Branch("NuCCNCTruth", nuccnc_truth, "NuCCNCTruth[NuTruthN]/I");
121 
122  fTree->Branch("NuVtxXTruth", nuvtxx_truth, "NuVtxXTruth[NuTruthN]/D");
123  fTree->Branch("NuVtxYTruth", nuvtxy_truth, "NuVtxYTruth[NuTruthN]/D");
124  fTree->Branch("NuVtxZTruth", nuvtxz_truth, "NuVtxZTruth[NuTruthN]/D");
125 
126  fTree->Branch("PandNuVtx", &pandora_nu_vtx, "PandNuVtx/I");
127  fTree->Branch("PandNuVertexX", &pandora_nu_vtx_x, "PandNuVertexX/D");
128  fTree->Branch("PandNuVertexY", &pandora_nu_vtx_y, "PandNuVertexY/D");
129  fTree->Branch("PandNuVertexZ", &pandora_nu_vtx_z, "PandNuVertexZ/D");
130 
131  fTree->Branch("NHits", &nhits, "NHits/I");
132 
133  fTree->Branch("ErecoNue", &ErecoNue, "ErecoNue/D");
134  fTree->Branch("RecoLepEnNue", &RecoLepEnNue, "RecoLepEnNue/D");
135  fTree->Branch("RecoHadEnNue", &RecoHadEnNue, "RecoHadEnNue/D");
136  fTree->Branch("RecoMethodNue", &RecoMethodNue, "RecoMethodNue/I");
137 
138 
139 }
140 
142 {
143  fMCGenModuleLabel = pset.get<std::string>("MCGenModuleLabel");
144  fHitsModuleLabel = pset.get<std::string>("HitsModuleLabel");
145  fEnergyRecoNueLabel = pset.get<std::string>("EnergyRecoNueLabel");
146 
147  fPandoraNuVertexModuleLabel = pset.get<std::string>("PandoraNuVertexModuleLabel");
148 
149  fRegCNNResultLabel = pset.get<std::string>("RegCNNEngResultLabel");
150  fRegCNNModuleLabel = pset.get<std::string>("RegCNNEngModuleLabel");
151 
152  fRegCNNVtxResultLabel = pset.get<std::string>("RegCNNVtxResultLabel");
153  fRegCNNVtx1ststepResultLabel = pset.get<std::string>("RegCNNVtx1ststepResultLabel");
154  fRegCNNVtx2ndstepResultLabel = pset.get<std::string>("RegCNNVtx2ndstepResultLabel");
155  fRegCNNVtxResult2Label = pset.get<std::string>("RegCNNVtxResult2Label");
156 
157  fRegCNNVtxModuleLabel = pset.get<std::string>("RegCNNVtxModuleLabel");
158  fRegCNNVtx1ststepModuleLabel = pset.get<std::string>("RegCNNVtx1ststepModuleLabel");
159  fRegCNNVtx2ndstepModuleLabel = pset.get<std::string>("RegCNNVtx2ndstepModuleLabel");
160  fRegCNNVtxModule2Label = pset.get<std::string>("RegCNNVtxModule2Label");
161 
162 }
163 
165 {
166  this->reset();
167  ievt = evt.id().event();
168  bool isMC = !evt.isRealData();
169 
170  // * MC truth information
171 
172  std::vector<art::Ptr<simb::MCTruth> > mclist;
173  if (isMC){
174  auto mctruthListHandle = evt.getHandle< std::vector<simb::MCTruth> >(fMCGenModuleLabel);
175  if (mctruthListHandle)
176  art::fill_ptr_vector(mclist, mctruthListHandle);
177  }
178 
179  // Get the hits out of the event record
180  std::vector<art::Ptr<recob::Hit> > hits;
181  auto hitHandle = evt.getHandle<std::vector<recob::Hit> >(fHitsModuleLabel);
182  if (hitHandle)
183  art::fill_ptr_vector(hits, hitHandle);
184 
185  // Get DUNE energy Reco
186  auto engrecoHandle = evt.getHandle<dune::EnergyRecoOutput>(fEnergyRecoNueLabel);
187 
188  // Get RegCNN Results
190  auto cnnresultListHandle = evt.getHandle<std::vector<cnn::RegCNNResult>>(itag);
191 
193  auto cnnvtxresultListHandle = evt.getHandle<std::vector<cnn::RegCNNResult>>(itag2);
194 
196  auto cnnvtxresultListHandle1st = evt.getHandle<std::vector<cnn::RegCNNResult>>(itag3);
197 
199  auto cnnvtxresultListHandle2nd = evt.getHandle<std::vector<cnn::RegCNNResult>>(itag4);
200 
202  auto cnnvtxresultListHandle2 = evt.getHandle<std::vector<cnn::RegCNNResult>>(itag5);
203 
204  // Get Truth information
205  if (mclist.size()>0)
206  {
207  int neutrino_i = 0;
208  for(size_t iList = 0; (iList < mclist.size()) && (neutrino_i < kMax) ; ++iList)
209  {
210  if (mclist[iList]->NeutrinoSet())
211  {
212  nueng_truth[neutrino_i] = mclist[iList]->GetNeutrino().Nu().Momentum().E();
213  nupdg_truth[neutrino_i] = mclist[iList]->GetNeutrino().Nu().PdgCode();
214  nuccnc_truth[neutrino_i] = mclist[iList]->GetNeutrino().CCNC();
215  numode_truth[neutrino_i] = mclist[iList]->GetNeutrino().Mode();
216 
217  nuvtxx_truth[neutrino_i] = mclist[iList]->GetNeutrino().Nu().Vx();
218  nuvtxy_truth[neutrino_i] = mclist[iList]->GetNeutrino().Nu().Vy();
219  nuvtxz_truth[neutrino_i] = mclist[iList]->GetNeutrino().Nu().Vz();
220 
221  neutrino_i++;
222  }
223  }
224  nu_truth_N = neutrino_i;
225  }
226  // Get Hit information
227  nhits = hits.size();
228  InDet = 0;
229  //cut with true vertex in fiducial volume
230  FidCut = 0;
231  if (nu_truth_N>0){
232  if(fabs(nuvtxx_truth[0]) < 310. && fabs(nuvtxy_truth[0]) < 550. && nuvtxz_truth[0] > 50. && nuvtxz_truth[0] < 1250.) FidCut = 1;
233  }
234 
235  // Pandora Nu Vertex
236  lar_pandora::PFParticleVector particleVector;
238  lar_pandora::VertexVector vertexVector;
239  lar_pandora::PFParticlesToVertices particlesToVertices;
240  lar_pandora::LArPandoraHelper::CollectVertices(evt, fPandoraNuVertexModuleLabel, vertexVector, particlesToVertices);
241 
242  pandora_nu_vtx = 0, pandora_nu_vtx_x = -10000, pandora_nu_vtx_y = -10000, pandora_nu_vtx_z = -10000;
243 
244  double xyz_temp[3] = {0.0, 0.0, 0.0} ;
245  for (unsigned int ipfp = 0; ipfp < particleVector.size(); ipfp++){
246  const art::Ptr<recob::PFParticle> particle = particleVector.at(ipfp);
247  if (!particle->IsPrimary()) continue;
248 
249  // Particles <-> Vertices
250  lar_pandora::PFParticlesToVertices::const_iterator vIter = particlesToVertices.find(particle);
251  if (particlesToVertices.end() != vIter)
252  {
253  const lar_pandora::VertexVector &vertexVector = vIter->second;
254  if (!vertexVector.empty())
255  {
256  if (vertexVector.size() !=1)
257  std::cout << " Warning: Found particle with more than one associated vertex " << std::endl;
258 
259  const art::Ptr<recob::Vertex> vertex_pfp = *(vertexVector.begin());
260  vertex_pfp->XYZ(xyz_temp);
261 
262  pandora_nu_vtx = 1;
263  pandora_nu_vtx_x = xyz_temp[0];
264  pandora_nu_vtx_y = xyz_temp[1];
265  pandora_nu_vtx_z = xyz_temp[2];
266  }
267  } // end of if
268  } // end of loop particleVector
269 
270 
271 
272  // Get RecoE from DUNE
273  if (!engrecoHandle.failedToGet())
274  {
275  ErecoNue = engrecoHandle->fNuLorentzVector.E();
276  RecoLepEnNue = engrecoHandle->fLepLorentzVector.E();
277  RecoHadEnNue = engrecoHandle->fHadLorentzVector.E();
278  RecoMethodNue = engrecoHandle->recoMethodUsed;
279  std::cout<< ErecoNue << std::endl;
280  }
281 
282  // Get RegCNN Results
283  if (!cnnresultListHandle.failedToGet())
284  {
285  if (!cnnresultListHandle->empty())
286  {
287  const std::vector<float>& v = (*cnnresultListHandle)[0].fOutput;
288  for (unsigned int ii = 0; ii < 1; ii++){
289  regcnn_energy = v[ii];
290  }
291  }
292  }
293 
294 
295  // Get RegCNN Results
296  if (!cnnvtxresultListHandle.failedToGet())
297  {
298  if (!cnnvtxresultListHandle->empty())
299  {
300  const std::vector<float>& v = (*cnnvtxresultListHandle)[0].fOutput;
301  for (unsigned int ii = 0; ii < 3; ii++){
302  CNN_vertex[ii] = v[ii];
303  }
304  }
305  }
306 
307  // Get RegCNN Results
308  if (!cnnvtxresultListHandle2.failedToGet())
309  {
310  if (!cnnvtxresultListHandle2->empty())
311  {
312  const std::vector<float>& v = (*cnnvtxresultListHandle2)[0].fOutput;
313  for (unsigned int ii = 0; ii < 3; ii++){
314  CNN_vertex2[ii] = v[ii];
315  }
316  }
317  }
318 
319  // Get RegCNN Results
320  if (!cnnvtxresultListHandle1st.failedToGet())
321  {
322  if (!cnnvtxresultListHandle1st->empty())
323  {
324  const std::vector<float>& v = (*cnnvtxresultListHandle1st)[0].fOutput;
325  //std::cout << v[0] << std::endl;
326  for (unsigned int ii = 0; ii < 3; ii++){
327  CNN_vertex1ststep[ii] = v[ii];
328  }
329  }
330  }
331  // Get RegCNN Results
332  if (!cnnvtxresultListHandle2nd.failedToGet())
333  {
334  if (!cnnvtxresultListHandle2nd->empty())
335  {
336  const std::vector<float>& v = (*cnnvtxresultListHandle2nd)[0].fOutput;
337  //std::cout << v[0] << std::endl;
338  for (unsigned int ii = 0; ii < 3; ii++){
339  CNN_vertex2ndstep[ii] = v[ii];
340  }
341  }
342  }
343 
344 
345 
346  // fill entry
347  fTree->Fill();
348 }
349 
351 {
352  ievt = -9999;
353  trueEnergy = -99999;
354  regcnn_energy = -99999;
355  ErecoNue = -99999;
356  RecoLepEnNue = -99999;
357  RecoHadEnNue = -99999;
358  RecoMethodNue = -99999;
359 
360  for (int ii = 0; ii < 3; ii++){
361  CNN_vertex[ii] = -99999;
362  CNN_vertex1ststep[ii] = -99999;
363  CNN_vertex2ndstep[ii] = -99999;
364  CNN_vertex2[ii] = -99999;
365  }
366  nu_truth_N = 0;
367  for (int ii = 0; ii < kMax; ++ii)
368  {
369  nupdg_truth[ii] = -99999;
370  numode_truth[ii] = -99999;
371  nuccnc_truth[ii] = -99999;
372  nueng_truth[ii] = -99999;
373 
374  nuvtxx_truth[ii] = -99999;
375  nuvtxy_truth[ii] = -99999;
376  nuvtxz_truth[ii] = -99999;
377 
378  }
379 
380 }
381 
void XYZ(double *xyz) const
Legacy method to access vertex position, preserved to avoid breaking code. Please try to use Vertex::...
Definition: Vertex.cxx:36
void analyze(art::Event const &evt)
art::ServiceHandle< art::TFileService > tfs
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
intermediate_table::const_iterator const_iterator
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
std::map< art::Ptr< recob::PFParticle >, VertexVector > PFParticlesToVertices
void reconfigure(fhicl::ParameterSet const &p)
bool isRealData() const
const int kMax
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
std::vector< art::Ptr< recob::PFParticle > > PFParticleVector
T get(std::string const &key) const
Definition: ParameterSet.h:271
static void CollectVertices(const art::Event &evt, const std::string &label, VertexVector &vertexVector, PFParticlesToVertices &particlesToVertices)
Collect the reconstructed PFParticles and associated Vertices from the ART event record.
RegCNNVertexAna(fhicl::ParameterSet const &pset)
p
Definition: test.py:223
bool IsPrimary() const
Returns whether the particle is the root of the flow.
Definition: PFParticle.h:86
static void CollectPFParticles(const art::Event &evt, const std::string &label, PFParticleVector &particleVector)
Collect the reconstructed PFParticles from the ART event record.
RegCNNResult for RegCNN modified from Result.h.
Declaration of signal hit object.
std::vector< art::Ptr< recob::Vertex > > VertexVector
EventNumber_t event() const
Definition: EventID.h:116
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
helper function for LArPandoraInterface producer module
EventID id() const
Definition: Event.cc:34
QTextStream & endl(QTextStream &s)