CTPTrackDump_module.cc
Go to the documentation of this file.
1 /**
2  * @file dunereco/TrackPID/modules/CTPTrackDump_module.cc
3  *
4  * @brief This module uses the analysis utilities to demonstrate
5  * some of their usage. This can be used as a basis for
6  * writing analysis code using these tools
7  */
8 
11 
12 #include "TTree.h"
13 #include "TVector3.h"
14 
19 
21 
27 
28 #include <fstream>
29 #include <string>
30 
31 //------------------------------------------------------------------------------------------------------------------------------------------
32 
33 namespace ctp
34 {
35 
36 /**
37  * @brief CTPTrackDump class
38  */
40 {
41 public:
42  /**
43  * @brief Constructor
44  *
45  * @param pset
46  */
47  CTPTrackDump(fhicl::ParameterSet const &pset);
48 
49  /**
50  * @brief Destructor
51  */
52  virtual ~CTPTrackDump();
53 
54  void beginJob();
55  void endJob();
56  void analyze(const art::Event &evt);
57 
58 private:
59 
60  void WriteTextFile(const art::Event &evt,const std::vector<std::vector<float>> &inputs, const std::pair<const simb::MCParticle*,float> &trueParticle,
61  const unsigned int &trackNumber, const unsigned int &nHits) const;
62 
66 };
67 
69 
70 } // namespace ctp
71 
72 //------------------------------------------------------------------------------------------------------------------------------------------
73 // implementation follows
74 
76 #include "fhiclcpp/ParameterSet.h"
79 #include "art_root_io/TFileService.h"
80 #include "art_root_io/TFileDirectory.h"
82 
88 
91 
92 #include "TRandom3.h"
93 
94 #include <iostream>
95 #include <random>
96 
97 namespace ctp
98 {
99 
101 fHelperPars(pset.get<fhicl::ParameterSet>("ctpHelper")),
103 fParticleLabel(pset.get<std::string>("particleLabel"))
104 {
105 
106 }
107 
108 //------------------------------------------------------------------------------------------------------------------------------------------
109 
111 {
112 }
113 
114 //------------------------------------------------------------------------------------------------------------------------------------------
115 
117 {
118 }
119 
120 //------------------------------------------------------------------------------------------------------------------------------------------
121 
123 {
124 }
125 
126 //------------------------------------------------------------------------------------------------------------------------------------------
127 
129 {
130 
131  // Get all of the PFParticles
132  const std::vector<art::Ptr<recob::PFParticle>> particles = dune_ana::DUNEAnaEventUtils::GetPFParticles(evt,fParticleLabel);
133 
134  unsigned int nTracks = 0;
135  for (const art::Ptr<recob::PFParticle> &particle : particles)
136  {
137  // Get the track if this particle is track-like
138  unsigned int nCaloPoints = 0;
139  const std::string trkLabel = fHelperPars.get<std::string>("TrackLabel");
141  {
143  const std::string caloLabel = fHelperPars.get<std::string>("CalorimetryLabel");
145 
146  nCaloPoints = calo->dEdx().size();
147  }
148  else continue;
149 
150  // Returns a dummy value if not a track or not suitable
151  std::vector<std::vector<float>> netInputs = fConvTrackPID.GetNetworkInputs(particle,evt);
152 
153  if(netInputs.empty()) continue;
154 
155  const std::pair<const simb::MCParticle*,float> trueParticle = fConvTrackPID.GetTrueParticle(particle,evt);
156 
157  this->WriteTextFile(evt,netInputs,trueParticle,nTracks,nCaloPoints);
158  ++nTracks;
159  }
160 
161 }
162 
163 void CTPTrackDump::WriteTextFile(const art::Event &evt, const std::vector<std::vector<float>> &inputs, const std::pair<const simb::MCParticle*,float> &trueParticle,
164  const unsigned int &trackNumber, const unsigned int &nHits) const{
165 
166  // Open our output file stream
167  std::stringstream filename;
168  filename << "tracks_" << evt.id().run() << "_" << evt.id().subRun() << "_" << evt.id().event() << "_" << trackNumber << "_" << std::floor(inputs.at(1).at(3)*1000) << ".dat";
169  std::ofstream output_file(filename.str());
170 
171  for(const float dedx : inputs.at(0))
172 // for(const float val : chargeVector)
173  {
174  output_file << dedx << "\n";
175  }
176  output_file << "End of dE/dx\n";
177 
178  for(const float var : inputs.at(1)){
179  output_file << var << "\n";
180  }
181 
182  output_file << trueParticle.first->PdgCode() << "\n";
183  output_file << nHits << "\n";
184  output_file << trueParticle.first->P() << "\n";
185  output_file << trueParticle.second << "\n";
186  output_file.close();
187 
188 }
189 
190 } //namespace ctp
191 
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
CTPTrackDump(fhicl::ParameterSet const &pset)
Constructor.
Utility containing helpful functions for end users to access information about Tracks.
std::string string
Definition: nybbler.cc:12
const std::vector< std::vector< float > > GetNetworkInputs(const art::Ptr< recob::PFParticle >, const art::Event &evt) const
Definition: CTPHelper.cxx:81
struct vector vector
STL namespace.
Functions to help use the convolutional track PID.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
Particle class.
string filename
Definition: train.py:213
RunNumber_t run() const
Definition: EventID.h:98
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
Utility containing helpful functions for end users to access information about Showers.
T get(std::string const &key) const
Definition: ParameterSet.h:271
virtual ~CTPTrackDump()
Destructor.
void analyze(const art::Event &evt)
Utility containing helpful functions for end users to access information about PFParticles.
Utility containing helpful functions for end users to access products from events.
const std::pair< const simb::MCParticle *, float > GetTrueParticle(const art::Ptr< recob::PFParticle >, const art::Event &evt) const
Definition: CTPHelper.cxx:152
void WriteTextFile(const art::Event &evt, const std::vector< std::vector< float >> &inputs, const std::pair< const simb::MCParticle *, float > &trueParticle, const unsigned int &trackNumber, const unsigned int &nHits) const
Declaration of signal hit object.
int var
Definition: 018_def.c:9
Provides recob::Track data product.
CTPTrackDump class.
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.
EventNumber_t event() const
Definition: EventID.h:116
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.
fhicl::ParameterSet fHelperPars
TCEvent evt
Definition: DataStructs.cxx:7
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
static std::vector< art::Ptr< recob::PFParticle > > GetPFParticles(const art::Event &evt, const std::string &label)
Get the particles from the event.
SubRunNumber_t subRun() const
Definition: EventID.h:110
EventID id() const
Definition: Event.cc:34
calorimetry