TruthExpand_module.cc
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////
2 /// \brief Module to take Cosmics MCTruth object which holds list of particles
3 /// and make it produce one MCTruth per Mother particle
4 /// \author Gavin Davies - gsdavies@iastate.edu
5 /// \date Feb. 2013
6 ////////////////////////////////////////////////////////////////////////////
7 #ifndef UTIL_TRUTHEXPAND_H
8 #define UTIL_TRUTHEXPAND_H
9 
10 #include <iostream>
11 #include <string>
12 #include <vector>
13 
14 // Framework includes
18 #include "fhiclcpp/ParameterSet.h"
20 
21 // Nova includes
22 #include "RecoBase/CellHit.h"
23 #include "MCCheater/BackTracker.h"
25 #include "Simulation/Particle.h"
26 
27 namespace util {
28  class TruthExpand : public art::EDProducer {
29  public:
30  explicit TruthExpand(fhicl::ParameterSet const& pset);
31  virtual ~TruthExpand();
32  void produce(art::Event& evt);
33  void reconfigure(const fhicl::ParameterSet& p);
34 
35  private:
36  std::string fHitInput; // Input for rb::CellHit
37  };
38 }
39 
40 namespace util{
41 
42  //......................................................................
44  {
45  reconfigure(pset);
46  produces< std::vector<simb::MCTruth> >();
47  }
48 
49  //......................................................................
51  {
52  }
53 
54  //......................................................................
56  {
57  fHitInput = pset.get< std::string >("HitInput" ) ;
58  }
59 
60  //......................................................................
61  /// Provides read-write access to the event data.
62 
64  {
66  // Not to be run on Data of course! Do nothing then
67  if( !bt->HaveTruthInfo() ) return;
68 
69  // Declare a container for MCTruth objects to be stored in the art::event
70  std::unique_ptr< std::vector<simb::MCTruth> > mcts(new std::vector<simb::MCTruth>);
71 
72  if(!bt->HaveTruthInfo()){
73  mf::LogWarning("TruthExpand") << "attempting to run MC truth check on "
74  << "things without truth, bail";
75  return;
76  }
77 
78  // get all hits in the event
80  evt.getByLabel(fHitInput, hithdl);
81 
83  for(size_t h = 0; h < hithdl->size(); ++h){
84  art::Ptr<rb::CellHit> hit(hithdl, h);
85  allhits.push_back(hit);
86  }
87 
88  // Use BackTracker to get a vector of all particles matched to the hits
89  // We then will only make one MCTruth for each Particle
90  std::vector<const sim::Particle*> parts = bt->HitsToParticle(allhits);
91 
92  // check our vector of Particles isn't empty
93  // If it is put out an empty truth to avoid exceptions
94  if(parts.empty()) {
95  simb::MCTruth truth;
96  mcts->push_back(truth);
97  evt.put(std::move(mcts));
98  return;
99  }
100 
101  /// iterate over the vector of Particles so that we can get at each one
102  for(auto const& particle : parts){
103 
104  // make one truth object for each particle
105  simb::MCTruth truth;
106 
107  // can't pass a const to MCTruth::Add so have to make a copy...
108  // Only proceed if a Mother particle
109  if((*particle).Mother() == 0){
110  sim::Particle p = (*particle);
111 
112  LOG_DEBUG("TruthExpand") << "PDG: " << (*particle).PdgCode() << std::endl;
113 
114  // add one particle to one MCTruth
115  truth.Add(p);
117  mcts->push_back(truth);
118  }
119  }
120 
121  // put all MCTruths into the event
122  evt.put(std::move(mcts));
123 
124  } // end produce
125 
126 } // end namespace util
127 
128 namespace util{
129 
131 
132 }
133 
134 #endif
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
back track the reconstruction to the simulation
Filter events based on their run/event numbers.
void produce(art::Event &evt)
Provides read-write access to the event data.
void SetOrigin(simb::Origin_t origin)
Definition: MCTruth.h:80
const char * p
Definition: xmltok.h:285
DEFINE_ART_MODULE(TestTMapFile)
void Add(simb::MCParticle &part)
Definition: MCTruth.h:79
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
T get(std::string const &key) const
Definition: ParameterSet.h:231
int evt
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
Definition: structs.h:12
void reconfigure(const fhicl::ParameterSet &p)
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
bool HaveTruthInfo() const
Is this a file with truth info in? (Is BackTracker going to be any use to you?)
Definition: BackTracker.h:133
Event generator information.
Definition: MCTruth.h:32
TruthExpand(fhicl::ParameterSet const &pset)
Cosmic rays.
Definition: MCTruth.h:24
std::vector< const sim::Particle * > HitsToParticle(const std::vector< const rb::CellHit * > &hits) const
Returns vector of sim::Particle objects contributing to the given collection of hits.