CTPEvaluator_module.cc
Go to the documentation of this file.
1 /**
2  * @file dunereco/TrackPID/modules/CTPEvaluator_module.cc
3  *
4  * @brief This module performs track PID using the convolutionsl
5  * track PID network
6  */
7 
10 
11 #include "TTree.h"
12 #include "TVector3.h"
13 
18 
21 
27 
28 #include <fstream>
29 #include <string>
30 
31 //------------------------------------------------------------------------------------------------------------------------------------------
32 
33 namespace ctp
34 {
35 
36 /**
37  * @brief CTPEvaluator class
38  */
40 {
41 public:
42  /**
43  * @brief Constructor
44  *
45  * @param pset
46  */
47  CTPEvaluator(fhicl::ParameterSet const &pset);
48 
49  /**
50  * @brief Destructor
51  */
52  virtual ~CTPEvaluator();
53 
54  void beginJob();
55  void endJob();
56  void produce(art::Event &evt);
57 
58 private:
59 
63  bool fWriteTree;
64 
65  std::vector<float> fMuonScoreVector;
66  std::vector<float> fPionScoreVector;
67  std::vector<float> fProtonScoreVector;
68  std::vector<int> fPDGVector;
69  std::vector<unsigned int> fCaloPoints;
70 
71 
72  TTree *fPIDTree;
73 };
74 
76 
77 } // namespace ctp
78 
79 //------------------------------------------------------------------------------------------------------------------------------------------
80 // implementation follows
81 
83 #include "fhiclcpp/ParameterSet.h"
86 #include "art_root_io/TFileService.h"
87 #include "art_root_io/TFileDirectory.h"
89 
95 
98 
100 
101 #include "TRandom3.h"
102 
103 #include <iostream>
104 #include <random>
105 
106 namespace ctp
107 {
108 
110 fHelperPars(pset.get<fhicl::ParameterSet>("ctpHelper")),
112 fParticleLabel(pset.get<std::string>("particleLabel")),
113 fWriteTree(pset.get<bool>("writeTree"))
114 {
115  produces<std::vector<ctp::CTPResult>>();
116  produces<art::Assns<recob::Track,ctp::CTPResult>>();
117 }
118 
119 //------------------------------------------------------------------------------------------------------------------------------------------
120 
122 {
123 }
124 
125 //------------------------------------------------------------------------------------------------------------------------------------------
126 
128 {
129  if (!fWriteTree) return;
130 
132 
133  fPIDTree = tfs->make<TTree>("pidTree","pidTree");
134  fPIDTree->Branch("muonScores",&fMuonScoreVector);
135  fPIDTree->Branch("pionScores",&fPionScoreVector);
136  fPIDTree->Branch("protonScores",&fProtonScoreVector);
137  fPIDTree->Branch("pdgCodes",&fPDGVector);
138  fPIDTree->Branch("caloPoints",&fCaloPoints);
139 }
140 
141 //------------------------------------------------------------------------------------------------------------------------------------------
142 
144 {
145 }
146 
147 //------------------------------------------------------------------------------------------------------------------------------------------
148 
150 {
151  // Define containers for the things we're going to produce
152  std::unique_ptr< std::vector<ctp::CTPResult> > resultCol(new std::vector<ctp::CTPResult>);
153  std::unique_ptr< art::Assns<recob::Track,ctp::CTPResult> > trackResultAssn(new art::Assns<recob::Track,ctp::CTPResult>);
154 
155  if (fWriteTree)
156  {
157  fMuonScoreVector.clear();
158  fPionScoreVector.clear();
159  fProtonScoreVector.clear();
160  fPDGVector.clear();
161  fCaloPoints.clear();
162  }
163 
164  // Get all of the PFParticles
165  const std::vector<art::Ptr<recob::PFParticle>> particles = dune_ana::DUNEAnaEventUtils::GetPFParticles(evt,fParticleLabel);
166 
167  // Get the tracks too
168  const std::string trkLabel = fHelperPars.get<std::string>("TrackLabel");
169  const std::vector<art::Ptr<recob::Track>> tracks = dune_ana::DUNEAnaEventUtils::GetTracks(evt,trkLabel);
170 
171  for (const art::Ptr<recob::PFParticle> &particle : particles)
172  {
173  int thisTrackID = -999;
174 
175  // Get the track if this particle is track-like
176  unsigned int nCaloPoints = 0;
178  {
180  const std::string caloLabel = fHelperPars.get<std::string>("CalorimetryLabel");
182 
183  thisTrackID = trk.key();
184  nCaloPoints = calo->dEdx().size();
185  }
186  else continue;
187 
188  // Returns a dummy value if not a track or not suitable
189  CTPResult thisPID = fConvTrackPID.RunConvolutionalTrackPID(particle,evt);
190  resultCol->push_back(thisPID);
191  auto const ptrMaker = art::PtrMaker<ctp::CTPResult>(evt);
192  art::Ptr<ctp::CTPResult> ptrResult = ptrMaker(resultCol->size()-1);
193  art::Ptr<recob::Track> thisTrack = tracks.at(thisTrackID);
194 
195 // std::cout << "Making association between track " << thisTrack.key() << " and PID result " << ptrResult.key() << std::endl;
196 
197  // Now to make the association
198  util::CreateAssn(evt,ptrResult,thisTrack,*trackResultAssn);
199 
200  if (fWriteTree)
201  {
202  if (!thisPID.IsValid()) continue;
203  int pdg = 0;
204  if(!evt.isRealData()){
205  pdg = fConvTrackPID.GetTruePDGCode(particle,evt);
206  }
207  std::cout << "Got a track PID for particle of type " << pdg << ": " << thisPID.GetMuonScore() << ", " << thisPID.GetPionScore() << ", " << thisPID.GetProtonScore() << std::endl;
208  fMuonScoreVector.push_back(thisPID.GetMuonScore());
209  fPionScoreVector.push_back(thisPID.GetPionScore());
210  fProtonScoreVector.push_back(thisPID.GetProtonScore());
211  fPDGVector.push_back(pdg);
212  fCaloPoints.push_back(nCaloPoints);
213  }
214  }
215  if (fWriteTree) fPIDTree->Fill();
216 
217  evt.put(std::move(resultCol));
218  evt.put(std::move(trackResultAssn));
219 }
220 
221 } //namespace ctp
222 
static art::Ptr< recob::Track > GetTrack(const art::Ptr< recob::PFParticle > &pParticle, const art::Event &evt, const std::string &pParticleLabel, const std::string &trackLabel)
Get the track associated to this particle. Should only be called if IsTrack method succeeds...
Class containing some utility functions for all things CVN.
Definition: CTPHelper.h:30
Class storing the result from the convolutional track PID.
std::vector< float > fPionScoreVector
Class containing some utility functions for all things CVN.
Definition: CTPResult.h:16
Utility containing helpful functions for end users to access information about Tracks.
std::string string
Definition: nybbler.cc:12
const int GetTruePDGCode(const art::Ptr< recob::PFParticle >, const art::Event &evt) const
Definition: CTPHelper.cxx:185
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
void produce(art::Event &evt)
STL namespace.
CTPEvaluator class.
Functions to help use the convolutional track PID.
float GetPionScore() const
Definition: CTPResult.h:30
Particle class.
std::vector< unsigned int > fCaloPoints
float GetProtonScore() const
Definition: CTPResult.h:31
std::vector< int > fPDGVector
bool isRealData() const
static std::vector< art::Ptr< recob::Track > > GetTracks(const art::Event &evt, const std::string &label)
Get the tracks from the event. This function shouldn&#39;t be used as the basis of an analysis...
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
float GetMuonScore() const
Definition: CTPResult.h:29
Utility containing helpful functions for end users to access information about Showers.
def move(depos, offset)
Definition: depos.py:107
key_type key() const noexcept
Definition: Ptr.h:216
T get(std::string const &key) const
Definition: ParameterSet.h:271
CTPEvaluator(fhicl::ParameterSet const &pset)
Constructor.
Utility containing helpful functions for end users to access information about PFParticles.
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
Utility containing helpful functions for end users to access products from events.
bool CreateAssn(PRODUCER const &prod, art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t indx=UINT_MAX)
Creates a single one-to-one association.
std::vector< float > fProtonScoreVector
Definition: tracks.py:1
Declaration of signal hit object.
fhicl::ParameterSet fHelperPars
virtual ~CTPEvaluator()
Destructor.
Provides recob::Track data product.
static art::Ptr< anab::Calorimetry > GetCalorimetry(const art::Ptr< recob::Track > &pTrack, const art::Event &evt, const std::string &trackLabel, const std::string &caloLabel)
Get the particle associated with the track.
static bool IsTrack(const art::Ptr< recob::PFParticle > &pParticle, const art::Event &evt, const std::string &pParticleLabel, const std::string &trackLabel)
Check if this particle has an associated track.
TCEvent evt
Definition: DataStructs.cxx:7
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
int bool
Definition: qglobal.h:345
static std::vector< art::Ptr< recob::PFParticle > > GetPFParticles(const art::Event &evt, const std::string &label)
Get the particles from the event.
calorimetry
QTextStream & endl(QTextStream &s)
const ctp::CTPResult RunConvolutionalTrackPID(const art::Ptr< recob::PFParticle > particle, const art::Event &evt) const
Definition: CTPHelper.cxx:57
std::vector< float > fMuonScoreVector
bool IsValid() const
Definition: CTPResult.cxx:39