TotallyCheatTracker_module.cc
Go to the documentation of this file.
1 /**
2  * @file TotallyCheatTracker_module.cc
3  * @brief Module running `lar::example::TotallyCheatTrackingAlg` algorithm.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date December 26, 2017
6  * @ingroup TotallyCheatTracks
7  *
8  * Provides:
9  *
10  * * `lar::example::TotallyCheatTracker` module
11  *
12  */
13 
14 // LArSoft libraries
18 
19 // framework libraries
23 #include "art/Framework/Principal/Handle.h" // art::ValidHandle<>
29 #include "fhiclcpp/types/Atom.h"
30 #include "fhiclcpp/types/Table.h"
31 
32 // C/C++ standard libraries
33 #include <memory> // std::make_unique()
34 
35 
36 namespace lar {
37  namespace example {
38 
39  /**
40  * @brief Module: creates tracks from simulated particles.
41  * @ingroup TotallyCheatTracks
42  * @see @ref TotallyCheatTracks "TotallyCheatTracks example overview"
43  *
44  * This module creates one LArSoft reconstructed track
45  * (`lar::example::CheatTrack`) for each input simulated particle
46  * (`simb::MCParticle`) passing the selection criteria.
47  *
48  * Input
49  * ------
50  *
51  * A collection of `simb::MCParticle` is required.
52  *
53  *
54  * Output
55  * ------
56  *
57  * A collection of `lar::example::CheatTrack` is produced, a one-to-one
58  * association of each of them to its original `simb::MCParticle`.
59  * Associations are inserted in the same order as the trajectories.
60  *
61  *
62  * Configuration parameters
63  * -------------------------
64  *
65  * * *particles* (input tag, default: `largeant`): label of the data product
66  * with input simulated particles
67  * * *minLength* (parameter set, default: 1.0): minimum length of the
68  * particle trajectory, in centimeters
69  * * *minEnergy* (parameter set, default: 1.0): minimum energy of the
70  * particle, in GeV
71  *
72  */
74 
75  public:
76 
77  /// Module configuration data
78  struct Config {
79 
80  using Name = fhicl::Name;
82 
84  Name("particles"),
85  Comment("the data product of simulated particles to be processed"),
86  "largeant" // default
87  };
88 
90  Name("minLength"),
91  Comment("minimum length of particle trajectory [cm]"),
92  1.0 // default
93  };
94 
96  Name("minEnergy"),
97  Comment("minimum energy of particle [GeV]"),
98  1.0 // default
99  };
100 
102  Name("algoConfig"),
103  Comment("configuration of TotallyCheatTrackingAlg algorithm"),
105  };
106 
107  }; // Config
108 
109  /// Standard _art_ alias for module configuration table
111 
112  /// Constructor; see the class documentation for the configuration
113  explicit TotallyCheatTracker(Parameters const& config);
114 
115 
116  virtual void produce(art::Event& event) override;
117 
118 
119  /// Returns whether the `particle` satisfies the selection criteria.
120  bool acceptParticle(simb::MCParticle const& particle) const;
121 
122 
123  private:
124  art::InputTag particleTag; ///< Label of the input data product.
125 
126  double minLength; ///< Minimum particle length [cm]
127  double minEnergy; ///< Minimum energy [GeV]
128 
129  /// Reconstruction algorithm.
131 
132 
133  }; // class TotallyCheatTracker
134 
135  } // namespace example
136 } // namespace lar
137 
138 
139 
140 //------------------------------------------------------------------------------
141 //--- TotallyCheatTracker
142 //---
145  : EDProducer{config}
147  , minLength(config().minLength())
148  , minEnergy(config().minEnergy())
150 {
151 
152  consumes<std::vector<simb::MCParticle>>(particleTag);
153 
154  produces<std::vector<lar::example::CheatTrack>>();
155  produces<art::Assns<lar::example::CheatTrack, simb::MCParticle>>();
156 
157 } // lar::example::TotallyCheatTracker::TotallyCheatTracker()
158 
159 
160 //------------------------------------------------------------------------------
162 
163  //
164  // read the input
165  //
166  auto particleHandle
167  = event.getValidHandle<std::vector<simb::MCParticle>>(particleTag);
168  auto const& particles = *particleHandle;
169 
170  //
171  // prepare the output structures
172  //
173  auto tracks = std::make_unique<std::vector<lar::example::CheatTrack>>();
174  auto trackToPart =
175  std::make_unique<art::Assns<lar::example::CheatTrack, simb::MCParticle>>();
176 
177  art::PtrMaker<simb::MCParticle> makePartPtr(event, particleHandle.id());
178  art::PtrMaker<lar::example::CheatTrack> makeTrackPtr(event);
179 
180 
181  //
182  // set up the algorithm
183  //
184  trackMaker.setup();
185 
186  //
187  // run the algorithm
188  //
189  for (std::size_t iParticle = 0U; iParticle < particles.size(); ++iParticle) {
190  simb::MCParticle const& particle = particles[iParticle];
191 
192  //
193  // apply filters
194  //
195  if (!acceptParticle(particle)) continue;
196 
197  //
198  // run the algorithm
199  //
200  tracks->push_back(trackMaker.makeTrack(particle));
201 
202  //
203  // create the association
204  //
205  auto const iTrack = tracks->size() - 1;
206  art::Ptr<lar::example::CheatTrack> const trackPtr = makeTrackPtr(iTrack);
207  art::Ptr<simb::MCParticle> const partPtr = makePartPtr(iParticle);
208  trackToPart->addSingle(trackPtr, partPtr);
209 
210  } // for
211 
212  //
213  // store the data products into the event (and print a short summary)
214  //
215  mf::LogInfo("TotallyCheatTracker")
216  << "Reconstructed " << tracks->size() << " tracks out of "
217  << particleHandle->size() << " particles from '"
218  << particleTag.encode() << "'";
219 
220  event.put(std::move(tracks));
221  event.put(std::move(trackToPart));
222 
223 } // lar::example::TotallyCheatTracker::produce()
224 
225 
226 //------------------------------------------------------------------------------
228  (simb::MCParticle const& particle) const
229 {
230  // skip empty particle
231  if (particle.NumberTrajectoryPoints() == 0) return false;
232 
233  // energy at the first point
234  if (particle.E() < minEnergy) return false;
235 
236  // path length
237  if (particle.Trajectory().TotalLength() < minLength) return false;
238 
239  // good enough!
240  return true;
241 
242 } // lar::example::TotallyCheatTracker::acceptParticle()
243 
244 //------------------------------------------------------------------------------
246 
247 
248 //------------------------------------------------------------------------------
double E(const int i=0) const
Definition: MCParticle.h:233
unsigned int NumberTrajectoryPoints() const
Definition: MCParticle.h:218
Reconstructs tracks from simulated particles.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
const simb::MCTrajectory & Trajectory() const
Definition: MCParticle.h:253
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
virtual void produce(art::Event &event) override
double minLength
Minimum particle length [cm].
ChannelGroupService::Name Name
Algorithm to "reconstruct" trajectories from simulated particles.
fhicl::Table< lar::example::TotallyCheatTrackingAlg::Config > algoConfig
Particle class.
std::string encode() const
Definition: InputTag.cc:97
Pseudo-track data product for TotallyCheatTracks example.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
static Config * config
Definition: config.cpp:1054
def move(depos, offset)
Definition: depos.py:107
bool acceptParticle(simb::MCParticle const &particle) const
Returns whether the particle satisfies the selection criteria.
lar::example::CheatTrack makeTrack(simb::MCParticle const &mcParticle) const
Returns a reconstructed track from the specified particle.
void setup()
Set up the algorithm (currently no operation).
lar::example::TotallyCheatTrackingAlg trackMaker
Reconstruction algorithm.
Module: creates tracks from simulated particles.
Definition: tracks.py:1
#define Comment
LArSoft-specific namespace.
double TotalLength() const
TotallyCheatTracker(Parameters const &config)
Constructor; see the class documentation for the configuration.
art::InputTag particleTag
Label of the input data product.
Definition: fwd.h:31
EventID id() const
Definition: Event.cc:34
Event finding and building.