VertexCheater_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // VertexCheater module
4 //
5 // brebel@fnal.gov
6 //
7 ////////////////////////////////////////////////////////////////////////
8 #include <string>
9 
10 // ROOT includes
11 
12 // LArSoft includes
19 #include "nug4/ParticleNavigation/ParticleList.h"
20 
21 // Framework includes
25 #include "fhiclcpp/ParameterSet.h"
28 #include "canvas/Persistency/Common/FindManyP.h"
30 
31 namespace vertex {
32  class VertexCheater : public art::EDProducer {
33  public:
34  explicit VertexCheater(fhicl::ParameterSet const& pset);
35 
36  void produce(art::Event& evt);
37 
38  private:
39 
40  std::string fCheatedTrackLabel; ///< label for module creating recob::Track objects
41  std::string fCheatedShowerLabel; ///< label for module creating recob::Shower objects
42  std::string fG4ModuleLabel; ///< label for module running G4 and making particles, etc
43 
44  };
45 }
46 
47 namespace vertex{
48 
49  //--------------------------------------------------------------------
51  : EDProducer{pset}
52  {
53  fCheatedTrackLabel = pset.get< std::string >("CheatedTrackLabel", "track" );
54  fCheatedShowerLabel = pset.get< std::string >("CheatedShowerLabel", "shower" );
55  fG4ModuleLabel = pset.get< std::string >("G4ModuleLabel", "largeant");
56 
57  produces< std::vector<recob::Vertex> >();
58  produces< art::Assns<recob::Vertex, recob::Shower> >();
59  produces< art::Assns<recob::Vertex, recob::Track> >();
60  produces< art::Assns<recob::Vertex, recob::Hit> >();
61 
62  }
63 
64  //--------------------------------------------------------------------
66  {
68 
69  // grab the sim::ParticleList
70  const sim::ParticleList& plist = pi_serv->ParticleList();
71 
72  // grab the showers that have been reconstructed
74  evt.getByLabel(fCheatedShowerLabel, showercol);
75 
76  // make a vector of them - we aren't writing anything out to a file
77  // so no need for a art::PtrVector here
78  std::vector< art::Ptr<recob::Shower> > showers;
79  try{
80  art::fill_ptr_vector(showers, showercol);
81  }
82  catch(cet::exception &e){
83  mf::LogWarning("VertexCheater") << "showers: " << e;
84  }
85 
86  // grab the tracks that have been reconstructed
88  evt.getByLabel(fCheatedTrackLabel, trackcol);
89 
90  // make a vector of them - we aren't writing anything out to a file
91  // so no need for a art::PtrVector here
92  std::vector< art::Ptr<recob::Track> > tracks;
93  try{
94  art::fill_ptr_vector(tracks, trackcol);
95  }
96  catch(cet::exception &e){
97  mf::LogWarning("VertexCheater") << "tracks: " << e;
98  }
99 
100  art::FindManyP<recob::Hit> fmhs(showers, evt, fCheatedShowerLabel);
101  art::FindManyP<recob::Hit> fmht(tracks, evt, fCheatedTrackLabel);
102 
103  // loop over the prongs and figure out which primaries they are associated with
104  std::vector< art::Ptr<recob::Shower> >::iterator shwitr = showers.begin();
105  std::vector< art::Ptr<recob::Track> >::iterator trkitr = tracks.begin();
106 
107  // protect against events where there are either no tracks or no showers
108  if(tracks.size() < 1) trkitr = tracks.end();
109  if(showers.size() < 1) shwitr = showers.end();
110 
111  // make a map of eve id's to collections of prongs
112  std::map<int, std::vector< std::pair<size_t, art::Ptr<recob::Shower> > > > eveShowerMap;
113  std::map<int, std::vector< std::pair<size_t, art::Ptr<recob::Track> > > > eveTrackMap;
114 
115  // make a map of eve id's to the number of prongs corresponding to that id
116  std::vector<int> eveIDs;
117 
118  // loop over all showers
119  for(size_t s = 0; s < showers.size(); ++s){
120 
121  std::pair<size_t, art::Ptr<recob::Shower> > idxShw(s, showers[s]);
122 
123  // in the ProngCheater module we set the prong ID to be
124  // the particle track ID creating the energy depositions
125  // of the prong
126  int prongID = showers[s]->ID();
127 
128  // now get the mother particle of this prong if it exists
129  // set the eveID to the mother particle track ID, or to the
130  // ID of this prong if it is primary and mother ID = 0
131  int eveID = plist[prongID]->Mother();
132  if( eveID < 1 ) eveID = prongID;
133 
134  if(std::find(eveIDs.begin(), eveIDs.end(), eveID) == eveIDs.end())
135  eveIDs.push_back(eveID);
136 
137  // now we want to associate all prongs having the same
138  // eve ID, so look to see if there are other prongs with that
139  // ID
140  eveShowerMap[eveID].push_back(idxShw);
141 
142  mf::LogInfo("VertexCheater") << "shower: " << prongID << " has mother " << eveID;
143 
144  }// end loop over showers
145 
146  // loop over all tracks
147  for(size_t t = 0; t < tracks.size(); ++t){
148 
149  std::pair<size_t, art::Ptr<recob::Track> > idxTrk(t, tracks[t]);
150 
151  // in the ProngCheater module we set the prong ID to be
152  // the particle track ID creating the energy depositions
153  // of the prong
154  int prongID = tracks[t]->ID();
155 
156  // now get the mother particle of this prong if it exists
157  // set the eveID to the mother particle track ID, or to the
158  // ID of this prong if it is primary and mother ID = 0
159  int eveID = plist[prongID]->Mother();
160  if( eveID < 1 ) eveID = prongID;
161 
162  if(std::find(eveIDs.begin(), eveIDs.end(), eveID) == eveIDs.end())
163  eveIDs.push_back(eveID);
164 
165  // now we want to associate all prongs having the same
166  // eve ID, so look to see if there are other prongs with that
167  // ID
168  eveTrackMap[eveID].push_back(idxTrk);
169 
170  mf::LogInfo("VertexCheater") << "track: " << prongID << " has mother " << eveID;
171 
172  }// end loop over tracks
173 
174  std::unique_ptr< std::vector<recob::Vertex> > vertexcol(new std::vector<recob::Vertex>);
175  std::unique_ptr< art::Assns<recob::Vertex, recob::Shower> > vsassn(new art::Assns<recob::Vertex, recob::Shower>);
176  std::unique_ptr< art::Assns<recob::Vertex, recob::Track> > vtassn(new art::Assns<recob::Vertex, recob::Track>);
177  std::unique_ptr< art::Assns<recob::Vertex, recob::Hit> > vhassn(new art::Assns<recob::Vertex, recob::Hit>);
178 
179  // loop over the eve ID values and make Vertexs
180  for(auto const& eveID : eveIDs){
181 
182  // Vertex objects require PtrVectors of showers and tracks as well
183  // as a vertex position for their constructor
186  std::vector<size_t> idxShw;
187  std::vector<size_t> idxTrk;
188 
189  // first get the showers
190  if(eveShowerMap.find(eveID) != eveShowerMap.end()){
191  auto const& eveShowers = eveShowerMap[eveID];
192  for(auto const& is : eveShowers){
193  ptrvshw.push_back(is.second);
194  idxShw.push_back(is.first);
195  } // end loop over showers for this particle
196  } // end find showers for this particle
197 
198  // now the tracks
199  if(eveTrackMap.find(eveID) != eveTrackMap.end()){
200  auto const& eveTracks = eveTrackMap[eveID];
201  for(auto const& it : eveTracks){
202  ptrvtrk.push_back(it.second);
203  idxTrk.push_back(it.first);
204  } // end loop over tracks for this particle
205  } // end find tracks for this particle
206 
207  double xyz[3] = { plist[eveID]->Vx(),
208  plist[eveID]->Vy(),
209  plist[eveID]->Vz() };
210 
211  // add a vector to the collection.
212  vertexcol->push_back(recob::Vertex(xyz, eveID));
213 
214  // associate the vertex with its showers and tracks
215 
216  if( ptrvtrk.size() > 0 ){
217  util::CreateAssn(*this, evt, *vertexcol, ptrvtrk, *vtassn);
218 
219  // get the hits associated with each track and associate those with the vertex
220  for(auto const& i : idxTrk){
221  std::vector< art::Ptr<recob::Hit> > hits = fmht.at(i);
222  util::CreateAssn(*this, evt, *vertexcol, hits, *vhassn);
223  }
224  }
225 
226  if( ptrvshw.size() > 0 ){
227  util::CreateAssn(*this, evt, *vertexcol, ptrvshw, *vsassn);
228  // get the hits associated with each shower and associate those with the vertex
229  for(auto const& i : idxShw){
230  std::vector< art::Ptr<recob::Hit> > hits = fmhs.at(i);
231  util::CreateAssn(*this, evt, *vertexcol, hits, *vhassn);
232  }
233  }
234 
235  mf::LogInfo("VertexCheater") << "adding vertex: \n"
236  << vertexcol->back()
237  << "\nto collection.";
238 
239  } // end loop over the eve ID values
240 
241  evt.put(std::move(vertexcol));
242  evt.put(std::move(vsassn));
243  evt.put(std::move(vtassn));
244  evt.put(std::move(vhassn));
245 
246  return;
247 
248  } // end produce
249 
250 } // end namespace
251 
252 
253 namespace vertex{
254 
256 
257 }
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
std::string fCheatedTrackLabel
label for module creating recob::Track objects
std::string fG4ModuleLabel
label for module running G4 and making particles, etc
Definition of vertex object for LArSoft.
Definition: Vertex.h:35
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:435
def move(depos, offset)
Definition: depos.py:107
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
bool CreateAssn(PRODUCER const &prod, art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t indx=UINT_MAX)
Creates a single one-to-one association.
size_type size() const
Definition: PtrVector.h:302
const sim::ParticleList & ParticleList() const
std::string fCheatedShowerLabel
label for module creating recob::Shower objects
Declaration of signal hit object.
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
Provides recob::Track data product.
VertexCheater(fhicl::ParameterSet const &pset)
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
static QCString * s
Definition: config.cpp:1042
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void produce(art::Event &evt)
vertex reconstruction