EvalVtx_module.cc
Go to the documentation of this file.
1 // Chris Backhouse - c.backhouse@ucl.ac.uk - Oct 2019
2 
9 #include "art_root_io/TFileService.h"
10 #include "canvas/Persistency/Common/FindManyP.h"
11 
15 
16 #include "TTree.h"
17 
18 namespace quad
19 {
20 
21 class EvalVtx: public art::EDAnalyzer
22 {
23 public:
24  explicit EvalVtx(const fhicl::ParameterSet& pset);
25 
26  void analyze(const art::Event& evt) override;
27 
28 private:
30  std::vector<std::string> fVertexLabels;
31 
32  TTree* fTree;
33 
34  // For use in filling the TTree
35  int gEvt;
36  double gTrueX, gTrueY, gTrueZ;
37  std::vector<double> gRecoX, gRecoY, gRecoZ;
38 };
39 
41 
42 // ---------------------------------------------------------------------------
43 EvalVtx::EvalVtx(const fhicl::ParameterSet& pset) :
44  EDAnalyzer(pset),
45  fTruthLabel(pset.get<std::string>("TruthLabel")),
46  fVertexLabels(pset.get<std::vector<std::string>>("VertexLabels"))
47 {
49 
50  fTree = tfs->make<TTree>("vtxs", "vtxs");
51 
52  fTree->Branch("evt", &gEvt);
53  fTree->Branch("true_x", &gTrueX);
54  fTree->Branch("true_y", &gTrueY);
55  fTree->Branch("true_z", &gTrueZ);
56 
57  gRecoX.resize(fVertexLabels.size());
58  gRecoY.resize(fVertexLabels.size());
59  gRecoZ.resize(fVertexLabels.size());
60 
61  for(unsigned int i = 0; i < fVertexLabels.size(); ++i){
62  const std::string& l = fVertexLabels[i];
63  fTree->Branch((l+"_x").c_str(), &gRecoX[i]);
64  fTree->Branch((l+"_y").c_str(), &gRecoY[i]);
65  fTree->Branch((l+"_z").c_str(), &gRecoZ[i]);
66  }
67 }
68 
69 // ---------------------------------------------------------------------------
71 {
72  const auto& vtxs = *evt.getValidHandle<std::vector<recob::Vertex>>(label);
73 
74  if(vtxs.empty()) return recob::Vertex();
75 
76  return vtxs[0];
77 }
78 
79 // ---------------------------------------------------------------------------
81 {
83  evt.getByLabel(label, vtxs);
84 
85  if(vtxs->empty()) return recob::Vertex();
86 
88  evt.getByLabel(label, parts);
89 
90  art::FindManyP<recob::Vertex> fm(parts, evt, label);
91 
92  for(unsigned int i = 0; i < parts->size(); ++i){
93  const int pdg = abs((*parts)[i].PdgCode());
94  if(pdg == 12 || pdg == 14 || pdg == 16){
95  const std::vector<art::Ptr<recob::Vertex>>& vtxs = fm.at(i);
96  if(vtxs.size() == 1) return *vtxs[0];
97 
98  if(vtxs.empty()){
99  std::cout << "Warning: vertex list empty!" << std::endl;
100  }
101  else{
102  std::cout << "Warning: " << vtxs.size() << " vertices for daughter?" << std::endl;
103  }
104  }
105  }
106 
107  return recob::Vertex();
108 }
109 
110 // ---------------------------------------------------------------------------
112 {
113  const auto& truths = *evt.getValidHandle<std::vector<simb::MCTruth>>(fTruthLabel);
114  if(truths.empty()) return;
115 
116  const simb::MCParticle& nu = truths[0].GetNeutrino().Nu();
117 
118  gEvt = evt.event();
119 
120  gTrueX = nu.Vx();
121  gTrueY = nu.Vy();
122  gTrueZ = nu.Vz();
123 
124  for(unsigned int i = 0; i < fVertexLabels.size(); ++i){
125  // NB - vertices returned by these functions can be invalid (default
126  // constructed) in case no vertex exists. We stil have to write them, since
127  // another algorithm may have made a valid vertex for this
128  // event. Downstream plotting code should be aware and treat (0,0,0)
129  // vertices specially.
130  const recob::Vertex vtx = (fVertexLabels[i] == "pandora") ? GetVtxByAssns(fVertexLabels[i], evt) : GetFirstVertex(fVertexLabels[i], evt);
131  const recob::Vertex::Point_t reco_vtx = vtx.position();
132 
133  gRecoX[i] = reco_vtx.x();
134  gRecoY[i] = reco_vtx.y();
135  gRecoZ[i] = reco_vtx.z();
136  } // end for i
137 
138  fTree->Fill();
139 }
140 
141 } // namespace
EventNumber_t event() const
Definition: DataViewImpl.cc:85
EvalVtx(const fhicl::ParameterSet &pset)
std::string string
Definition: nybbler.cc:12
void analyze(const art::Event &evt) override
recob::Vertex GetVtxByAssns(const std::string &label, const art::Event &evt)
STL namespace.
std::vector< std::string > fVertexLabels
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
std::vector< double > gRecoZ
static QStrList * l
Definition: config.cpp:1044
Definition of vertex object for LArSoft.
Definition: Vertex.h:35
T abs(T value)
tracking::Point_t Point_t
Definition: Vertex.h:39
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
int PdgCode(Resonance_t res, int Q)
(resonance id, charge) -> PDG code
std::vector< double > gRecoX
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
recob::Vertex GetFirstVertex(const std::string &label, const art::Event &evt)
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
std::vector< double > gRecoY
double Vx(const int i=0) const
Definition: MCParticle.h:221
std::string fTruthLabel
double Vz(const int i=0) const
Definition: MCParticle.h:223
boost::graph_traits< ModuleGraph >::vertex_descriptor Vertex
Definition: ModuleGraph.h:25
static constexpr double fm
Definition: Units.h:75
TCEvent evt
Definition: DataStructs.cxx:7
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
const Point_t & position() const
Return vertex 3D position.
Definition: Vertex.h:60
double Vy(const int i=0) const
Definition: MCParticle.h:222
QTextStream & endl(QTextStream &s)