All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
icaruscode/icaruscode/CRT/CRTTzeroProducer_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CRTTzeroProducer
3 // Module Type: producer
4 // File: CRTTzeroProducer_module.cc
5 // Description: Module for constructiong over-simplified CRT tracks.
6 // Copied from CRTTrackProducer by David Lorca Galindo
7 // Edited by Michelle Stancari April 3, 2018
8 // Ported to icaruscode by Chris.Hilgenberg@colostate.edu March 4, 2019
9 ////////////////////////////////////////////////////////////////////////
10 #include "art/Framework/Core/EDProducer.h"
11 #include "art/Framework/Core/ModuleMacros.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "art/Framework/Principal/Run.h"
15 #include "art/Framework/Principal/SubRun.h"
16 #include "canvas/Persistency/Common/Ptr.h"
17 #include "canvas/Persistency/Common/Assns.h"
18 #include "canvas/Persistency/Provenance/ProductID.h"
19 #include "art/Persistency/Common/PtrMaker.h"
20 //#include "lardata/Utilities/PtrMaker.h"
22 #include "canvas/Utilities/InputTag.h"
23 #include "fhiclcpp/ParameterSet.h"
24 #include "messagefacility/MessageLogger/MessageLogger.h"
25 #include "art_root_io/TFileService.h"
29 #include "TTree.h"
30 #include "TH1F.h"
31 #include "TH2F.h"
32 #include "TH3S.h"
33 #include "TProfile.h"
34 #include "TF1.h"
35 #include "TDatime.h"
36 #include <iostream>
37 #include <stdio.h>
38 #include <sstream>
39 #include <vector>
40 #include <map>
41 #include <utility>
42 #include <cmath>
43 #include <memory>
44 
45 namespace icarus{
46 
47 class CRTTzeroProducer : public art::EDProducer {
48 
49 public:
50 
51  explicit CRTTzeroProducer(fhicl::ParameterSet const & p);
52 
53  // The destructor generated by the compiler is fine for classes
54  // without bare pointers or other resource use.
55  // Plugins should not be copied or assigned.
56  CRTTzeroProducer(CRTTzeroProducer const &) = delete;
58  CRTTzeroProducer & operator = (CRTTzeroProducer const &) = delete;
60 
61  // Required functions.
62  void produce(art::Event & e) override;
63 
64  // Selected optional functions.
65  void beginJob() override;
66  void endJob() override;
67 
68 private:
69 
70  // Declare member data here.
71  // art::ServiceHandle<art::TFileService> tfs;
72  std::string data_label_;
73  double max_time_difference_ ;//max time for coincidence
75  int verbose_ = 0;
76 
77 };
78 
79 void vmanip(std::vector<double> v, double* ave, double* rms);
81 
82 CRTTzeroProducer::CRTTzeroProducer(fhicl::ParameterSet const & p)
83  :
84  EDProducer{p},
85  // Initialize member data here.
86  data_label_(p.get<std::string>("data_label")),
87  max_time_difference_(p.get<double>("max_time_difference")),
88  store_tzero_(p.get<int>("store_tzero")),
89  verbose_(p.get<int>("verbose"))
90 {
91  // Call appropriate produces<>() functions here.
92  if(store_tzero_ == 1) {
93  produces< std::vector<sbn::crt::CRTTzero> >();
94  //produces<art::Assns<sbn::crt::CRTTzero, sbn::crt::CRTHit> >();
95  }
96 
97 }
98 
99 void CRTTzeroProducer::produce(art::Event & evt)
100 {
101  std::cout << "getting handle to CRTHits..." << std::endl;
102 
103  // Implementation of required member function here.
104  art::Handle< std::vector<sbn::crt::CRTHit> > rawHandle;
105  evt.getByLabel(data_label_, rawHandle);
106  //check to make sure the data we asked for is valid
107  if(!rawHandle.isValid()){
108  std::cout << "Run " << evt.run() << ", subrun " << evt.subRun()
109  << ", event " << evt.event() << " has zero"
110  << " CRTHits " << " in module " << data_label_ << std::endl;
111  std::cout << std::endl;
112  return;
113  }
114 
115  //get better access to the data
116  std::vector<sbn::crt::CRTHit> const& CRTHitCollection(*rawHandle);
117 
118  //CRTTzero collection on this event
119  std::unique_ptr<std::vector<sbn::crt::CRTTzero> > CRTTzeroCol(new std::vector<sbn::crt::CRTTzero>);
120 
121  // Output collections
122  //std::unique_ptr<art::Assns<sbn::crt::CRTTzero, sbn::crt::CRTHit>> outputHits(new art::Assns<sbn::crt::CRTTzero, sbn::crt::CRTHit>);
123 
124  // auto outputHits = std::make_unique<art::Assns<sbn::crt::CRTTzero, sbn::crt::CRTHit>>();
125  art::PtrMaker<sbn::crt::CRTHit> hitPtrMaker(evt, rawHandle.id());
126  art::PtrMaker<sbn::crt::CRTTzero> tzeroPtrMaker(evt);
127 
128  int N_CRTHits = CRTHitCollection.size();
129  //int iflag[1000] = {};
130  std::vector<int> iflag;
131 
132  std::cout << "found " << N_CRTHits << " CRTHits" << std::endl;
133 
134  for(int i = 0; i < N_CRTHits; i++) iflag.push_back(0);
135 
136  int nTzero = 0;
137  uint planeA, planeB;
138 
139  for(int i = 0; i < N_CRTHits; i++) {//A
140  if (iflag[i]==0) { // new tzero
141  //temporary hit collection for each tzero
142  std::vector<art::Ptr<sbn::crt::CRTHit>> CRTHitCol;
143  sbn::crt::CRTHit CRTHiteventA = CRTHitCollection[i];
144  art::Ptr<sbn::crt::CRTHit> hptr = hitPtrMaker(i);
145  CRTHitCol.push_back(hptr);
146  double time_s_A = 0; //CRTHiteventA.ts0_s;
147  double time_ns_A = CRTHiteventA.ts1_ns;
148  iflag[i]=1;
149  // create and initialize, ugly code :(
150  sbn::crt::CRTTzero CRTcanTzero;
151  CRTcanTzero.ts0_ns=0;
152  CRTcanTzero.ts1_ns=0;
153  for (int j=0; j<7 ;++j) { // NUMBER OF PLANES, CHANGE TO 7
154  CRTcanTzero.nhits[j]=0;
155  CRTcanTzero.pes[j]=0;
156  }
157  //
158  CRTcanTzero.ts0_s=CRTHiteventA.ts0_s;
159  CRTcanTzero.ts0_s_err=0;
160  planeA = CRTHiteventA.plane;
161  CRTcanTzero.nhits[planeA]=1;
162  int icount=1;
163  CRTcanTzero.pes[planeA]=CRTHiteventA.peshit;
164  for(int j = i+1; j < N_CRTHits; j++) {//B
165  if (iflag[j]==0) {
166  sbn::crt::CRTHit CRTHiteventB = CRTHitCollection[j];
167  //look for coincidences
168  double time_s_B = 0; //CRTHiteventB.ts0_s;
169  double time_ns_B = CRTHiteventB.ts1_ns;
170  double time_diff = time_ns_B - time_ns_A;
171  if( (time_s_A == time_s_B) && (abs(time_diff)<max_time_difference_) ){//D
172  art::Ptr<sbn::crt::CRTHit> hptr = hitPtrMaker(j);
173  CRTHitCol.push_back(hptr);
174  planeB = CRTHiteventB.plane;
175  CRTcanTzero.nhits[planeB]+=1;
176  CRTcanTzero.pes[planeB]+=CRTHiteventB.peshit;
177  iflag[j]=1;
178  CRTcanTzero.ts1_ns+=(int)(time_diff);
179  CRTcanTzero.ts0_ns+=(CRTHiteventB.ts0_ns-CRTHiteventA.ts0_ns);
180  icount++;
181  }
182  }
183  } // done with this tzero
184  // Make a tzero data product
185  CRTcanTzero.ts1_ns/=icount;
186  CRTcanTzero.ts1_ns+=(int)time_ns_A;
187  CRTcanTzero.ts0_ns/=icount;
188  CRTcanTzero.ts0_ns+=(int)CRTHiteventA.ts0_ns;
189  CRTcanTzero.ts1_ns_err=0.;
190  CRTcanTzero.ts0_ns_err=0.;
191  CRTTzeroCol->push_back(CRTcanTzero);
192  nTzero++;
193  //associate hits to this Tzero
194  art::Ptr<sbn::crt::CRTTzero> aptz = tzeroPtrMaker(CRTTzeroCol->size()-1);
195  std::cout << "produced " << CRTTzeroCol->size() << " CRTTzero objects" << std::endl;
196  std::cout << "pointer to CRTTzero = " << aptz << std::endl;
197  //util::CreateAssn(*this,evt,aptz,CRTHitCol,*outputHits);
198 
199  }//B
200 
201  }//A
202  //store tzero collection into event
203 
204  std::cout << "about to move CRTTzero and art::Assns to file..." << std::endl;
205 
206  if(store_tzero_ == 1) {
207  evt.put(std::move(CRTTzeroCol));
208  //evt.put(std::move(outputHits));
209  }
210 }
211 
213 {
214 
215 }
216 
218 {
219  // Implementation of optional member function here.
220 }
221 
222 DEFINE_ART_MODULE(CRTTzeroProducer)
223 
224 } //namespace icarus
double ts1_ns
Timestamp T1 ([signal time w.r.t. Trigger time]), in UTC absolute time scale in nanoseconds from the ...
Definition: CRTHit.hh:34
double pes[7]
Definition: CRTTzero.hh:32
int plane
Name of the CRT wall (in the form of numbers).
Definition: CRTHit.hh:36
float peshit
Total photo-electron (PE) in a crt hit.
Definition: CRTHit.hh:27
pdgs p
Definition: selectors.fcl:22
uint32_t ts0_ns
Definition: CRTTzero.hh:25
uint16_t ts1_ns_err
Definition: CRTTzero.hh:28
uint64_t ts0_s
Second-only part of timestamp T0.
Definition: CRTHit.hh:29
void set_def(sbn::crt::CRTTzero tz)
double ts0_ns
Timestamp T0 (from White Rabbit), in UTC absolute time scale in nanoseconds from the Epoch...
Definition: CRTHit.hh:32
T abs(T value)
void vmanip(std::vector< double > v, double *ave, double *rms)
do i e
uint16_t ts0_s_err
Definition: CRTTzero.hh:24
uint32_t ts0_s
Definition: CRTTzero.hh:23
TCEvent evt
Definition: DataStructs.cxx:8
CRTTzeroProducer & operator=(CRTTzeroProducer const &)=delete
BEGIN_PROLOG could also be cout
uint16_t ts0_ns_err
Definition: CRTTzero.hh:26