LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
DisambigCheater_module.cc
Go to the documentation of this file.
1 // Class: DisambigCheater
3 // Module Type: producer
4 // File: DisambigCheater_module.cc
5 //
6 // tylerdalion@gmail.com
7 //
9 #ifndef DisambigCheater_h
10 #define DisambigCheater_h
11 
17 
28 
29 
30 namespace hit{
31 
33  public:
34  explicit DisambigCheater(fhicl::ParameterSet const & p);
35  virtual ~DisambigCheater();
36 
37  void produce(art::Event & e);
38  void beginJob();
39  void endJob();
40  void reconfigure(fhicl::ParameterSet const & p);
41 
42  private:
43 
46 
47  std::string fChanHitLabel;
48  std::string fWidHitLabel;
49 
50  std::map< std::pair<double,double>, std::vector<geo::WireID> > fHitToWids;
51  void InitHitToWids( const std::vector< art::Ptr<recob::Hit> >& ChHits );
52 
53  void MakeDisambigHit(
55  geo::WireID const& wid,
56  art::Ptr<recob::Wire> const& wire,
57  art::Ptr<raw::RawDigit> const& rawdigits,
59  );
60 
61  unsigned int fFalseChanHits = 0; // hits with no IDEs - could be noise or other technical problems
62  unsigned int fBadIDENearestWire = 0; // Just to count the inconsistent IDE wire returns
63 
64  std::vector<unsigned int> fMaxWireShift; // shift to account for space charge and diffusion
65 
66  };
67 
68  //-------------------------------------------------------------------
70  {
71  this->reconfigure(p);
72 
73  // let HitCollectionCreator declare that we are going to produce
74  // hits and associations with wires and raw digits
75  // (with no particular product label)
77 
78 
79  // Space charge can shift true IDE postiion to far-off channels.
80  // Calculate maximum number of wires to shift hit in order to be on correct channel.
81  // Shift no more than half of the number of channels, as beyond there will
82  // have been a closer wire segment.
83  if( geom->Ncryostats()!=1 || geom->NTPC()<1 ){
84  fMaxWireShift.resize(3);
85  fMaxWireShift[0]=1;
86  fMaxWireShift[1]=1;
87  fMaxWireShift[2]=1;
88  } else {
89  // assume TPC 0 is typical of all in terms of number of channels
90  unsigned int np = geom->Cryostat(0).TPC(0).Nplanes();
91  fMaxWireShift.resize(np);
92  for(unsigned int p = 0; p<np; ++p){
93  double xyz[3] = {0.};
94  double xyz_next[3] = {0.};
95  unsigned int nw = geom->Cryostat(0).TPC(0).Plane(p).Nwires();
96  for(unsigned int w = 0; w<nw; ++w){
97 
98  // for vertical planes
99  if(geom->Cryostat(0).TPC(0).Plane(p).View()==geo::kZ) {
100  fMaxWireShift[2] = geom->Cryostat(0).TPC(0).Plane(p).Nwires();
101  break;
102  }
103 
104  geom->Cryostat(0).TPC(0).Plane(p).Wire(w).GetCenter(xyz);
105  geom->Cryostat(0).TPC(0).Plane(p).Wire(w+1).GetCenter(xyz_next);
106 
107  if(xyz[2]==xyz_next[2]){
108  fMaxWireShift[p] = w;
109  break;
110  }
111  }// end wire loop
112  }// end plane loop
113 
114  for(unsigned int i=0; i<np; i++)
115  fMaxWireShift[i] = std::floor(fMaxWireShift[i]/2);
116  }
117 
118  }
119 
120  //-------------------------------------------------------------------
122  {
123  }
124 
125  //-------------------------------------------------------------------
127  {
128 
129  // get hits on channels
131  evt.getByLabel(fChanHitLabel, ChanHits);
132  std::vector< art::Ptr<recob::Hit> > ChHits;
133  art::fill_ptr_vector(ChHits, ChanHits);
134 
135  // also get the associated wires and raw digits;
136  // we assume they have been created by the same module as the hits
137  art::FindOneP<raw::RawDigit> ChannelHitRawDigits
138  (ChanHits, evt, fChanHitLabel);
139  const bool doRawDigitAssns = ChannelHitRawDigits.isValid();
140 
141  art::FindOneP<recob::Wire> ChannelHitWires(ChanHits, evt, fChanHitLabel);
142  const bool doWireAssns = ChannelHitWires.isValid();
143 
144  // this object contains the hit collection
145  // and its associations to wires and raw digits
146  // (if the original objects have them):
147  recob::HitCollectionCreator hits(*this, evt, doWireAssns, doRawDigitAssns);
148 
149 
150  // find the wireIDs each hit is on
151  this->InitHitToWids( ChHits );
152 
153 
154  // make all of the hits
155  for(size_t h=0; h<ChHits.size(); h++){
156 
157  // get the objects associated with this hit
159  if (doWireAssns) wire = ChannelHitWires.at(h);
160  art::Ptr<raw::RawDigit> rawdigits;
161  if (doRawDigitAssns) rawdigits = ChannelHitRawDigits.at(h);
162 
163  // the trivial Z hits
164  if(ChHits[h]->View()==geo::kZ){
165  this->MakeDisambigHit(ChHits[h], ChHits[h]->WireID(), wire, rawdigits, hits);
166  continue;
167  }
168 
169 
170  // make U/V hits if any wire IDs are associated
171  // count hits without a wireID
173  std::pair<double,double> ChanTime(ChHits[h]->Channel()*1., ChHits[h]->PeakTime()*1.);
174  if( fHitToWids[ChanTime].size() == 1 )
175  this->MakeDisambigHit(ChHits[h], fHitToWids[ChanTime][0], wire, rawdigits, hits);
176  else if( fHitToWids[ChanTime].size() == 0 )
177  fFalseChanHits++;
178  else if( fHitToWids[ChanTime].size() > 1 )
179  this->MakeDisambigHit(ChHits[h], fHitToWids[ChanTime][0], wire, rawdigits, hits); // same thing for now
180 
181  } // for
182 
183 
184  // put the hit collection and associations into the event
185  hits.put_into(evt);
186 
187  fHitToWids.clear();
188  return;
189 
190  }
191 
192 
193  //-------------------------------------------------
195  art::Ptr<recob::Hit> const& original_hit, geo::WireID const& wid,
196  art::Ptr<recob::Wire> const& wire, art::Ptr<raw::RawDigit> const& rawdigits,
198  )
199  {
200 
201  if( !wid.isValid ){
202  mf::LogWarning("InvalidWireID") << "wid is invalid, hit not being made\n";
203  return;
204  }
205 
206  // create a hit copy of the original one, but with a different wire ID
207  recob::HitCreator hit(*original_hit, wid);
208  hcol.emplace_back(hit.move(), wire, rawdigits);
209 
210  }
211 
212 
213  //-------------------------------------------------------------------
215  {
216 
217  unsigned int Ucount(0), Vcount(0);
218  for( size_t h = 0; h < ChHits.size(); h++ ){
219  recob::Hit const& chit = *(ChHits[h]);
220  if( ChHits[h]->View() == geo::kZ ) continue;
221  if( ChHits[h]->View() == geo::kU ) Ucount++;
222  else if( ChHits[h]->View() == geo::kV ) Vcount++;
223  std::vector<geo::WireID> cwids = geom->ChannelToWire(chit.Channel());
224  std::pair<double,double> ChanTime( (double) chit.Channel(), (double) chit.PeakTime()); // hit key value
225 
226  // get hit IDEs
227  std::vector<const sim::IDE* > ides;
228  try{
229  ides=bt_serv->HitToSimIDEs_Ps( chit);
230  }
231  catch(...){};
232 
233  // catch the hits that have no IDEs
234  bool hasIDEs = !ides.empty();
235  if (hasIDEs) {
236  try { bt_serv->SimIDEsToXYZ(ides); } //What is this supposed to be doing? It get a vector, but never assigns it anywhere. There has to be a better way to do this check
237  catch(...) { hasIDEs = false; }
238  } // if
239  if (!hasIDEs) {
240  mf::LogVerbatim("DisambigCheat") << "Hit on channel " << chit.Channel()
241  << " between " << chit.PeakTimeMinusRMS()
242  << " and " << chit.PeakTimePlusRMS() << " matches no IDEs.";
243  fHitToWids[ChanTime] = std::vector< geo::WireID >();
244  continue;
245  } // if no IDEs
246 
247  // see what wire ID(s) the hit-IDEs are on
248  // if none: hit maps to empty vector
249  // if one: we have a unique hit, vector of size 1
250  // if more than one: make a vector of all wids
251  std::vector<geo::WireID> widsWithIdes;
252  for( size_t i=0; i<ides.size(); i++ ){
253  const double xyzIde[] = { ides[i]->x, ides[i]->y, ides[i]->z };
254 
255  // Occasionally, ide position is not in TPC
257  geo::TPCID tpcID = geom->FindTPCAtPosition(xyzIde);
258  if (!tpcID.isValid) {
259  mf::LogWarning("DisambigCheat") << "IDE at x = " << xyzIde[0]
260  << ", y = " << xyzIde[1]
261  << ", z = " << xyzIde[2]
262  << " does not correspond to a TPC.";
263  continue;
264  }
265  unsigned int tpc = tpcID.TPC, cryo = tpcID.Cryostat;
266 
267  // NearestWire seems to be missing some correction that is applied in LArG4, and is
268  // sometimes off by one or two wires. Use it and then correct it given channel
270  geo::WireID IdeWid;
271  try {
272  IdeWid = geom->NearestWireID(xyzIde, cwids[0].Plane, tpc, cryo);
273  }
274  catch (geo::InvalidWireError const& e) { // adopt suggestion if possible
275  if (!e.hasSuggestedWire()) throw;
276  IdeWid = e.suggestedWireID();
277  mf::LogError("DisambigCheat") << "Detected a point out of its wire plane:\n"
278  << e.what() << "\nUsing suggested wire " << IdeWid << "\n";
279  }
280  geo::WireID storethis = IdeWid; // default...
281  bool foundmatch(false);
282  for( size_t w=0; w<cwids.size(); w++ ){
283  if (cwids[w].TPC!=tpc || cwids[w].Cryostat!=cryo) continue;
284  if( (unsigned int)std::abs((int)(IdeWid.Wire) - (int)(cwids[w].Wire)) <= fMaxWireShift[cwids[0].Plane] ){
285  storethis = cwids[w]; // ...apply correction
286  foundmatch = true;
287  break;
288  }
289  }
290  if( !foundmatch ){
291  mf::LogWarning("DisambigCheat") << "IDE NearestWire return more than 1 off from channel wids: wire "
292  << IdeWid.Wire;
294  }
295 
296  bool alreadyStored(false);
297  for( size_t wid=0; wid<widsWithIdes.size(); wid++ ) if( storethis == widsWithIdes[wid] ) alreadyStored = true;
298  if( !alreadyStored ) widsWithIdes.push_back(storethis);
299  } // end loop through ides from HitToSimIdes
300 
301  fHitToWids[ChanTime] = widsWithIdes;
302 
303  } // end U/V channel hit loop
304 
305  if(fHitToWids.size() != Ucount + Vcount){
306  //throw cet::exception("DisambigCheat");
307  mf::LogWarning("DisambigCheat")<<"Nhits mismatch: "<<fHitToWids.size()<<" "<<Ucount+Vcount;
308  }
309  return;
310  }
311 
312 
313  //-------------------------------------------------------------------
315  {
316  return;
317  }
318 
319 
320  //-------------------------------------------------------------------
322  {
323 
324  if(fFalseChanHits > 0)
325  mf::LogWarning("DisambigCheater") << fFalseChanHits << " hits had no associated IDE or WireIDs";
326 
327  return;
328  }
329 
330 
331  //-------------------------------------------------------------------
333  {
334  fChanHitLabel = p.get< std::string >("ChanHitLabel");
335  return;
336  }
337 
338 #endif // DisambigCheater_h
339 
341 
342 } // end hit namespace
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
void InitHitToWids(const std::vector< art::Ptr< recob::Hit > > &ChHits)
const std::vector< double > SimIDEsToXYZ(std::vector< sim::IDE > const &ides)
DisambigCheater(fhicl::ParameterSet const &p)
WireGeo const & Wire(unsigned int iwire) const
Definition: PlaneGeo.cxx:506
Planes which measure V.
Definition: geo_types.h:77
unsigned int Nplanes() const
Number of planes in this tpc.
Definition: TPCGeo.h:145
Declaration of signal hit object.
std::vector< unsigned int > fMaxWireShift
bool isValid
Whether this ID points to a valid element.
Definition: geo_types.h:129
art::ServiceHandle< geo::Geometry > geom
std::vector< geo::WireID > ChannelToWire(raw::ChannelID_t const channel) const
Returns a list of wires connected to the specified TPC channel.
CryostatID_t Cryostat
Index of cryostat.
Definition: geo_types.h:130
Definition of basic raw digits.
Planes which measure Z direction.
Definition: geo_types.h:79
WireID_t Wire
Index of the wire within its plane.
Definition: geo_types.h:313
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
unsigned int Ncryostats() const
Returns the number of cryostats in the detector.
void MakeDisambigHit(art::Ptr< recob::Hit > const &hit, geo::WireID const &wid, art::Ptr< recob::Wire > const &wire, art::Ptr< raw::RawDigit > const &rawdigits, recob::HitCollectionCreator &hcol)
static void declare_products(ModuleType &producer, std::string instance_name="", bool doWireAssns=true, bool doRawDigitAssns=true)
Declares the hit products we are going to fill.
Definition: HitCreator.h:1117
const std::vector< const sim::IDE * > HitToSimIDEs_Ps(recob::Hit const &hit)
geo::TPCID FindTPCAtPosition(double const worldLoc[3]) const
Returns the ID of the TPC at specified location.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Class managing the creation of a new recob::Hit object.
Definition: HitCreator.h:84
Planes which measure U.
Definition: geo_types.h:76
Helper functions to create a hit.
View_t View() const
Which coordinate does this plane measure.
Definition: PlaneGeo.h:171
void hits()
Definition: readHits.C:15
A class handling a collection of hits and its associations.
Definition: HitCreator.h:513
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
void reconfigure(fhicl::ParameterSet const &p)
T get(std::string const &key) const
Definition: ParameterSet.h:231
void produce(art::Event &e)
CryostatGeo const & Cryostat(geo::CryostatID const &cryoid) const
Returns the specified cryostat.
art::ServiceHandle< cheat::BackTrackerService > bt_serv
void emplace_back(recob::Hit &&hit, art::Ptr< recob::Wire > const &wire=art::Ptr< recob::Wire >(), art::Ptr< raw::RawDigit > const &digits=art::Ptr< raw::RawDigit >())
Adds the specified hit to the data collection.
Definition: HitCreator.cxx:245
The data type to uniquely identify a TPC.
Definition: geo_types.h:195
Definition of data types for geometry description.
float PeakTimeMinusRMS(float sigmas=+1.) const
Returns a time sigmas RMS away from the peak time.
Definition: Hit.h:240
void put_into(art::Event &)
Moves the data into an event.
Definition: HitCreator.h:663
Detector simulation of raw signals on wires.
unsigned int NTPC(unsigned int cstat=0) const
Returns the total number of TPCs in the specified cryostat.
float PeakTime() const
Time of the signal peak, in tick units.
Definition: Hit.h:219
Encapsulate the construction of a single detector plane.
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
const TPCGeo & TPC(unsigned int itpc) const
Return the itpc&#39;th TPC in the cryostat.
unsigned int Nwires() const
Number of wires in this plane.
Definition: PlaneGeo.h:250
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
object containing MC truth information necessary for making RawDigits and doing back tracking ...
Exception thrown on invalid wire number.
Definition: Exceptions.h:42
geo::WireID NearestWireID(geo::Point_t const &point, geo::PlaneID const &planeid) const
Returns the ID of wire closest to position in the specified TPC.
Declaration of basic channel signal object.
PlaneGeo const & Plane(geo::View_t view) const
Return the plane in the tpc with View_t view.
Definition: TPCGeo.cxx:299
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:49
void GetCenter(double *xyz, double localz=0.0) const
Fills the world coordinate of a point on the wire.
Definition: WireGeo.cxx:68
float PeakTimePlusRMS(float sigmas=+1.) const
Returns a time sigmas RMS away from the peak time.
Definition: Hit.h:237
TCEvent evt
Definition: DataStructs.cxx:5
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:464
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:203
Float_t e
Definition: plot.C:34
geo::WireID suggestedWireID() const
Returns a better wire ID.
Definition: Exceptions.h:120
recob::tracking::Plane Plane
Definition: TrackState.h:17
bool hasSuggestedWire() const
Returns whether we known a better wire number.
Definition: Exceptions.h:114
Float_t w
Definition: plot.C:23
recob::Hit && move()
Prepares the constructed hit to be moved away.
Definition: HitCreator.h:344
raw::ChannelID_t Channel() const
ID of the readout channel the hit was extracted from.
Definition: Hit.h:231
std::map< std::pair< double, double >, std::vector< geo::WireID > > fHitToWids
art framework interface to geometry description
Encapsulate the construction of a single detector plane.