All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OpDetAnalyzer_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: OpDetAnalyzer
3 // Plugin Type: analyzer (Unknown Unknown)
4 // File: OpDetAnalyzer_module.cc
5 //
6 // Generated at Tue Oct 19 04:32:01 2021 by Patrick Green using cetskelgen
7 // from version .
8 ////////////////////////////////////////////////////////////////////////
9 
10 #include "art/Framework/Core/EDAnalyzer.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 "art/Utilities/make_tool.h"
17 
18 #include "canvas/Utilities/InputTag.h"
19 #include "fhiclcpp/ParameterSet.h"
20 #include "messagefacility/MessageLogger/MessageLogger.h"
21 
22 #include "art_root_io/TFileService.h"
23 #include <TTree.h>
24 
26 
29 
30 #include "sbncode/OpDet/PDMapAlg.h"
31 
32 #include <iostream>
33 
34 namespace opdet {
35  class OpDetAnalyzer;
36 }
37 
38 
39 class opdet::OpDetAnalyzer : public art::EDAnalyzer {
40 public:
41  explicit OpDetAnalyzer(fhicl::ParameterSet const& p);
42  // The compiler-generated destructor is fine for non-base
43  // classes without bare pointers or other resource use.
44 
45  // Plugins should not be copied or assigned.
46  OpDetAnalyzer(OpDetAnalyzer const&) = delete;
47  OpDetAnalyzer(OpDetAnalyzer&&) = delete;
48  OpDetAnalyzer& operator=(OpDetAnalyzer const&) = delete;
50 
51  // Required functions.
52  void analyze(art::Event const& e) override;
53 
54  // Selected optional functions.
55  void beginJob() override;
56  void endJob() override;
57 
58 private:
59 
60  std::vector<std::string> fInputModule; // Input tag for OpDet collection
61 
62  // PDS map
63  std::unique_ptr<opdet::PDMapAlg> fPDSMapPtr;
64  std::vector<std::string> fPMTMapLabel, fArapucaMapLabel;
65 
66  std::vector<int> fNOpDetAll;
67  std::vector<int> fNOpDetDirect;
68  std::vector<int> fNOpDetReflected;
69 
71  TTree *fTheOpDetTree;
72  TTree *fTheEventTree;
73 
74  int fEventID;
76  float fWavelength;
77  float fTime;
78 
83  bool fisPMT;
84 
88 
89  int fVerbosity; // debugging
90 
91  /// Value used when a typical ultraviolet light wavelength is needed.
92  static constexpr double kVUVWavelength = 128.0; // nm
93 
94  /// Value used when a typical visible light wavelength is needed.
95  static constexpr double kVisibleWavelength = 450.0; // nm
96 
97 };
98 
99 
100 opdet::OpDetAnalyzer::OpDetAnalyzer(fhicl::ParameterSet const& pset)
101  : EDAnalyzer{pset},
102  fPDSMapPtr{art::make_tool<opdet::PDMapAlg>(pset.get<fhicl::ParameterSet>("PDSMapTool"))}
103 {
104 
105  fVerbosity = pset.get<int>("Verbosity");
106 
107  try
108  {
109  fInputModule = pset.get<std::vector<std::string>>("InputModule");
110  }
111  catch(...)
112  {
113  fInputModule.push_back(pset.get<std::string>("InputModule"));
114  }
115 }
116 
118 {
119  // create output trees
120  art::ServiceHandle<art::TFileService> tfs;
121 
122  // all photons tree
123  fAllPhotonsTree = tfs->make<TTree>("AllPhotons", "AllPhotons");
124  fAllPhotonsTree->Branch("EventID", &fEventID, "EventID/I");
125  fAllPhotonsTree->Branch("Wavelength", &fWavelength, "Wavelength/F");
126  fAllPhotonsTree->Branch("OpChannel", &fOpChannel, "OpChannel/I");
127  fAllPhotonsTree->Branch("Time", &fTime, "Time/F");
128 
129  // photons per opdet tree
130  fTheOpDetTree = tfs->make<TTree>("PhotonsPerOpDet","PhotonsPerOpDet");
131  fTheOpDetTree->Branch("EventID", &fEventID, "EventID/I");
132  fTheOpDetTree->Branch("OpChannel", &fOpChannel, "OpChannel/I");
133  fTheOpDetTree->Branch("OpDetX", &fOpDetX, "OpDetX/F");
134  fTheOpDetTree->Branch("OpDetY", &fOpDetY, "OpDetY/F");
135  fTheOpDetTree->Branch("OpDetZ", &fOpDetZ, "OpDetZ/F");
136  fTheOpDetTree->Branch("isPMT", &fisPMT);
137  fTheOpDetTree->Branch("CountAll", &fCountOpDetAll, "CountAll/I");
138  fTheOpDetTree->Branch("CountDirect", &fCountOpDetDirect, "CountDirect/I");
139  fTheOpDetTree->Branch("CountReflected", &fCountOpDetReflected, "CountReflected/I");
140 
141  // photons per event tree
142  fTheEventTree = tfs->make<TTree>("PhotonsPerEvent","PhotonsPerEvent");
143  fTheEventTree->Branch("EventID", &fEventID, "EventID/I");
144  fTheEventTree->Branch("CountAll", &fCountEventAll, "CountAll/I");
145  fTheEventTree->Branch("CountDirect", &fCountEventDirect, "CountDirect/I");
146  fTheEventTree->Branch("CountReflected", &fCountEventReflected, "CountReflected/I");
147 
148 }
149 
150 void opdet::OpDetAnalyzer::analyze(art::Event const& evt)
151 {
152 
153  // geometry information
154  art::ServiceHandle<geo::Geometry> geo;
155 
156  // get event number
157  fEventID = evt.id().event();
158 
159  // adapted from standard SimPhotonCounter code
160 
161  // Get *ALL* SimPhotonsCollection from Event
162  auto photon_handles = evt.getMany<std::vector<sim::SimPhotonsLite>>();
163  if (photon_handles.size() == 0)
164  throw art::Exception(art::errors::ProductNotFound)<<"sim SimPhotons retrieved and you requested them.";
165 
166  // reset counters
167  // event
168  fCountEventAll=0;
169  fCountEventDirect=0;
170  fCountEventReflected=0;
171  // opdet
172  fCountOpDetAll=0;
173  fCountOpDetDirect=0;
174  fCountOpDetReflected=0;
175 
176  // reset vectors
177  fNOpDetAll.clear(); fNOpDetAll.resize(geo->NOpChannels(), 0);
178  fNOpDetDirect.clear(); fNOpDetDirect.resize(geo->NOpChannels(), 0);
179  fNOpDetReflected.clear(); fNOpDetReflected.resize(geo->NOpChannels(), 0);
180 
181  // Get SimPhotonsLite from Event
182  for(auto const& mod : fInputModule){
183 
184  // Loop over direct/reflected photons
185  for (auto const& ph_handle: photon_handles) {
186  // Do some checking before we proceed
187  if (!ph_handle.isValid()) continue;
188  if (ph_handle.provenance()->moduleLabel() != mod) continue; //not the most efficient way of doing this, but preserves the logic of the module. Andrzej
189 
190  bool Reflected = (ph_handle.provenance()->productInstanceName() == "Reflected");
191 
192  if(fVerbosity > 0) std::cout<<"Found OpDet hit collection of size "<< (*ph_handle).size()<<std::endl;
193 
194  if((*ph_handle).size()>0)
195  {
196 
197  for ( auto const& photon : (*ph_handle) )
198  {
199  //Get data from HitCollection entry
200  fOpChannel=photon.OpChannel;
201  std::map<int, int> PhotonsMap = photon.DetectedPhotons;
202 
203  // do not save if PD is not sensitive to this light
204  std::string pd_type=fPDSMapPtr->pdType(fOpChannel);
205  if (Reflected && pd_type=="xarapuca_vuv") continue;
206  if(!Reflected && (pd_type=="xarapuca_vis" || pd_type=="pmt_uncoated")) continue;
207 
208  for(auto it = PhotonsMap.begin(); it!= PhotonsMap.end(); it++)
209  {
210 
211  // Calculate wavelength in nm
212  if (Reflected) fWavelength = kVisibleWavelength;
213  else fWavelength= kVUVWavelength;
214 
215  // Get arrival time from phot
216  fTime= it->first;
217 
218  for(int i = 0; i < it->second ; i++)
219  {
220  // Increment per OpDet counters and fill all photon tree
221  fCountOpDetAll++;
222  fAllPhotonsTree->Fill();
223 
224  // all
225  fNOpDetAll[fOpChannel]++;
226 
227  // direct light
228  if (!Reflected){
229  fNOpDetDirect[fOpChannel]++;
230  }
231  // reflected light
232  else if (Reflected) {
233  fNOpDetReflected[fOpChannel]++;
234  }
235  }
236  }
237  } // eng loop over photons
238  }
239  } // end loop over photon handles
240  } // end loop over input modules
241 
242 
243  // fill information in perOpDet and perEvent trees
244  for (unsigned int OpDet = 0; OpDet < geo->NOpChannels(); OpDet++) {
245 
246  // perOpDet tree
247  fOpChannel = OpDet;
248  geo::Point_t OpDetCenter = geo->OpDetGeoFromOpDet(OpDet).GetCenter();
249  fOpDetX = OpDetCenter.X();
250  fOpDetY = OpDetCenter.Y();
251  fOpDetZ = OpDetCenter.Z();
252  fisPMT = geo->OpDetGeoFromOpDet(OpDet).isSphere();
253  fCountOpDetAll = fNOpDetAll[OpDet];
254  fCountOpDetDirect = fNOpDetDirect[OpDet];
255  fCountOpDetReflected = fNOpDetReflected[OpDet];
256  fTheOpDetTree->Fill();
257 
258  // increment counters for perEvent tree
259  fCountEventAll+=fCountOpDetAll;
260  fCountEventDirect+=fCountOpDetDirect;
261  fCountEventReflected+=fCountOpDetReflected;
262 
263  if(fVerbosity >2) std::cout<<"OpDetResponseInterface PerOpDet : Event "<<fEventID<<" OpDet " << fOpChannel << " All " << fCountOpDetAll << " Dir " <<fCountOpDetDirect << ", Refl " << fCountOpDetReflected<<std::endl;
264  }
265 
266  // perEvent tree
267  fTheEventTree->Fill();
268 
269  if(fVerbosity >1) std::cout<<"OpDetResponseInterface PerEvent : Event "<<fEventID<<" All " << fCountEventAll << " Dir " <<fCountEventDirect << ", Refl " << fCountEventReflected <<std::endl;
270 
271 }
272 
274 {
275  // Implementation of optional member function here.
276 }
277 
278 
279 DEFINE_ART_MODULE(opdet::OpDetAnalyzer)
process_name can override from command line with o or output photon
Definition: runPID.fcl:28
void analyze(art::Event const &e) override
OpDetAnalyzer(fhicl::ParameterSet const &p)
pdgs p
Definition: selectors.fcl:22
std::unique_ptr< opdet::PDMapAlg > fPDSMapPtr
std::vector< int > fNOpDetAll
std::vector< int > fNOpDetReflected
static constexpr double kVUVWavelength
Value used when a typical ultraviolet light wavelength is needed.
static constexpr double kVisibleWavelength
Value used when a typical visible light wavelength is needed.
Simulation objects for optical detectors.
std::vector< int > fNOpDetDirect
This is the interface class for a tool to handle PD mapping in SBN detectors, used for flash matching...
OpDetAnalyzer & operator=(OpDetAnalyzer const &)=delete
std::vector< std::string > fArapucaMapLabel
std::vector< std::string > fPMTMapLabel
do i e
art::ServiceHandle< art::TFileService > tfs
BEGIN_PROLOG OpDetAnalyzer
TCEvent evt
Definition: DataStructs.cxx:8
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
Definition: geo_vectors.h:184
Tools and modules for checking out the basics of the Monte Carlo.
std::vector< std::string > fInputModule
art framework interface to geometry description
BEGIN_PROLOG could also be cout