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