All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
wvfAna_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: wvfAna
3 // Module Type: analyzer
4 // File: wvfAna_module.cc
5 //
6 // Analyzer to read optical waveforms
7 //
8 // Authors: L. Paulucci and F. Marinho
9 ////////////////////////////////////////////////////////////////////////
10 
11 #include <algorithm>
12 #include <vector>
13 #include <cmath>
14 #include <memory>
15 #include <string>
16 
17 #include "art/Framework/Core/EDAnalyzer.h"
18 #include "art/Framework/Core/ModuleMacros.h"
19 #include "art/Framework/Principal/Event.h"
20 #include "art/Framework/Principal/Handle.h"
21 #include "art/Framework/Principal/Run.h"
22 #include "art/Framework/Principal/SubRun.h"
23 #include "art_root_io/TFileService.h"
24 #include "canvas/Utilities/InputTag.h"
25 #include "fhiclcpp/ParameterSet.h"
26 #include "messagefacility/MessageLogger/MessageLogger.h"
27 
28 #include "canvas/Utilities/Exception.h"
29 #include "art/Framework/Services/Registry/ServiceHandle.h"
31 
41 
42 #include "TH1D.h"
43 #include "TFile.h"
44 #include "TTree.h"
45 
47 
48 namespace opdet {
49 
50  class wvfAna;
51 
52  class wvfAna : public art::EDAnalyzer {
53  public:
54  explicit wvfAna(fhicl::ParameterSet const & p);
55  // The destructor generated by the compiler is fine for classes
56  // without bare pointers or other resource use.
57 
58  // Plugins should not be copied or assigned.
59  wvfAna(wvfAna const &) = delete;
60  wvfAna(wvfAna &&) = delete;
61  wvfAna & operator = (wvfAna const &) = delete;
62  wvfAna & operator = (wvfAna &&) = delete;
63 
64  // Required functions.
65  void analyze(art::Event const & e) override;
66 
67  //Selected optional functions
68  void beginJob() override;
69  void endJob() override;
70 
71  opdet::sbndPDMapAlg pdMap; //map for photon detector types
72 
73  private:
74 
75  size_t fEvNumber;
76  size_t fChNumber;
77  double fSampling;
79  double fStartTime;
80  double fEndTime;
81  //TTree *fWaveformTree;
82 
83  // Declare member data here.
84  std::string fInputModuleName;
85  std::vector<std::string> fOpDetsToPlot;
86  std::stringstream histname;
87  std::string opdetType;
88  std::string opdetElectronics;
89  };
90 
91 
92  wvfAna::wvfAna(fhicl::ParameterSet const & p)
93  :
94  EDAnalyzer(p) // ,
95  // More initializers here.
96  {
97  fInputModuleName = p.get< std::string >("InputModule" );
98  fOpDetsToPlot = p.get<std::vector<std::string> >("OpDetsToPlot");
99 
100  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataForJob();
101  fSampling = clockData.OpticalClock().Frequency(); // MHz
102  fSampling_Daphne = p.get<double>("DaphneFrequency" );
103  }
104 
106  {
107 
108  }
109 
110  void wvfAna::analyze(art::Event const & e)
111  {
112  // Implementation of required member function here.
113  std::cout << "My module on event #" << e.id().event() << std::endl;
114 
115  art::ServiceHandle<art::TFileService> tfs;
116  fEvNumber = e.id().event();
117 
118  art::Handle< std::vector< raw::OpDetWaveform > > waveHandle;
119  e.getByLabel(fInputModuleName, waveHandle);
120 
121  if(!waveHandle.isValid()) {
122  std::cout << Form("Did not find any G4 photons from a producer: %s", "largeant") << std::endl;
123  }
124 
125  // // example of usage for pdMap.getCollectionWithProperty()
126  // //
127  // // define a container
128  // auto inBoxTwo = pdMap.getCollectionWithProperty("pds_box", 2);
129  // // you can cout the whole json object
130  // std::cout << "inBoxTwo:\t" << inBoxTwo << "\n";
131  // // traverse its components in a loop
132  // for (auto const &e: inBoxTwo) {
133  // std::cout << e["pd_type"] << " " << e["channel"] << ' ' << "\n";
134  // }
135 
136  // // example of usage for pdMap.getCollectionFromCondition()
137  // // define a lambda function with the conditions
138  // auto subsetCondition = [](auto const& e)->bool
139  // // modify conditions as you want in the curly braces below
140  // {return e["pd_type"] == "pmt_uncoated" && e["tpc"] == 0;};
141  // // get the container that satisfies the conditions
142  // auto uncoatedsInTPC0 = pdMap.getCollectionFromCondition(subsetCondition);
143  // std::cout << "uncoatedsInTPC0.size():\t" << uncoatedsInTPC0.size() << "\n";
144  // for(auto const& e:uncoatedsInTPC0){
145  // std::cout << "e:\t" << e << "\n";
146  // }
147 
148  std::cout << "Number of waveforms: " << waveHandle->size() << std::endl;
149 
150  std::cout << "fOpDetsToPlot:\t";
151  for (auto const& opdet : fOpDetsToPlot){std::cout << opdet << " ";}
152  std::cout << std::endl;
153 
154  int hist_id = 0;
155  for(auto const& wvf : (*waveHandle)) {
156  fChNumber = wvf.ChannelNumber();
159  if (std::find(fOpDetsToPlot.begin(), fOpDetsToPlot.end(), opdetType) == fOpDetsToPlot.end()) {continue;}
160  histname.str(std::string());
161  histname << "event_" << fEvNumber
162  << "_opchannel_" << fChNumber
163  << "_" << opdetType
164  << "_" << hist_id;
165 
166  fStartTime = wvf.TimeStamp(); //in us
167  if (opdetElectronics == "daphne"){
168  fEndTime = double(wvf.size()) / fSampling_Daphne + fStartTime;
169  } //in us
170  else{
171  fEndTime = double(wvf.size()) / fSampling + fStartTime;
172  } //in us
173 
174  //Create a new histogram
175  TH1D *wvfHist = tfs->make< TH1D >(histname.str().c_str(), TString::Format(";t - %f (#mus);", fStartTime), wvf.size(), fStartTime, fEndTime);
176  for(unsigned int i = 0; i < wvf.size(); i++) {
177  wvfHist->SetBinContent(i + 1, (double)wvf[i]);
178  }
179  hist_id++;
180  }
181  }
182 
184  {
185  }
186 
187  DEFINE_ART_MODULE(opdet::wvfAna)
188 
189 }
std::string fInputModuleName
wvfAna & operator=(wvfAna const &)=delete
void beginJob() override
pdgs p
Definition: selectors.fcl:22
wvfAna(fhicl::ParameterSet const &p)
std::string opdetElectronics
std::stringstream histname
void endJob() override
std::vector< std::string > fOpDetsToPlot
Simulation objects for optical detectors.
std::string opdetType
Service to provide microboone-specific signal shaping for simulation (convolution) and reconstruction...
opdet::sbndPDMapAlg pdMap
std::string electronicsType(size_t ch) const
double fSampling_Daphne
object containing MC truth information necessary for making RawDigits and doing back tracking ...
do i e
void analyze(art::Event const &e) override
art::ServiceHandle< art::TFileService > tfs
std::string pdType(size_t ch) const override
Tools and modules for checking out the basics of the Monte Carlo.
art framework interface to geometry description
BEGIN_PROLOG could also be cout