HitHandScan_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: HitHandScan
3 // Module Type: producer
4 // File: HitHandScan_module.cc
5 //
6 // Generated at Tue May 3 01:14:23 2016 by Matthew Thiesse using artmod
7 // from cetpkgsupport v1_10_01.
8 ////////////////////////////////////////////////////////////////////////
9 
17 #include "fhiclcpp/ParameterSet.h"
19 
20 #include "canvas/Persistency/Common/FindOneP.h"
27 
28 #include <sstream>
29 #include <string>
30 #include <fstream>
31 #include <vector>
32 #include <memory>
33 #include <map>
34 
35 #include "TMath.h"
36 
37 class HitHandScan;
38 
39 typedef struct {
40  unsigned int run;
41  unsigned int event;
42  unsigned int tpc;
43  unsigned int plane;
44  double wire;// wire number in this tpc on this plane
45  double tick;// IN RELATION TO EXTERNAL TRIGGER, NOT NECESSARILY THE FIRST TICK IN THE EVENT
46  unsigned int numpoly;
47 } Point;
48 
49 class HitHandScan : public art::EDProducer {
50 public:
51  explicit HitHandScan(fhicl::ParameterSet const & p);
52  // The destructor generated by the compiler is fine for classes
53  // without bare pointers or other resource use.
54 
55  // Plugins should not be copied or assigned.
56  HitHandScan(HitHandScan const &) = delete;
57  HitHandScan(HitHandScan &&) = delete;
58  HitHandScan & operator = (HitHandScan const &) = delete;
59  HitHandScan & operator = (HitHandScan &&) = delete;
60 
61  // Required functions.
62  void produce(art::Event & e) override;
63 
64  // Selected optional functions.
65  void reconfigure(fhicl::ParameterSet const & p) ;
66 
67 private:
68  bool insidePolygon(std::vector<Point> polygon, Point ptest);
69 
72 
73 };
74 
75 
77 {
78  this->reconfigure(p);
80 }
81 
83 {
84  auto prevHitHandle = e.getHandle< std::vector< recob::Hit> >(fPreviousHitModuleLabel);
85 
86  art::FindOneP<recob::Wire> wires(prevHitHandle,e,fPreviousHitModuleLabel);
87  art::FindOneP<raw::RawDigit> rawdigits(prevHitHandle,e,fPreviousHitModuleLabel);
88 
89  recob::HitCollectionCreator hcol(e, wires.isValid(), rawdigits.isValid());
90 
91  std::vector<Point> trackBox;
92 
93  std::string readrun,readevent,readtpc,readplane,readwire,readtick,readnumpoly;
94  unsigned int run,event,tpc,plane,numpoly;
95  double tick,wire;
96  int linenumber = 0;
98  std::ifstream handscanfile(fHandScanFileName.c_str(),std::ifstream::in);
99  if (handscanfile.is_open())
100  {
101  while (std::getline(handscanfile,line))
102  {
103  linenumber++;
104  std::stringstream ss(line);
105  ss >> std::ws >> readrun >> readevent >> readtpc >> readplane >> readwire >> readtick >> readnumpoly;
106  try
107  {
108  run = stoul(readrun);
109  event = stoul(readevent);
110  tick = stod(readtick);
111  tpc = stoul(readtpc);
112  plane = stoul(readplane);
113  wire = stod(readwire);
114  numpoly = stoul(readnumpoly);
115 
116  Point p;
117  p.run = run;
118  p.event = event;
119  p.tpc = tpc;
120  p.plane = plane;
121  p.wire = wire;
122  p.tick = tick;
123  p.numpoly = numpoly;
124  trackBox.push_back(p);
125  }
126  catch (const std::exception& ex)
127  {
128  std::cout << "Line # " << linenumber << " is invalid, skipping..." << std::endl;
129  }
130  }
131  handscanfile.close();
132  }
133  else
134  {
135  std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
136  << "NO FILE FOUND WITH HAND SCANNED HIT DATA\n"
137  << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
138  return;
139  }
140 
141  for (size_t i_hit = 0; i_hit < prevHitHandle->size(); i_hit++)
142  {
143  art::Ptr<recob::Hit> phit(prevHitHandle,i_hit);
144 
145  art::Ptr<recob::Wire> wire(wires.at(i_hit));
146  art::Ptr<raw::RawDigit> rawdigit(rawdigits.at(i_hit));
147 
148  Point ptest;
149  ptest.run = e.run();
150  ptest.event = e.event();
151  ptest.tpc = phit->WireID().TPC;
152  ptest.plane = phit->WireID().Plane;
153  ptest.wire = phit->WireID().Wire;
154  ptest.tick = phit->PeakTime();
155 
156  std::map< unsigned int, std::vector<Point> > polygons;
157  for (auto const &poly : trackBox)
158  {
159  if (poly.run == ptest.run
160  && poly.event == ptest.event
161  && poly.tpc == ptest.tpc
162  && poly.plane == ptest.plane)
163  {
164  polygons[poly.numpoly].push_back(poly);
165  }
166  }
167  bool acceptPoint = false;
168  for (auto const &p : polygons)
169  {
170  if (insidePolygon(p.second,ptest))
171  {
172  acceptPoint = true;
173  }
174  }
175  if (acceptPoint)
176  {
177  hcol.emplace_back(*phit,wire,rawdigit);
178  }
179  }
180 
181  hcol.put_into(e);
182 
183 }
184 
186 {
187  fHandScanFileName = p.get<std::string>("HandScanFileName");
188  fPreviousHitModuleLabel = p.get<std::string>("PreviousHitModuleLabel");
189 }
190 
191 bool HitHandScan::insidePolygon(std::vector<Point> polygon, Point ptest)
192 {
193  size_t n = polygon.size();
194  size_t i, j;
195  bool c = false;
196  for (i = 0, j = n - 1; i < n; j = i++)
197  {
198  if ( ((polygon[i].tick > ptest.tick) != (polygon[j].tick > ptest.tick)) &&
199  (ptest.wire < (polygon[j].wire - polygon[i].wire) *
200  (ptest.tick - polygon[i].tick) / (polygon[j].tick - polygon[i].tick) + polygon[i].wire) )
201  c = !c;
202  }
203  return c;
204 }
205 
EventNumber_t event() const
Definition: DataViewImpl.cc:85
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
unsigned int tpc
geo::WireID WireID() const
Definition: Hit.h:233
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
WireID_t Wire
Index of the wire within its plane.
Definition: geo_types.h:580
std::string fPreviousHitModuleLabel
HitHandScan & operator=(HitHandScan const &)=delete
static void declare_products(art::ProducesCollector &collector, std::string instance_name="", bool doWireAssns=true, bool doRawDigitAssns=true)
Declares the hit products we are going to fill.
Definition: HitCreator.cxx:248
unsigned int numpoly
Helper functions to create a hit.
const double e
A class handling a collection of hits and its associations.
Definition: HitCreator.h:508
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
unsigned int run
std::string fHandScanFileName
std::tuple< double, double, const reco::ClusterHit3D * > Point
Definitions used by the VoronoiDiagram algorithm.
Definition: DCEL.h:44
std::void_t< T > n
T get(std::string const &key) const
Definition: ParameterSet.h:271
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
p
Definition: test.py:223
RunNumber_t run() const
Definition: DataViewImpl.cc:71
void reconfigure(fhicl::ParameterSet const &p)
PlaneID_t Plane
Index of the plane within its TPC.
Definition: geo_types.h:493
Definition of data types for geometry description.
unsigned int event
ProducesCollector & producesCollector() noexcept
float PeakTime() const
Time of the signal peak, in tick units.
Definition: Hit.h:218
Declaration of signal hit object.
bool insidePolygon(std::vector< Point > polygon, Point ptest)
unsigned int plane
void produce(art::Event &e) override
void line(double t, double *p, double &x, double &y, double &z)
Declaration of basic channel signal object.
HitHandScan(fhicl::ParameterSet const &p)
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:406
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)