All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EvalVtx_module.cc
Go to the documentation of this file.
1 // Chris Backhouse - c.backhouse@ucl.ac.uk - Oct 2019
2 
3 #include "fhiclcpp/ParameterSet.h"
4 #include "art/Framework/Core/ModuleMacros.h"
5 #include "art/Framework/Core/EDAnalyzer.h"
6 #include "art/Framework/Principal/Event.h"
7 #include "art/Framework/Principal/Handle.h"
8 #include "art/Framework/Services/Registry/ServiceHandle.h"
9 #include "art_root_io/TFileService.h"
10 #include "canvas/Persistency/Common/FindManyP.h"
11 
14 #include "nusimdata/SimulationBase/MCTruth.h"
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:
29  std::string fTruthLabel;
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 
40 DEFINE_ART_MODULE(EvalVtx)
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 {
48  art::ServiceHandle<art::TFileService> tfs;
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 // ---------------------------------------------------------------------------
70 recob::Vertex GetFirstVertex(const std::string& label, const art::Event& evt)
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 // ---------------------------------------------------------------------------
80 recob::Vertex GetVtxByAssns(const std::string& label, const art::Event& evt)
81 {
82  art::Handle<std::vector<recob::Vertex>> vtxs;
83  evt.getByLabel(label, vtxs);
84 
85  if(vtxs->empty()) return recob::Vertex();
86 
87  art::Handle<std::vector<recob::PFParticle>> parts;
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 // ---------------------------------------------------------------------------
111 void EvalVtx::analyze(const art::Event& evt)
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
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
EvalVtx(const fhicl::ParameterSet &pset)
var pdg
Definition: selectors.fcl:14
void analyze(const art::Event &evt) override
recob::Vertex GetVtxByAssns(const std::string &label, const art::Event &evt)
std::vector< std::string > fVertexLabels
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:265
T abs(T value)
tracking::Point_t Point_t
Definition: Vertex.h:39
std::vector< double > gRecoX
recob::Vertex GetFirstVertex(const std::string &label, const art::Event &evt)
std::vector< double > gRecoY
std::string fTruthLabel
art::ServiceHandle< art::TFileService > tfs
TCEvent evt
Definition: DataStructs.cxx:8
process_name out1 physics analyzers evalvtx VertexLabels
Definition: quadvtxjob.fcl:48
BEGIN_PROLOG SN nu
const Point_t & position() const
Return vertex 3D position.
Definition: Vertex.h:60
BEGIN_PROLOG could also be cout