MCTruthAndFriendsItr.cxx
Go to the documentation of this file.
1 #include "MCTruthAndFriendsItr.h"
2 
3 #include <iostream>
4 #include <iomanip>
5 
6 #ifndef ART_V1
7  #include "canvas/Persistency/Common/FindOneP.h"
9 #else
10  #include "art/Framework/Core/FindOneP.h"
11  #include "art/Utilities/InputTag.h"
12 #endif
13 
14 
16  std::vector<std::string> const & labels)
17  : evt(evtIn)
18  , fInputModuleLabels(labels)
19  , indx_itr(indices.begin())
20  , nmctruth(0)
21  , imctruth(-1)
22  , thisMCTruth(0)
23  , thisGTruth(0)
24  , thisMCFlux(0)
25  , thisDk2Nu(0)
26  , thisNuChoice(0)
27  , thisLabel("")
28 {
29 
30  // look for any existing MCTruth info in this event
31  mclists.clear();
32 
33  if ( fInputModuleLabels.size()==0) {
35  // std::cout << "evt.getManyByType" << std::endl;
36  } else {
37  mclists.resize(fInputModuleLabels.size());
38  for (size_t i=0; i<fInputModuleLabels.size(); ++i) {
40  //std::cout << "evt.getByLabel " << fInputModuleLabels[i] << std::endl;
41  }
42  }
43 
44  //std::cerr << "evg::GENIEDumper::analyze got stuff ---------------- "
45  // << evt.id() << std::endl;
46 
47  for (size_t mcl = 0; mcl < mclists.size(); ++mcl) {
49  if ( ! mclistHandle.isValid() ) {
50  // std::cerr << "not valid mcl=" << mcl << "---------------- " << std::endl;
51  continue;
52  }
53  // processName, moduleLabel, instance (productInstanceName?)
54  /*
55  std::cout << " mclistHandle processName '"
56  << mclistHandle.provenance()->processName() // top of fcl file
57  << "' moduleLabel '"
58  << mclistHandle.provenance()->moduleLabel()
59  << "' productInstanceName '"
60  << mclistHandle.provenance()->productInstanceName()
61  << "'"
62  << std::endl;
63  */
64 
65  std::string handleLabel = mclistHandle.provenance()->moduleLabel();
66  outlabels.push_back(handleLabel);
67  /*
68  std::cerr << "mcl=" << mcl << " '" << handleLabel << "' ---------------- " << std::endl;
69  */
70 
71  // loop over mctruths in a list
72  for(size_t nmc = 0; nmc < mclistHandle->size(); ++nmc) {
73  art::Ptr<simb::MCTruth> mct(mclistHandle, nmc);
74 
75  std::pair<int,int> ipair(mcl,nmc);
76  indices.insert(ipair);
77 
78  /**
79  std::cout << "+++ mcl " << mcl << "[" << mclists.size() << "] "
80  << "nmc " << nmc << "[" << mclistHandle->size() << "] "
81  << std::endl;
82  std::cout << *(mct.get()) << std::endl;
83  ***/
84  }
85 
86  }
87 
88  indx_itr = indices.begin();
89  nmctruth = (int)(indices.size());
90  //std::cout << ".... found nmctruth " << nmctruth
91  // << " nlabels " << outlabels.size()
92  // << std::endl;
93 
94 }
95 
97 {
98  ++imctruth; // started at -1, so first call to Next() prepares us for indx=0
99  thisMCTruth = 0;
100  thisGTruth = 0;
101  thisMCFlux = 0;
102  thisDk2Nu = 0;
103  thisNuChoice = 0;
104  //std::cerr << "Next() called ... moved to imctruth " << imctruth << std::endl;
105  if ( imctruth >= nmctruth ) return false;
106 
107  size_t indx_handle = (*indx_itr).first;
108  size_t indx_within = (*indx_itr).second;
109 
110  thisLabel = outlabels[indx_handle];
111 
112  art::Handle< std::vector<simb::MCTruth> > hvMCTruth = mclists[indx_handle];
113 
114  /**
115  std::cout << "imctruth " << std::setw(3) << imctruth
116  << " [" << indx_handle << "," << indx_within << "]"
117  << " hvMCTruth.isValid() " << hvMCTruth.isValid()
118  << " '" << outlabels[indx_handle] << "' "
119  << std::endl;
120  **/
121 
122  thisMCTruth = &(hvMCTruth->at(indx_within));
123 
124  // inefficient ... should only need to do this bit for every new
125  // Handle ...
126 
127  //art::FindOneP<recob::Hit> findSPToHits(fSpacePoints,evt,fSpacePointLabel);
128  //const art::Ptr<recob::Hit> hit = findSPToHits.at(iSP);
129 
130  try {
131  art::FindOneP<simb::GTruth> qgtruth(hvMCTruth,evt,outlabels[indx_handle]);
132  const art::Ptr<simb::GTruth> gtruthptr = qgtruth.at(indx_within);
133  thisGTruth = gtruthptr.get();
134  }
135  catch (...) {
136  // std::cerr << "no GTruth for this handle" << std::endl;
137  }
138 
139  try {
140  art::FindOneP<simb::MCFlux> qmcflux(hvMCTruth,evt,outlabels[indx_handle]);
141  const art::Ptr<simb::MCFlux> mcfluxptr = qmcflux.at(indx_within);
142  thisMCFlux = mcfluxptr.get();
143  }
144  catch (...) {
145  // std::cerr << "no MCFlux for this handle" << std::endl;
146  }
147 
148  try {
149  art::FindOneP<bsim::Dk2Nu> qdk2nu(hvMCTruth,evt,outlabels[indx_handle]);
150  const art::Ptr<bsim::Dk2Nu> dk2nuptr = qdk2nu.at(indx_within);
151  thisDk2Nu = dk2nuptr.get();
152  }
153  catch (...) {
154  // std::cerr << "no bsim::Dk2NU for this handle" << std::endl;
155  }
156 
157  try {
158  art::FindOneP<bsim::NuChoice> qnuchoice(hvMCTruth,evt,outlabels[indx_handle]);
159  const art::Ptr<bsim::NuChoice> nuchoiceptr = qnuchoice.at(indx_within);
160  thisNuChoice = nuchoiceptr.get();
161  }
162  catch (...) {
163  // std::cerr << "no bsim::NuChoice for this handle" << std::endl;
164  }
165 
166  //std::cerr << "Next() called ... seeing " << thisMCTruth
167  // << " " << thisGTruth << " " << thisMCFlux << std::endl;
168 
169  // so user code looks like
170  // evgb::MCTruthAndFriendsItr mcitr(evt,labels);
171  // while ( mcitr.Next() ) {
172  // const simb::MCTruth* amctruth = mcitr.GetMCTruth();
173  // const simb::GTruth* agtruth = mcitr.GetGTruth();
174  //...
175 
176  /*
177  // loop over lists
178  try {
179  //art::FindOneP<simb::GTruth> QueryG(mclistHandle,evt,handleLabel);
180  }
181  catch (art::Exception) {
182  //std::cerr << "no GTruth for " << handleLabel << std::endl;
183  }
184  */
185 
186  // move the iterator on
187  ++indx_itr;
188  return true;
189 }
std::vector< art::Handle< std::vector< simb::MCTruth > > > mclists
MCTruthAndFriendsItr(art::Event const &evtIn, std::vector< std::string > const &labels)
std::string string
Definition: nybbler.cc:12
std::vector< std::string > outlabels
bool isValid() const
Definition: Handle.h:183
const simb::GTruth * thisGTruth
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:446
Provenance const * provenance() const
Definition: Handle.h:197
constexpr std::array< std::size_t, geo::vect::dimension< Vector >)> indices()
Returns a sequence of indices valid for a vector of the specified type.
void getManyByType(std::vector< Handle< PROD >> &results) const
Definition: DataViewImpl.h:558
std::vector< std::string > const & fInputModuleLabels
const bsim::NuChoice * thisNuChoice
std::set< std::pair< int, int > > indices
const simb::MCFlux * thisMCFlux
const simb::MCTruth * thisMCTruth
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
TCEvent evt
Definition: DataStructs.cxx:7
std::set< std::pair< int, int > >::const_iterator indx_itr
T const * get() const
Definition: Ptr.h:149