TruthSlim_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \brief TruthSlim -- remove generated objects that don't contribute
3 /// \author dwwhitti@indiana.edu
4 ////////////////////////////////////////////////////////////////////////
5 #include <cstdlib>
6 #include <string>
7 #include <sstream>
8 #include <sys/stat.h>
9 #include <vector>
10 #include <set>
11 
12 // ROOT include
13 #include <TDatabasePDG.h>
14 #include <TSystem.h>
15 #include "TStopwatch.h"
16 
17 // Framework includes
21 #include "fhiclcpp/ParameterSet.h"
25 #include "cetlib/search_path.h"
28 #include "Simulation/Particle.h"
32 #include "Utilities/AssociationUtil.h"
34 
35 namespace g4n {
36 
37  /// A module to slim the generator output
38  class TruthSlim : public art::EDProducer {
39  public:
40  explicit TruthSlim(fhicl::ParameterSet const &pset);
41  virtual ~TruthSlim();
42 
43 
44  void produce (art::Event& evt);
45  void beginRun(art::Run& run);
46 
47  private:
48 
49  TStopwatch fStopwatch; ///< keep track of how long it takes to run the job
50  std::string fTruthLabel; ///< label used for the Monte Carlo generator module
51  std::string fGeantLabel; ///< label used for the Geant simulation module
52 
53  };
54 
55  //____________________________________________________________________________
56  // Constructor
58  : fTruthLabel(pset.get< std::string >("TruthLabel"))
59  , fGeantLabel(pset.get< std::string >("GeantLabel"))
60  {
61 
62  // Start counting the time
63  fStopwatch.Start();
64 
65  // Produce new (slimmed) truth containers
66  produces< std::vector<simb::MCTruth> >();
67  produces< std::vector<simb::MCFlux> >();
68  produces< std::vector<simb::GTruth> >();
69  produces< art::Assns<simb::MCTruth, simb::MCFlux> >();
70  produces< art::Assns<simb::MCTruth, simb::GTruth> >();
71 
72  }// end of constructor
73 
74  //____________________________________________________________________________
75  // Destructor
77  {
78  fStopwatch.Stop();
79  mf::LogVerbatim("TruthSlim") << "real time to run truth slimming: " << fStopwatch.RealTime();
80  }// end of destructor
81 
82  //______________________________________________________________________________
84  {
85  return;
86  }// end of begin job
87 
88  //______________________________________________________________________________
90  {
91  // Get the Particle vector.
93  evt.getByLabel(fGeantLabel,particles);
94 
95  // Compile a list of MCTruth associated with the
96  // sim::Particles in this event
97  std::set<art::Ptr<simb::MCTruth> > truthToKeep;
98  // Retrieve the associated MCTruth(s)
99  /* each element is a *list* of MCTruths associated with each particle */
100  art::FindManyP<simb::MCTruth> truthAssn(particles,evt,fGeantLabel);
101  // Loop through MCTruth objects to accumulate list of keepers
102  for ( size_t p = 0; p < particles->size(); ++p ) {
103  for ( size_t t = 0; t < truthAssn.at(p).size(); ++t ) {
104  art::Ptr<simb::MCTruth> mcTru = truthAssn.at(p)[t];
105  truthToKeep.insert(mcTru);
106  }
107  }
108 
109  // Retrieve the MCTruth information for this event
110  std::vector< art::Handle< std::vector<simb::MCTruth> > > mclists;
111  evt.getManyByType(mclists);
112 
113  // Create the associated (slimmed) truth data containers
114  for(size_t mcl = 0; mcl < mclists.size(); ++mcl){
115  std::unique_ptr<std::vector<simb::MCTruth> > slimMCTruth (new std::vector<simb::MCTruth> );
116  std::unique_ptr<std::vector<simb::MCFlux> > slimMCFlux (new std::vector<simb::MCFlux> );
117  std::unique_ptr<std::vector<simb::GTruth> > slimGTruth (new std::vector<simb::GTruth> );
118  std::unique_ptr<art::Assns<simb::MCTruth, simb::MCFlux > > slimFluxAssns (new art::Assns<simb::MCTruth, simb::MCFlux> );
119  std::unique_ptr<art::Assns<simb::MCTruth, simb::GTruth > > slimGTruAssns (new art::Assns<simb::MCTruth, simb::GTruth> );
120  // Loop through MCTruth container(s)
121  art::Handle< std::vector<simb::MCTruth> > mclistHandle = mclists[mcl];
122  // Get MCFlux and GTruth associations
123  art::FindManyP<simb::MCFlux> fluxAssn(mclistHandle,evt,fTruthLabel);
124  art::FindManyP<simb::GTruth> gtruAssn(mclistHandle,evt,fTruthLabel);
125 
126  // Loop through MCTruth objects to veto those with no associated sim::Particles.
127  for(size_t i = 0; i < mclistHandle->size(); ++i){
128  art::Ptr<simb::MCTruth> mct(mclistHandle, i);
129  // Skip this MCTruth if it's not in the list of keepers
130  // (Only works if fSlimSimParticles was true)
131  if ( truthToKeep.find(mct) == truthToKeep.end() ) continue;
132  // Pack everything into slimmed containers and build new associations
133  slimMCTruth->push_back(*mct);
134  for ( size_t j = 0; j < fluxAssn.at(i).size(); ++j ) {
135  art::Ptr<simb::MCFlux> theFlux = fluxAssn.at(i)[j];
136  slimMCFlux->push_back(*theFlux);
137  util::CreateAssn(*this, evt, *slimMCTruth, theFlux, *slimFluxAssns, i);
138  }
139  for ( size_t j = 0; j < gtruAssn.at(i).size(); ++j ) {
140  art::Ptr<simb::GTruth> theGTru = gtruAssn.at(i)[j];
141  slimGTruth->push_back(*theGTru);
142  util::CreateAssn(*this, evt, *slimMCTruth, theGTru, *slimGTruAssns, i);
143  }
144  }
145  evt.put(std::move(slimMCTruth));
146  evt.put(std::move(slimMCFlux ));
147  evt.put(std::move(slimGTruth ));
148  evt.put(std::move(slimFluxAssns));
149  evt.put(std::move(slimGTruAssns));
150  }// end loop through MCTruth container(s)
151 
152  return;
153  }// end of produce
154 
155 } // end namespace
156 
TruthSlim – remove generated objects that don&#39;t contribute.
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
static bool CreateAssn(art::EDProducer const &prod, art::Event &evt, std::vector< T > &a, art::Ptr< U > b, art::Assns< T, U > &assn, size_t indx=UINT_MAX, std::string const &instance=std::string())
Create a 1 to 1 association between a new product and one already in the event.
virtual ~TruthSlim()
void beginRun(art::Run &run)
const char * p
Definition: xmltok.h:285
void produce(art::Event &evt)
DEFINE_ART_MODULE(TestTMapFile)
object containing MC flux information
Definition: Run.h:31
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
std::string fGeantLabel
label used for the Geant simulation module
void getManyByType(std::vector< Handle< PROD >> &results) const
Definition: DataViewImpl.h:446
std::string fTruthLabel
label used for the Monte Carlo generator module
const double j
Definition: BetheBloch.cxx:29
Definition: run.py:1
TStopwatch fStopwatch
keep track of how long it takes to run the job
::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
TruthSlim(fhicl::ParameterSet const &pset)
A module to slim the generator output.