All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MeVPrtlTestRayTrace_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: MeVPrtlTestRayTrace
3 // Plugin Type: analyzer (art v3_02_06)
4 // File: MeVPrtlTestRayTrace_module.cc
5 //
6 // Generated at Wed Feb 19 17:38:21 2020 by Gray Putnam using cetskelgen
7 // from cetlib version v3_07_02.
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 #include "art_root_io/TFileService.h"
18 
19 #include "canvas/Utilities/InputTag.h"
20 #include "fhiclcpp/ParameterSet.h"
21 #include "messagefacility/MessageLogger/MessageLogger.h"
23 
24 #include "nurandom/RandomUtils/NuRandomService.h"
25 #include "CLHEP/Random/JamesRandom.h"
26 #include "CLHEP/Random/RandFlat.h"
27 
28 #include "nusimdata/SimulationBase/MCTruth.h"
29 #include "nusimdata/SimulationBase/MCFlux.h"
30 #include "nusimdata/SimulationBase/GTruth.h"
35 
40 
41 #include "Tools/IMesonGen.h"
42 #include "Tools/IMeVPrtlFlux.h"
43 #include "Tools/IRayTrace.h"
44 #include "Tools/IMeVPrtlDecay.h"
45 
46 #include "Tools/Constants.h"
47 
48 #include "TTree.h"
49 
50 #include <memory>
51 #include <chrono>
52 
53 namespace evgen {
54  namespace ldm {
55  class MeVPrtlTestRayTrace;
56  }
57 }
58 
59 
60 class evgen::ldm::MeVPrtlTestRayTrace : public art::EDAnalyzer {
61 public:
62  explicit MeVPrtlTestRayTrace(fhicl::ParameterSet const& p);
63  // The compiler-generated destructor is fine for non-base
64  // classes without bare pointers or other resource use.
65 
66  // Plugins should not be copied or assigned.
71 
72  // Required functions.
73  void analyze(const art::Event& e) override;
74 
75  ~MeVPrtlTestRayTrace() noexcept {}
76 
77 private:
78  std::unique_ptr<evgen::ldm::IMesonGen> fGenTool;
79  std::unique_ptr<evgen::ldm::IMeVPrtlFlux> fFluxTool;
80  std::vector<std::unique_ptr<evgen::ldm::IRayTrace>> fRayTools;
81 
82  unsigned fNCall;
83  unsigned fNEvt;
84 
85  TTree *fTree;
86  std::vector<double> fBranchWeights;
88 };
89 
91  : EDAnalyzer{p}
92 {
93  // bring in the tools
94  fGenTool = art::make_tool<IMesonGen>(p.get<fhicl::ParameterSet>("MesonGen"));
95  fFluxTool = art::make_tool<IMeVPrtlFlux>(p.get<fhicl::ParameterSet>("Flux"));
96 
97  for (auto const &rayconfig: p.get<std::vector<fhicl::ParameterSet>>("RayTraces")) {
98  fRayTools.push_back(art::make_tool<IRayTrace>(rayconfig));
99  fBranchWeights.push_back(0);
100  }
101 
102  fNCall = p.get<unsigned>("NCall", 10000);
103  fNEvt = 0;
104 
105  art::ServiceHandle<art::TFileService> tfs;
106  fTree = tfs->make<TTree>("testraytrace", "testraytrace");
107  fMeVPrtl = new evgen::ldm::MeVPrtlTruth();
108  fTree->Branch("mevprtl", &fMeVPrtl);
109  for (unsigned iray = 0; iray < fRayTools.size(); iray++) {
110  fTree->Branch((std::string(fRayTools[iray]->Name()) + "_wgt").c_str(), &fBranchWeights[iray]);
111  }
112 
113 }
114 
116 {
117  // get the next MeVPrtl Truth
118  while (1) {
119  simb::MCFlux kaon = fGenTool->GetNext();
120 
121  evgen::ldm::KaonParent kaonp(kaon);
122  bool is_kaon = kaonp.kaon_pdg != 0;
123 
124  // (void) is_kaon;
125  if (is_kaon) {
126  std::cout << "Flux is kaon (" << is_kaon << "). Weight: " << kaonp.weight << ". Produced with energy: " << kaonp.mom.E()
127  << " M=" << kaonp.mom.M() << " P=(" << kaonp.mom.Px() << ", " << kaonp.mom.Py() << ", " << kaonp.mom.Pz() << ") At: ("
128  << kaonp.pos.X() << ", " << kaonp.pos.Y() << ", " << kaonp.pos.Z() << ")" << std::endl;
129  }
130 
131  bool success;
132 
134  double flux_weight;
135 
136  success = fFluxTool->MakeFlux(kaon, flux, flux_weight);
137  if (!success) continue;
138 
139  std::cout << "New flux. E=" << flux.mom.E() << " At: (" << flux.pos.X() << ", " << flux.pos.Y() << ", " << flux.pos.Z() << ")" << std::endl;
140  std::cout << "P=(" << flux.mom.Px() << ", " << flux.mom.Py() << ", " << flux.mom.Pz() << ")" << std::endl;
141  std::cout << "Flux weight: " << flux_weight << std::endl;
142 
143 
144  // See if an intersection is possible
145  double costh_crit = minKinematicCosTheta(flux.kmom.M(), flux.sec.M(), flux.mom.M(), flux.kmom.E());
146  TVector3 det(0., 0., 0.); // detector should be near origin
147  double costh = flux.kmom.Vect().Unit().Dot((det - flux.pos.Vect()).Unit());
148  std::cout << "COSTH CRIT: " << costh_crit << " DETECTOR COSTH: " << costh << std::endl;
149  if (costh < costh_crit) continue;
150 
151  std::cout << "CALLING RAY TOOLS\n";
152  fNEvt ++;
153  unsigned iray = 0;
154  std::vector<double> weightsum(fRayTools.size(), 0.);
155  std::vector<double> time(fRayTools.size(), 0.);
156  for (auto const &r: fRayTools) {
157  std::array<TVector3, 2> intersection;
158  double ray_weight;
159  std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
160 
161  for (unsigned icall = 0; icall < fNCall; icall++) {
162  success = r->IntersectDetector(flux, intersection, ray_weight);
163  if (!success) ray_weight = 0.;
164  weightsum[iray] += ray_weight;
165  }
166 
167  std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
168  std::chrono::duration<double, std::milli> duration = t2 - t1;
169  time[iray] = duration.count();
170 
171  iray ++;
172  }
173 
174  std::cout << "DONE CALLING RAY TOOLS\n";
175  for (unsigned iray = 0; iray < fRayTools.size(); iray++) {
176  std::cout << fRayTools[iray]->Name() << " " << (time[iray] / fNCall) << " " << (weightsum[iray] / fNCall) << std::endl;
177  }
178 
180  std::array<TVector3, 2> intersection;
181  *fMeVPrtl = evgen::ldm::MeVPrtlTruth(flux, decay, intersection, flux_weight, 1., 1., 0.);
182  for (unsigned iray = 0; iray < fRayTools.size(); iray++) {
183  fBranchWeights[iray] = weightsum[iray] / fNCall;
184  }
185 
186  fTree->Fill();
187 
188  break;
189  }
190 }
191 
192 DEFINE_ART_MODULE(evgen::ldm::MeVPrtlTestRayTrace)
MeVPrtlTestRayTrace(fhicl::ParameterSet const &p)
void analyze(const art::Event &e) override
TLorentzVector mom
Definition: MeVPrtlFlux.h:14
pdgs p
Definition: selectors.fcl:22
std::vector< std::unique_ptr< evgen::ldm::IRayTrace > > fRayTools
std::unique_ptr< evgen::ldm::IMesonGen > fGenTool
TLorentzVector kmom
Definition: MeVPrtlFlux.h:13
This is an interface for an art Tool which sources MCFlux objects for downstream processing and tabul...
TLorentzVector pos
Definition: KaonParent.h:12
MeVPrtlTestRayTrace & operator=(MeVPrtlTestRayTrace const &)=delete
BEGIN_PROLOG vertical distance to the surface Name
TLorentzVector mom
Definition: KaonParent.h:13
std::unique_ptr< evgen::ldm::IMeVPrtlFlux > fFluxTool
double minKinematicCosTheta(double parentM, double secM, double prtlM, double parentE)
Definition: Constants.cpp:195
TLorentzVector pos
Definition: MeVPrtlFlux.h:11
do i e
This is an interface for an art Tool which turns MCFlux objects (which is a meson decay to neutrinos)...
art::ServiceHandle< art::TFileService > tfs
TCEvent evt
Definition: DataStructs.cxx:8
This provides an interface for an art tool which ray traces &quot;Prtl&quot; (massive) particles from their pro...
esac echo uname r
art framework interface to geometry description
BEGIN_PROLOG could also be cout
This is an interface for an art Tool which decays &quot;Prtl&quot; inside a detector volume. It maps MeVPrtlFlux to MeVPrtlDecay.
TLorentzVector sec
Definition: MeVPrtlFlux.h:16