All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FullOpHitFinder_module.cc
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset: 2; -*-
2 // Gleb Sinev, Duke, 2016
3 //
4 // This module finds periods of time-localized activity
5 // on each channel of the optical system and creates OpHits.
6 //
7 // Split from OpFlashFinder_module.cc
8 // by Ben Jones, MIT, 2013
9 //
10 
11 // LArSoft includes
13 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
29 
30 // Framework includes
31 #include "art/Framework/Core/EDProducer.h"
32 #include "art/Framework/Core/ModuleMacros.h"
33 #include "art/Framework/Principal/Event.h"
34 #include "fhiclcpp/ParameterSet.h"
35 #include "art/Framework/Principal/Handle.h"
36 #include "canvas/Utilities/Exception.h"
37 
38 // ROOT includes
39 #include <TTree.h>
40 #include <TFile.h>
41 // C++ Includes
42 #include <map>
43 #include <string>
44 #include <memory>
45 
46 namespace opdet {
47 
48  class FullOpHitFinder : public art::EDProducer{
49  public:
50 
51  // Standard constructor and destructor for an ART module.
52  explicit FullOpHitFinder(const fhicl::ParameterSet&);
53  virtual ~FullOpHitFinder();
54 
55  // The producer routine, called once per event.
56  void produce(art::Event&);
57 
58  void beginJob();
59  void endJob();
60 
61  private:
62  std::map< int, int > GetChannelMap();
63  std::vector< double > GetSPEScales();
64  std::vector< double > GetSPEShifts();
65 
66  // The parameters we'll read from the .fcl file.
67  std::string fInputModule; // Input tag for OpDetWaveform collection
68  std::vector< std::string > fInputLabels;
69  std::set< unsigned int > fChannelMasks;
70 
74 
75 
76  std::string _output_filename;
77  std::vector<double> _tstart_v,_tmax_v,_tend_v,_tcross_v;
78  std::vector<double> _amp_v,_area_v;
79  std::vector<double> _ped_mean_v,_ped_sigma_v;
80  std::vector<double> _wf;
81  double _tstart;
83 
84  TTree* _wftree;
85  TTree* _hittree;
86  TTree* _geotree;
87  TFile* _ofile;
88 
89  };
90 
91 }
92 
93 namespace opdet {
94  DEFINE_ART_MODULE(FullOpHitFinder)
95 }
96 
97 namespace opdet {
98 
99  //----------------------------------------------------------------------------
100  // Constructor
101  FullOpHitFinder::FullOpHitFinder(const fhicl::ParameterSet & pset):
102  EDProducer{pset},
103  fPulseRecoMgr()
104  {
105 
106  // Indicate that the Input Module comes from .fcl
107  fInputModule = pset.get< std::string >("InputModule");
108  fInputLabels = pset.get< std::vector< std::string > >("InputLabels");
109 
110  for (auto const& ch : pset.get< std::vector< unsigned int > >
111  ("ChannelMasks", std::vector< unsigned int >()))
112  fChannelMasks.insert(ch);
113 
114  // Initialize the hit finder algorithm
115  auto const hit_alg_pset = pset.get< fhicl::ParameterSet >("HitAlgoPset");
116  std::string threshAlgName = hit_alg_pset.get< std::string >("Name");
117  if (threshAlgName == "Threshold")
118  fThreshAlg = new pmtana::AlgoThreshold(hit_alg_pset);
119  else if (threshAlgName == "SiPM")
120  fThreshAlg = new pmtana::AlgoSiPM(hit_alg_pset);
121  else if (threshAlgName == "SlidingWindow")
122  fThreshAlg = new pmtana::AlgoSlidingWindow(hit_alg_pset);
123  else if (threshAlgName == "FixedWindow")
124  fThreshAlg = new pmtana::AlgoFixedWindow(hit_alg_pset);
125  else if (threshAlgName == "CFD" )
126  fThreshAlg = new pmtana::AlgoCFD(hit_alg_pset);
127  else throw art::Exception(art::errors::UnimplementedFeature)
128  << "Cannot find implementation for "
129  << threshAlgName << " algorithm.\n";
130 
131  auto const ped_alg_pset = pset.get< fhicl::ParameterSet >("PedAlgoPset");
132  std::string pedAlgName = ped_alg_pset.get< std::string >("Name");
133  if (pedAlgName == "Edges")
134  fPedAlg = new pmtana::PedAlgoEdges(ped_alg_pset);
135  else if (pedAlgName == "RollingMean")
136  fPedAlg = new pmtana::PedAlgoRollingMean(ped_alg_pset);
137  else if (pedAlgName == "UB" )
138  fPedAlg = new pmtana::PedAlgoUB(ped_alg_pset);
139  else throw art::Exception(art::errors::UnimplementedFeature)
140  << "Cannot find implementation for "
141  << pedAlgName << " algorithm.\n";
142 
143  fPulseRecoMgr.AddRecoAlgo(fThreshAlg);
144  fPulseRecoMgr.SetDefaultPedAlgo(fPedAlg);
145 
146  _output_filename = pset.get<std::string>("OutputFile","out.root");
147 
148  }
149 
151  {
152  // analyzie tuple prep
153  _ofile = TFile::Open(_output_filename.c_str(),"RECREATE");
154 
155  _wftree = new TTree("wftree","wftree");
156  _wftree->Branch("run",&_run,"run/I");
157  _wftree->Branch("event",&_event,"event/I");
158  _wftree->Branch("ch",&_ch,"ch/I");
159  _wftree->Branch("wf",&_wf);
160  _wftree->Branch("ped_mean",&_ped_mean_v);
161  _wftree->Branch("ped_sigma",&_ped_sigma_v);
162  _wftree->Branch("tstart",&_tstart,"tstart/D");
163 
164  _hittree = new TTree("hittree","hittree");
165  _hittree->Branch("run",&_run,"run/I");
166  _hittree->Branch("event",&_event,"event/I");
167  _hittree->Branch("ch",&_ch,"ch/I");
168  _hittree->Branch("tstart",&_tstart_v);
169  _hittree->Branch("tmax",&_tmax_v);
170  _hittree->Branch("tend",&_tend_v);
171  _hittree->Branch("tcross",&_tcross_v);
172  _hittree->Branch("amp",&_amp_v);
173  _hittree->Branch("area",&_area_v);
174  _hittree->Branch("ped_mean",&_ped_mean_v);
175  _hittree->Branch("ped_sigma",&_ped_sigma_v);
176 
177  _geotree = new TTree("geotree","geotree");
178  std::vector<double> pmtX, pmtY, pmtZ;
179  std::vector<double> minX, minY, minZ;
180  std::vector<double> maxX, maxY, maxZ;
181  auto const geop = lar::providerFrom<geo::Geometry>();
182  double PMTxyz[3];
183  for(size_t opch=0; opch<geop->NOpChannels(); ++opch) {
184  geop->OpDetGeoFromOpChannel(opch).GetCenter(PMTxyz);
185  pmtX.push_back(PMTxyz[0]);
186  pmtY.push_back(PMTxyz[1]);
187  pmtZ.push_back(PMTxyz[2]);
188  }
189  for(auto iter=geop->begin_TPC(); iter!=geop->end_TPC(); ++iter) {
190  auto const& tpc = (*iter);
191  minX.push_back(tpc.BoundingBox().MinX());
192  minY.push_back(tpc.BoundingBox().MinY());
193  minZ.push_back(tpc.BoundingBox().MinZ());
194  maxX.push_back(tpc.BoundingBox().MaxX());
195  maxY.push_back(tpc.BoundingBox().MaxY());
196  maxZ.push_back(tpc.BoundingBox().MaxZ());
197  }
198  _geotree->Branch("pmtX",&pmtX);
199  _geotree->Branch("pmtY",&pmtY);
200  _geotree->Branch("pmtZ",&pmtZ);
201  _geotree->Branch("minX",&minX);
202  _geotree->Branch("minY",&minY);
203  _geotree->Branch("minZ",&minZ);
204  _geotree->Branch("maxX",&maxX);
205  _geotree->Branch("maxY",&maxY);
206  _geotree->Branch("maxZ",&maxZ);
207  _geotree->Fill();
208  }
209 
211  {
212  _ofile->cd();
213  _wftree->Write();
214  _hittree->Write();
215  _geotree->Write();
216  _ofile->Close();
217  }
218 
219  //----------------------------------------------------------------------------
220  // Destructor
222  {
223 
224  delete fThreshAlg;
225  delete fPedAlg;
226 
227  }
228 
229  //----------------------------------------------------------------------------
230  void FullOpHitFinder::produce(art::Event& evt)
231  {
232 
233  _event = evt.id().event();
234  _run = evt.id().run();
235 
236  // These is the storage pointer we will put in the event
237  std::unique_ptr< std::vector< recob::OpHit > >
238  HitPtr(new std::vector< recob::OpHit >);
239 
240  //
241  // Get the pulses from the event
242  //
243 
244  if(fInputLabels.empty()) fInputLabels.push_back("");
245  for (auto label : fInputLabels) {
246 
247  art::Handle< std::vector< raw::OpDetWaveform > > wfHandle;
248  evt.getByLabel(fInputModule, label, wfHandle);
249  if (!wfHandle.isValid()) continue; // Skip non-existent collections
250 
251  for(auto const& waveform : (*wfHandle)) {
252 
253  _ch = static_cast< int >(waveform.ChannelNumber());
254  _tstart = waveform.TimeStamp();
255 
256  _wf.clear();
257  _wf.resize(waveform.size());
258  for(size_t idx=0; idx<_wf.size(); ++idx)
259  _wf[idx] = waveform[idx];
260 
261  fPulseRecoMgr.Reconstruct(waveform);
262 
263  // Record waveforms
264  _ped_mean_v = fPedAlg->Mean();
266  _wftree->Fill();
267 
268  // Record pulses
269  auto const& pulses = fThreshAlg->GetPulses();
270  size_t npulse = pulses.size();
271  _tstart_v.resize(npulse); _tmax_v.resize(npulse); _tend_v.resize(npulse); _tcross_v.resize(npulse);
272  _amp_v.resize(npulse); _area_v.resize(npulse);
273  _ped_mean_v.resize(npulse); _ped_sigma_v.resize(npulse);
274 
275  for(size_t idx=0; idx<npulse; ++idx) {
276  auto const& pulse = pulses[idx];
277  _tstart_v[idx] = pulse.t_start;
278  _tmax_v[idx] = pulse.t_max;
279  _tend_v[idx] = pulse.t_end;
280  _tcross_v[idx] = pulse.t_cfdcross;
281  _amp_v[idx] = pulse.peak;
282  _area_v[idx] = pulse.area;
283  _ped_mean_v[idx] = pulse.ped_mean;
284  _ped_sigma_v[idx] = pulse.ped_sigma;
285  }
286  _hittree->Fill();
287  }
288  }
289  }
290 
291 } // namespace opdet
std::vector< double > _tcross_v
std::vector< double > _ped_mean_v
double Mean(size_t i) const
Getter of the pedestal mean value.
Utilities related to art service access.
std::vector< double > _amp_v
std::vector< double > _tend_v
std::vector< double > _area_v
std::vector< double > _tmax_v
std::vector< std::string > fInputLabels
pmtana::PMTPedestalBase * fPedAlg
pmtana::PMTPulseRecoBase * fThreshAlg
std::map< int, int > GetChannelMap()
const pulse_param_array & GetPulses() const
A getter for the whole array of pulse_param struct object.
Class definition file of PedAlgoRollingMean.
double Sigma(size_t i) const
Getter of the pedestal standard deviation.
Class definition file of AlgoCFD.
bool Reconstruct(const pmtana::Waveform_t &) const
Implementation of ana_base::analyze method.
std::vector< double > GetSPEShifts()
Class definition file of PedAlgoUB.
std::vector< double > _tstart_v
Class definition file of AlgoFixedWindow.
std::vector< double > _wf
Class definition file of AlgoSlidingWindow.
Class definition file of AlgoThreshold.
std::set< unsigned int > fChannelMasks
std::vector< double > _ped_sigma_v
Class definition file of PMTPulseRecoBase.
Class definition file of PedAlgoEdges.
TCEvent evt
Definition: DataStructs.cxx:8
pmtana::PulseRecoManager fPulseRecoMgr
std::vector< double > GetSPEScales()
FullOpHitFinder(const fhicl::ParameterSet &)
art framework interface to geometry description
Class definition file of PulseRecoManager.