All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbndcode/sbndcode/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 ////////////////////////////////////////////////////////////////////////
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 
21 //#include "lardata/Utilities/PtrMaker.h"
23 #include "canvas/Utilities/InputTag.h"
24 #include "fhiclcpp/ParameterSet.h"
25 #include "messagefacility/MessageLogger/MessageLogger.h"
26 #include "art_root_io/TFileService.h"
27 
31 
32 #include "TTree.h"
33 #include "TH1F.h"
34 #include "TH2F.h"
35 #include "TH3S.h"
36 #include "TProfile.h"
37 #include "TF1.h"
38 #include "TDatime.h"
39 
40 #include <iostream>
41 #include <stdio.h>
42 #include <sstream>
43 #include <vector>
44 #include <map>
45 #include <utility>
46 #include <cmath>
47 #include <memory>
48 
49 namespace sbnd{
50 
51 class CRTTzeroProducer : public art::EDProducer {
52 
53 public:
54 
55  explicit CRTTzeroProducer(fhicl::ParameterSet const & p);
56 
57  // The destructor generated by the compiler is fine for classes
58  // without bare pointers or other resource use.
59  // Plugins should not be copied or assigned.
60  CRTTzeroProducer(CRTTzeroProducer const &) = delete;
62  CRTTzeroProducer & operator = (CRTTzeroProducer const &) = delete;
64 
65  // Required functions.
66  void produce(art::Event & e) override;
67 
68  // Selected optional functions.
69  void beginJob() override;
70  void endJob() override;
71 
72 private:
73 
74  // Declare member data here.
75  // art::ServiceHandle<art::TFileService> tfs;
76 
77  std::string data_label_;
78  double max_time_difference_ ;//max time for coincidence
80  int verbose_ = 0;
81 
82 };
83 
84 void vmanip(std::vector<double> v, double* ave, double* rms);
85 
87 
88 CRTTzeroProducer::CRTTzeroProducer(fhicl::ParameterSet const & p)
89  : EDProducer(p),
90 
91  // Initialize member data here.
92  data_label_(p.get<std::string>("data_label")),
93  max_time_difference_(p.get<double>("max_time_difference")),
94  store_tzero_(p.get<int>("store_tzero")),
95  verbose_(p.get<int>("verbose"))
96 
97 {
98 
99  // Call appropriate produces<>() functions here.
100  if(store_tzero_ == 1) {
101  produces< std::vector<sbn::crt::CRTTzero> >();
102  produces<art::Assns<sbn::crt::CRTTzero, sbn::crt::CRTHit> >();
103  }
104 
105 }
106 
107 void CRTTzeroProducer::produce(art::Event & evt)
108 {
109  // Implementation of required member function here.
110 
111  art::Handle< std::vector<sbn::crt::CRTHit> > rawHandle;
112  evt.getByLabel(data_label_, rawHandle);
113  //check to make sure the data we asked for is valid
114  if(!rawHandle.isValid()){
115  std::cout << "Run " << evt.run() << ", subrun " << evt.subRun()
116  << ", event " << evt.event() << " has zero"
117  << " CRTHits " << " in module " << data_label_ << std::endl;
118  std::cout << std::endl;
119  return;
120  }
121 
122  //get better access to the data
123  std::vector<sbn::crt::CRTHit> const& CRTHitCollection(*rawHandle);
124 
125  //CRTTzero collection on this event
126  std::unique_ptr<std::vector<sbn::crt::CRTTzero> > CRTTzeroCol(new std::vector<sbn::crt::CRTTzero>);
127 
128  // Output collections
129  std::unique_ptr<art::Assns<sbn::crt::CRTTzero, sbn::crt::CRTHit>> outputHits(new art::Assns<sbn::crt::CRTTzero, sbn::crt::CRTHit>);
130  // auto outputHits = std::make_unique<art::Assns<sbn::crt::CRTTzero, sbn::crt::CRTHit>>();
131  art::PtrMaker<sbn::crt::CRTHit> hitPtrMaker(evt, rawHandle.id());
132  art::PtrMaker<sbn::crt::CRTTzero> tzeroPtrMaker(evt);
133 
134  int N_CRTHits = CRTHitCollection.size();
135 
136  //int iflag[1000] = {};
137  std::vector<int> iflag;
138  for(int i = 0; i < N_CRTHits; i++) iflag.push_back(0);
139  int nTzero = 0;
140  uint planeA, planeB;
141  for(int i = 0; i < N_CRTHits; i++) {//A
142  if (iflag[i]==0) { // new tzero
143  //temporary hit collection for each tzero
144  std::vector<art::Ptr<sbn::crt::CRTHit>> CRTHitCol;
145  sbn::crt::CRTHit CRTHiteventA = CRTHitCollection[i];
146  art::Ptr<sbn::crt::CRTHit> hptr = hitPtrMaker(i);
147  CRTHitCol.push_back(hptr);
148 
149  double time_s_A = 0; //CRTHiteventA.ts0_s;
150  double time_ns_A = CRTHiteventA.ts1_ns;
151 
152  iflag[i]=1;
153 
154  // create and initialize, ugly code :(
155  sbn::crt::CRTTzero CRTcanTzero;
156  CRTcanTzero.ts0_ns=0;
157  CRTcanTzero.ts1_ns=0;
158  for (int j=0; j<7 ;++j) { // NUMBER OF PLANES, CHANGE TO 7
159  CRTcanTzero.nhits[j]=0;
160  CRTcanTzero.pes[j]=0;
161  }
162 
163  //
164  CRTcanTzero.ts0_s=CRTHiteventA.ts0_s;
165  CRTcanTzero.ts0_s_err=0;
166  planeA = CRTHiteventA.plane;
167  CRTcanTzero.nhits[planeA]=1;
168 
169  int icount=1;
170 
171  CRTcanTzero.pes[planeA]=CRTHiteventA.peshit;
172  for(int j = i+1; j < N_CRTHits; j++) {//B
173  if (iflag[j]==0) {
174  sbn::crt::CRTHit CRTHiteventB = CRTHitCollection[j];
175  //look for coincidences
176  double time_s_B = 0; //CRTHiteventB.ts0_s;
177  double time_ns_B = CRTHiteventB.ts1_ns;
178  double time_diff = time_ns_B - time_ns_A;
179 
180  if( (time_s_A == time_s_B) && (abs(time_diff)<max_time_difference_) ){//D
181  art::Ptr<sbn::crt::CRTHit> hptr = hitPtrMaker(j);
182  CRTHitCol.push_back(hptr);
183  planeB = CRTHiteventB.plane;
184  CRTcanTzero.nhits[planeB]+=1;
185  CRTcanTzero.pes[planeB]+=CRTHiteventB.peshit;
186  iflag[j]=1;
187  CRTcanTzero.ts1_ns+=(int)(time_diff);
188  CRTcanTzero.ts0_ns+=(CRTHiteventB.ts0_ns-CRTHiteventA.ts0_ns);
189  icount++;
190  }
191  }
192  } // done with this tzero
193 
194  // Make a tzero data product
195  CRTcanTzero.ts1_ns/=icount;
196  CRTcanTzero.ts1_ns+=(int)time_ns_A;
197  CRTcanTzero.ts0_ns/=icount;
198  CRTcanTzero.ts0_ns+=(int)CRTHiteventA.ts0_ns;
199  CRTcanTzero.ts1_ns_err=0.;
200  CRTcanTzero.ts0_ns_err=0.;
201  CRTTzeroCol->push_back(CRTcanTzero);
202  nTzero++;
203 
204  //associate hits to this Tzero
205  art::Ptr<sbn::crt::CRTTzero> aptz = tzeroPtrMaker(CRTTzeroCol->size()-1);
206  util::CreateAssn(*this,evt,aptz,CRTHitCol,*outputHits);
207 
208  }//B
209 
210  }//A
211 
212  //store tzero collection into event
213  if(store_tzero_ == 1) {
214  evt.put(std::move(CRTTzeroCol));
215  evt.put(std::move(outputHits));
216  }
217 }
218 
220 {
221 
222 }
223 
225 {
226  // Implementation of optional member function here.
227 }
228 
229 DEFINE_ART_MODULE(CRTTzeroProducer)
230 
231 } //namespace sbnd
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
void set_def(sbn::crt::CRTTzero tz)
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
void vmanip(std::vector< float > v, float *ave, float *rms)
pdgs p
Definition: selectors.fcl:22
CRTTzeroProducer & operator=(CRTTzeroProducer const &)=delete
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
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)
bool CreateAssn(art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t index=UINT_MAX)
Creates a single one-to-one association.
do i e
stream1 can override from command line with o or output services user sbnd
uint16_t ts0_s_err
Definition: CRTTzero.hh:24
uint32_t ts0_s
Definition: CRTTzero.hh:23
TCEvent evt
Definition: DataStructs.cxx:8
BEGIN_PROLOG could also be cout
uint16_t ts0_ns_err
Definition: CRTTzero.hh:26