LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
PDFastSimANN_module.cc
Go to the documentation of this file.
1 // Class: PDFastSimANN
3 // Plugin Type: producer
4 // File: PDFastSimANN_module.cc
5 // Description:
6 // - acts on sim::SimEnergyDeposit from LArG4Main,
7 // - simulate the OpDet response to optical photons
8 // Input: 'sim::SimEnergyDeposit'
9 // Output: 'sim::OpDetBacktrackerRecord'
10 //Fast simulation of propagating the photons created from SimEnergyDeposits.
11 
12 //This module does a fast simulation of propagating the photons created from SimEnergyDeposits,
13 //This simulation is done using the graph trained by artificial neural network,
14 //which gives the visibilities of each optical channel with respect to scinitllation vertex in the TPC volume,
15 //to avoid propagating single photons using Geant4.
16 //At the end of this module a collection of the propagated photons either as
17 //'sim::OpDetBacktrackerRecord' are placed into the art event.
18 
19 //The steps this module takes are:
20 // - to take number of photon and the vertex information from 'sim::SimEnergyDeposits',
21 // - use the visibilities to determine the amount of visible photons at each optical channel,
22 // - visible photons: the number of photons times the visibility at the middle of the Geant4 step for a given optical channel.
23 // - other photon information is got from 'sim::SimEnergyDeposits'
24 // - add 'sim::OpDetBacktrackerRecord' to event
25 // Aug. 20, 2020 by Mu Wei
27 
28 // Art libraries
37 #include "fhiclcpp/ParameterSet.h"
39 
40 // LArSoft libraries
55 
56 // Random number engine
57 #include "CLHEP/Random/RandFlat.h"
58 #include "CLHEP/Random/RandPoissonQ.h"
60 
61 namespace phot {
62  class PDFastSimANN : public art::EDProducer {
63  public:
64  explicit PDFastSimANN(fhicl::ParameterSet const&);
65  void beginJob() override;
66  void endJob() override;
67  void produce(art::Event&) override;
68 
69  private:
70  const bool fDoSlowComponent;
71  const bool fUseLitePhotons;
73  std::unique_ptr<ScintTime> fScintTime; //Tool to retrive timinig of scintillation
74  std::unique_ptr<TFLoader>
75  fTFGenerator; //Tool to predict the hit pattern based on TensorFlow network
76  CLHEP::HepRandomEngine& fPhotonEngine;
77  CLHEP::HepRandomEngine& fScintTimeEngine;
78  std::map<int, int> PDChannelToSOCMap; //Where each OpChan is.
79  int nOpChannels; //Number of optical detector
80 
81  void AddOpDetBTR(std::vector<sim::OpDetBacktrackerRecord>& opbtr,
82  std::map<int, int>& ChannelMap,
84  };
85 
86  //......................................................................
88  : art::EDProducer{pset}
89  , fDoSlowComponent(pset.get<bool>("DoSlowComponent", true))
91  , simTag{pset.get<art::InputTag>("SimulationLabel")}
92  , fScintTime{art::make_tool<ScintTime>(pset.get<fhicl::ParameterSet>("ScintTimeTool"))}
93  , fTFGenerator{art::make_tool<TFLoader>(pset.get<fhicl::ParameterSet>("TFLoaderTool"))}
95  createEngine(0, "HepJamesRandom", "photon"),
96  "HepJamesRandom",
97  "photon",
98  pset,
99  "SeedPhoton"))
101  createEngine(0, "HepJamesRandom", "scinttime"),
102  "HepJamesRandom",
103  "scinttime",
104  pset,
105  "SeedScintTime"))
106  {
107  std::cout << "PDFastSimANN Module Construct" << std::endl;
108 
109  if (fUseLitePhotons) {
110  std::cout << "Use Lite Photon." << std::endl;
111  produces<std::vector<sim::SimPhotonsLite>>();
112  produces<std::vector<sim::OpDetBacktrackerRecord>>();
113  }
114  else {
115  std::cout << "Use Sim Photon." << std::endl;
116  produces<std::vector<sim::SimPhotons>>();
117  }
118  }
119 
120  //......................................................................
122  {
123  std::cout << "PDFastSimANN beginJob." << std::endl;
124 
125  fTFGenerator->Initialization();
126 
128  nOpChannels = int(geo->Cryostat().NOpDet());
129  std::cout << "Number of optical detectors: " << nOpChannels << std::endl;
130 
131  return;
132  }
133 
134  //......................................................................
136  {
137  std::cout << "PDFastSimANN endJob." << std::endl;
138  fTFGenerator->CloseSession();
139 
140  return;
141  }
142 
143  //......................................................................
145  {
146  std::cout << "PDFastSimANN Module Producer..." << std::endl;
147 
148  CLHEP::RandPoissonQ randpoisphot{fPhotonEngine};
149 
150  std::unique_ptr<std::vector<sim::SimPhotons>> phot(new std::vector<sim::SimPhotons>);
151  std::unique_ptr<std::vector<sim::SimPhotonsLite>> phlit(new std::vector<sim::SimPhotonsLite>);
152  std::unique_ptr<std::vector<sim::OpDetBacktrackerRecord>> opbtr(
153  new std::vector<sim::OpDetBacktrackerRecord>);
154 
155  auto& photonCollection(*phot);
156  auto& photonLiteCollection(*phlit);
157 
158  photonCollection.resize(nOpChannels);
159  photonLiteCollection.resize(nOpChannels);
160 
161  for (int i = 0; i < nOpChannels; i++) {
162  photonCollection[i].fOpChannel = i;
163  photonLiteCollection[i].OpChannel = i;
164  }
165 
167  if (!event.getByLabel(simTag, edepHandle)) {
168  std::cout << "PDFastSimANN Module Cannot getByLabel: " << simTag << std::endl;
169  return;
170  }
171 
173  auto const& edeps = edepHandle;
174 
175  int num_points = 0;
176  float vis_scale = 1.0; // to scale the visibility fraction, for test only;
177  for (auto const& edepi : *edeps) {
178  num_points++;
179  int trackID = edepi.TrackID();
180  int nphot = edepi.NumPhotons();
181  double edeposit = edepi.Energy() / nphot;
182  double pos[3] = {edepi.MidPointX(), edepi.MidPointY(), edepi.MidPointZ()};
183 
184  int nphot_fast = edepi.NumFPhotons();
185  int nphot_slow = edepi.NumSPhotons();
186 
187  std::vector<double> pars;
188  pars.push_back(pos[0]);
189  pars.push_back(pos[1]);
190  pars.push_back(pos[2]);
191  fTFGenerator->Predict(pars);
192  std::vector<double> Visibilities = fTFGenerator->GetPrediction();
193  if (int(Visibilities.size()) != nOpChannels) {
194  std::cout << "PDFastSimANN get channels from graph " << Visibilities.size()
195  << " is not the same as from geometry: " << nOpChannels << std::endl;
196  break;
197  }
198 
199  for (int channel = 0; channel < int(Visibilities.size()); ++channel) {
200  auto visibleFraction =
201  Visibilities[channel] * vis_scale; // to scale the visibility fraction;
202 
203  if (visibleFraction == 0.0) {
204  continue; //vertex is not visible at this optical channel.
205  }
206 
207  if (fUseLitePhotons) {
208  sim::OpDetBacktrackerRecord tmpbtr(channel);
209  if (nphot_fast > 0) {
210  //random number, poisson distribution, mean: the amount of photons visible at this channel
211  auto n = static_cast<int>(randpoisphot.fire(nphot_fast * visibleFraction));
212  for (long i = 0; i < n; ++i) {
213  //calculates the time at which the photon was produced
214  fScintTime->GenScintTime(true, fScintTimeEngine);
215  auto time = static_cast<int>(edepi.StartT() + fScintTime->GetScintTime());
216  ++photonLiteCollection[channel].DetectedPhotons[time];
217  tmpbtr.AddScintillationPhotons(trackID, time, 1, pos, edeposit);
218  }
219  }
220 
221  if ((nphot_slow > 0) && fDoSlowComponent) {
222  auto n = static_cast<int>(randpoisphot.fire(nphot_slow * visibleFraction));
223  for (long i = 0; i < n; ++i) {
224  fScintTime->GenScintTime(false, fScintTimeEngine);
225  auto time = static_cast<int>(edepi.StartT() + fScintTime->GetScintTime());
226  ++photonLiteCollection[channel].DetectedPhotons[time];
227  tmpbtr.AddScintillationPhotons(trackID, time, 1, pos, edeposit);
228  }
229  }
230 
231  AddOpDetBTR(*opbtr, PDChannelToSOCMap, tmpbtr);
232  }
233  else {
234  sim::OnePhoton photon;
235  photon.SetInSD = false;
236  photon.InitialPosition = {edepi.MidPointX(), edepi.MidPointY(), edepi.MidPointZ()};
237  photon.Energy = 9.7e-6;
238 
239  if (nphot_fast > 0) {
240  //random number, poisson distribution, mean: the amount of photons visible at this channel
241  auto n = static_cast<int>(randpoisphot.fire(nphot_fast * visibleFraction));
242  if (n > 0) {
243  fScintTime->GenScintTime(true, fScintTimeEngine);
244  auto time = static_cast<int>(edepi.StartT() + fScintTime->GetScintTime());
245  photon.Time = time;
246  // add n copies of sim::OnePhoton photon to the photon collection for a given OpChannel
247  photonCollection[channel].insert(photonCollection[channel].end(), n, photon);
248  }
249  }
250  if ((nphot_slow > 0) && fDoSlowComponent) {
251  //random number, poisson distribution, mean: the amount of photons visible at this channel
252  auto n = static_cast<int>(randpoisphot.fire(nphot_slow * visibleFraction));
253  if (n > 0) {
254  fScintTime->GenScintTime(false, fScintTimeEngine);
255  auto time = static_cast<int>(edepi.StartT() + fScintTime->GetScintTime());
256  photon.Time = time;
257  // add n copies of sim::OnePhoton photon to the photon collection for a given OpChannel
258  photonCollection[channel].insert(photonCollection[channel].end(), n, photon);
259  }
260  }
261  }
262  }
263  }
264  std::cout << "PDFastSimANN module produced " << num_points << " images..." << std::endl;
265  PDChannelToSOCMap.clear();
266 
267  if (fUseLitePhotons) {
268  event.put(move(phlit));
269  event.put(move(opbtr));
270  }
271  else {
272  event.put(move(phot));
273  }
274 
275  return;
276  }
277 
278  //......................................................................
279  void PDFastSimANN::AddOpDetBTR(std::vector<sim::OpDetBacktrackerRecord>& opbtr,
280  std::map<int, int>& ChannelMap,
282  {
283  int iChan = btr.OpDetNum();
284  std::map<int, int>::iterator channelPosition = ChannelMap.find(iChan);
285 
286  if (channelPosition == ChannelMap.end()) {
287  ChannelMap[iChan] = opbtr.size();
288  opbtr.emplace_back(std::move(btr));
289  }
290  else {
291  unsigned int idtest = channelPosition->second;
292  auto const& timePDclockSDPsMap = btr.timePDclockSDPsMap();
293 
294  for (auto const& timePDclockSDP : timePDclockSDPsMap) {
295  for (auto const& sdp : timePDclockSDP.second) {
296  double xyz[3] = {sdp.x, sdp.y, sdp.z};
297  opbtr.at(idtest).AddScintillationPhotons(
298  sdp.trackID, timePDclockSDP.first, sdp.numPhotons, xyz, sdp.energy);
299  }
300  }
301  }
302 
303  return;
304  }
305 } // namespace
306 
intermediate_table::iterator iterator
base_engine_t & createEngine(seed_t seed)
Store parameters for running LArG4.
Utilities related to art service access.
Definition of util::enumerate().
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
All information of a photon entering the sensitive optical detector volume.
Definition: SimPhotons.h:60
CLHEP::HepRandomEngine & fPhotonEngine
CryostatGeo const & Cryostat(CryostatID const &cryoid=cryostat_zero) const
Returns the specified cryostat.
Energy deposited on a readout Optical Detector by simulated tracks.
geo::Point_t InitialPosition
Scintillation position in world coordinates [cm].
Definition: SimPhotons.h:63
std::unique_ptr< ScintTime > fScintTime
std::unique_ptr< TFLoader > fTFGenerator
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
PDFastSimANN(fhicl::ParameterSet const &)
Simulation objects for optical detectors.
int OpDetNum() const
Returns the readout Optical Detector this object describes.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
Definitions of geometry vector data types.
void produce(art::Event &) override
void beginJob() override
Test of util::counter and support utilities.
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
Provides a base class aware of world box coordinates.
void AddScintillationPhotons(TrackID_t trackID, timePDclock_t timePDclock, double numberPhotons, double const *xyz, double energy)
Add scintillation photons and energy to this OpticalDetector.
Encapsulate the geometry of an optical detector.
void AddOpDetBTR(std::vector< sim::OpDetBacktrackerRecord > &opbtr, std::map< int, int > &ChannelMap, sim::OpDetBacktrackerRecord btr)
General LArSoft Utilities.
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
bool SetInSD
Whether the photon reaches the sensitive detector.
Definition: SimPhotons.h:84
unsigned int NOpDet() const
Number of optical detectors in this TPC.
Definition: CryostatGeo.h:321
contains information for a single step in the detector simulation
Definition: MVAAlg.h:12
Char_t n[5]
float Energy
Scintillation photon energy [GeV].
Definition: SimPhotons.h:78
std::map< int, int > PDChannelToSOCMap
Namespace collecting geometry-related classes utilities.
CLHEP::HepRandomEngine & fScintTimeEngine
timePDclockSDPs_t const & timePDclockSDPsMap() const
Returns all the deposited energy information as stored.
art framework interface to geometry description
Event finding and building.