T0Counter_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: T0Counter
3 // Module Type: producer
4 // File: T0Counter_module.cc
5 //
6 // Generated at Tue Mar 24 08:49:12 2015 by Matthew Thiesse using artmod
7 // from cetpkgsupport v1_08_04.
8 ////////////////////////////////////////////////////////////////////////
9 
16 #include "art_root_io/TFileService.h"
18 #include "fhiclcpp/ParameterSet.h"
20 
26 
27 #include <memory>
28 #include <map>
29 #include <limits>
30 
31 #include "TTree.h"
32 
33 namespace dune {
34  class T0Counter;
35 }
36 
38 public:
39  explicit T0Counter(fhicl::ParameterSet const & p);
40  // The destructor generated by the compiler is fine for classes
41  // without bare pointers or other resource use.
42 
43  // Plugins should not be copied or assigned.
44  T0Counter(T0Counter const &) = delete;
45  T0Counter(T0Counter &&) = delete;
46  T0Counter & operator = (T0Counter const &) = delete;
47  T0Counter & operator = (T0Counter &&) = delete;
48 
49  // Required functions.
50  void produce(art::Event & e) override;
51 
52  // Selected optional functions.
53  void beginJob() override;
54 
55 private:
56 
57  // data structure to hold t0 information and ExternalTrigger data products
58  struct t0 {
59 
60  std::vector< std::pair< unsigned int, double> > idtime;
61  std::vector< art::Ptr< raw::ExternalTrigger> > trigs;
62 
63  // returns true if the trigger is within tolerance of at least one other trigger in the t0
64  bool testCoincidence(double tim, double tol) const {// tol is in units of ns
65  for(auto const &i : idtime) {
66  if (tim >= i.second-tol && tim <= i.second+tol) return true;
67  }
68  return false;
69  }
70 
71  // calculate average time of hit
72  double avgTime() const {
73  double t = 0;
74  int n = 0;
75  for(auto const &i : idtime) t += i.second, ++n;
76  if (n != 0) return t/n;
78  }
79 
80  // insert another trigger into the t0 object
81  void insert(double tim, unsigned int id, art::Ptr< raw::ExternalTrigger> trig) {
82  idtime.push_back(std::pair<unsigned int,double>(id,tim));
83  trigs.push_back(trig);
84  return;
85  }
86 
87  // constructor
88  t0(double tim, unsigned int id, art::Ptr< raw::ExternalTrigger> trig) {
89  insert(tim,id,trig);
90  }
91  };
92 
93  double tick2Time(unsigned int t);
94 
95  // fhicl parameters
100  bool fVerbose;
101  bool fMakeTree;
102 
103  // calculated parameters
106 
107  // anab::T0 data members to fill
108  double t0time;
109  unsigned int trigtype;
110  int trigbits;
111  //int t0id; // unused
112 
113  // output TTree
114  TTree *fTree;
115  int run;
116  int subrun;
117  int event;
118  int ntrigs;
119  unsigned int auxdetid;
120  unsigned int tick;
121  double time;
122 
123 
124 };
125 
126 /////////////////////////////////////////////////////////////////////////////////////
127 
129  : EDProducer{p}, fTriggerModuleLabel(p.get<std::string>("TriggerModuleLabel")),
130  fClockSpeedCounter(p.get<double>("ClockSpeedCounter")), // MHz
131  fCombinedTimeDelay(p.get<double>("CombinedTimeDelay")), // ns
132  fCoincidenceTolerance(p.get<int>("CoincidenceTolerance")), // ticks
133  fVerbose(p.get<bool>("Verbose",true)),
134  fMakeTree(p.get<bool>("MakeTree",false))
135 {
137 
138  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataForJob();
139 
140  fTriggerOffsetTPC = clockData.TriggerOffsetTPC()*1.e3; // ns
141 
142  produces< std::vector< anab::T0 > >();
143  produces< art::Assns< anab::T0, raw::ExternalTrigger> >();
144 }
145 
146 /////////////////////////////////////////////////////////////////////////////////////
147 
149 {
150  // make collection of T0 objects, and associations
151  std::unique_ptr< std::vector< anab::T0> > t0col(new std::vector<anab::T0>);
152  std::unique_ptr< art::Assns< anab::T0, raw::ExternalTrigger> > assn( new art::Assns< anab::T0, raw::ExternalTrigger>);
153 
154  // get basic info
155  run = e.run();
156  subrun = e.subRun();
157  event = e.id().event();
158 
159  // get raw::ExternalTriggers
160  auto externalTriggerListHandle = e.getHandle< std::vector< raw::ExternalTrigger> >(fTriggerModuleLabel);
161  if (externalTriggerListHandle)
162  {
163 
164  std::vector< art::Ptr< raw::ExternalTrigger> > trigs;
165  art::fill_ptr_vector(trigs,externalTriggerListHandle);
166 
167  ntrigs = externalTriggerListHandle->size();
168 
169  // this vector will contain t0 objects in a nice, convenient, organised package
170  std::vector<t0> t0vect;
171 
172  // fill t0vect with triggers, group if times are within fCoincidenceTolerance
173  for (auto const& trig : trigs) {
174  auxdetid = trig->GetTrigID();
175  tick = trig->GetTrigTime();
176  time = tick2Time(tick);
177  if (fMakeTree) fTree->Fill();
178 
179  // convert # PENN ticks tolerance to nsec
181 
182  // loop over t0vect
184  for (t0it = t0vect.begin(); t0it != t0vect.end(); ++t0it) {
185  // get t0 object
186  t0* t = &(*t0it);
187  // test for coincidence with all triggers associated with t0 object
188  if (t->testCoincidence(time,tol)) {
189  // insert trigger into t0
190  t->insert(time,auxdetid,trig);
191  // stop loop after one hit to prevent multiple insertions
192  break;
193  }
194  }
195  // if the above loop didn't find a coincidence, create a new t0 object
196  if (t0it == t0vect.end()) t0vect.push_back(t0(time,auxdetid,trig));
197  }
198 
199  // print information, if interested
200  if (fVerbose) {
201  int num = 0;
202  for (auto const &i : t0vect) {
203  std::cout << "Trigger " << num << " has " << i.idtime.size() << " triggers associated." << std::endl;
204  for (auto const &tit : i.idtime) {
205  std::cout << " time=" << std::fixed << std::setprecision(1) << tit.second << " ID=" << tit.first << std::endl;
206  }
207  ++num;
208  }
209  }
210 
211 
212  // make anab::T0 and Assns<anab::T0, raw::ExternalTrigger>
213  for (auto const &i : t0vect) {
214  t0time = i.avgTime(); // calculate average time of triggers, use as T0 time
215  trigtype = 1; // use 1 for counters
216  trigbits = i.idtime.size(); // number of associated triggers
217  t0col->push_back(anab::T0(t0time,
218  trigtype,
219  trigbits,
220  (*t0col).size()
221  ));
222  util::CreateAssn(*this, e, *t0col, i.trigs, *assn);
223  }
224 
225  }
226  e.put(std::move(t0col));
227  e.put(std::move(assn));
228 
229 }
230 
231 /////////////////////////////////////////////////////////////////////////////////////
232 
234 {
236 
237  if (fMakeTree) {
238  fTree = tfs->make<TTree>("T0Counter","T0Counter");
239  fTree->Branch("run",&run,"run/I");
240  fTree->Branch("subrun",&subrun,"subrun/I");
241  fTree->Branch("event",&event,"event/I");
242  fTree->Branch("ntrigs",&ntrigs,"ntrigs/I");
243  fTree->Branch("auxdetid",&auxdetid,"auxdetid/I");
244  fTree->Branch("tick",&tick,"tick/I");
245  fTree->Branch("time",&time,"time/D");
246  }
247 }
248 
249 /////////////////////////////////////////////////////////////////////////////////////
250 
251 double dune::T0Counter::tick2Time(unsigned int t)
252 {
253  double calctime = (double)t+0.5;//get central value of tick bin
254  calctime *= fSampleTimeCounter;//convert tick to time in ns
255  calctime += fTriggerOffsetTPC;//adjust for possible TPC window offset
256  calctime -= fCombinedTimeDelay;//correct for delay in signal propagation in cables and processing
257  return calctime;
258 }
259 
code to link reconstructed objects back to the MC truth information
intermediate_table::iterator iterator
std::vector< std::pair< unsigned int, double > > idtime
void produce(art::Event &e) override
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
auto const tol
Definition: SurfXYZTest.cc:16
void beginJob() override
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
void insert(double tim, unsigned int id, art::Ptr< raw::ExternalTrigger > trig)
Definition: T0.h:16
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
T0Counter & operator=(T0Counter const &)=delete
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
std::void_t< T > n
def move(depos, offset)
Definition: depos.py:107
double avgTime() const
unsigned int trigtype
p
Definition: test.py:223
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:78
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.
RunNumber_t run() const
Definition: DataViewImpl.cc:71
T0Counter(fhicl::ParameterSet const &p)
bool testCoincidence(double tim, double tol) const
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
unsigned int auxdetid
EventNumber_t event() const
Definition: EventID.h:116
t0(double tim, unsigned int id, art::Ptr< raw::ExternalTrigger > trig)
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
EventID id() const
Definition: Event.cc:34
QTextStream & endl(QTextStream &s)
Event finding and building.
std::vector< art::Ptr< raw::ExternalTrigger > > trigs
std::string fTriggerModuleLabel
double tick2Time(unsigned int t)