All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FluxReaderAna_module.cc
Go to the documentation of this file.
1 /**
2  * @file FluxReaderAna_module.cc
3  * @author Marco Del Tutto
4  * @date 3 Dec 2021
5  * @brief An analyzer module to make flux histograms
6  *
7  */
8 
9 ////////////////////////////////////////////////////////////////////////
10 // Class: FluxReaderAna
11 // Plugin Type: analyzer (Unknown Unknown)
12 // File: FluxReaderAna_module.cc
13 //
14 // Generated at Mon Oct 11 19:10:14 2021 by Marco Del Tutto using cetskelgen
15 // from version .
16 ////////////////////////////////////////////////////////////////////////
17 
18 #include "art/Framework/Core/EDAnalyzer.h"
19 #include "art/Framework/Core/ModuleMacros.h"
20 #include "art/Framework/Principal/Event.h"
21 #include "art/Framework/Principal/Handle.h"
22 #include "art/Framework/Principal/Run.h"
23 #include "art/Framework/Principal/SubRun.h"
24 #include "canvas/Utilities/InputTag.h"
25 #include "fhiclcpp/ParameterSet.h"
26 #include "messagefacility/MessageLogger/MessageLogger.h"
27 #include "art_root_io/TFileService.h"
28 
30 #include "TGeoManager.h"
31 #include "TVector3.h"
32 #include "TTree.h"
33 #include "TDatabasePDG.h"
34 
35 #include "nusimdata/SimulationBase/MCFlux.h"
36 #include "nusimdata/SimulationBase/MCTruth.h"
37 
38 class FluxReaderAna;
39 
40 
41 class FluxReaderAna : public art::EDAnalyzer {
42 public:
43  explicit FluxReaderAna(fhicl::ParameterSet const& p);
44  // The compiler-generated destructor is fine for non-base
45  // classes without bare pointers or other resource use.
46 
47  // Plugins should not be copied or assigned.
48  FluxReaderAna(FluxReaderAna const&) = delete;
49  FluxReaderAna(FluxReaderAna&&) = delete;
50  FluxReaderAna& operator=(FluxReaderAna const&) = delete;
52 
53  // Required functions.
54  void analyze(art::Event const& e) override;
55 
56 private:
57 
58  std::string _flux_label; ///< Label for flux dataproduct (to be set via fcl)
59  float _x_shift; // cm (to be set via fcl)
60  float _baseline; // cm (to be set via fcl)
61  float _nu_intersection_z; ///< Z position where to evaluate the flux (to be set via fcl)
62  float _nu_other_intersection_z; ///< Additional Z position where to evaluate the flux (to be set via fcl)
63 
64  bool _apply_position_cuts; // wheter or not to apply position cuts (to be set via fcl)
65  float _x_cut; // cm, only saves neutrinos with x in [-_xcut, +_xcut] (to be set via fcl)
66  float _y_cut; // cm, only saves neutrinos with y in [-_ycut, +_ycut] (to be set via fcl)
67 
68 
69  const TDatabasePDG *_pdg_database = TDatabasePDG::Instance();
70 
71  /// Returns the intersection point with the front face of the TPC
72  TVector3 GetIntersection(TVector3 nu_pos, TVector3 nu_dir, float z_location=0);
73 
74  TTree* _tree;
75  bool _nu_hit; /// True if the neutrino hit the requested volumes
76  int _nu_pdg; /// PDG of neutrino
77  float _nu_e; /// Energy of the neutrino
78  float _nu_t; /// Time of the neutrino
79  float _nu_w; /// Neutrino weight
80  float _nu_x; /// X poisition of neutrino at the front face of the TPC
81  float _nu_y; /// Y poisition of neutrino at the front face of the TPC
82  float _nu_z; /// Z poisition of neutrino at the front face of the TPC
83  float _nu_vx; /// X poisition of neutrino at neutrino production point
84  float _nu_vy; /// Y poisition of neutrino at neutrino production point
85  float _nu_vz; /// Z poisition of neutrino at neutrino production point
86  float _nu_px; /// X momentum of neutrino
87  float _nu_py; /// Y momentum of neutrino
88  float _nu_pz; /// Z momentum of neutrino
89  int _nu_decay; /// Neutrino parent decay code
90  float _nu_dk2gen; /// Distance from decay to ray origin
91  float _nu_p_angle; /// Angle between neutrino and parent direction
92  int _nu_p_type; /// Neutrino parent pdg
93  float _nu_p_dpx; /// Neutrino parent momentum x
94  float _nu_p_dpy; /// Neutrino parent momentum x
95  float _nu_p_dpz; /// Neutrino parent momentum x
96  float _nu_p_e; /// Neutrino parent energy
97  float _nu_r; /// Neutrino r
98  float _nu_oaa; /// Neutrino off axis angle
99 
100  float _nu_other_x; /// X poisition of neutrino at the front face of the TPC (other)
101  float _nu_other_y; /// Y poisition of neutrino at the front face of the TPC (other)
102  float _nu_other_z; /// Z poisition of neutrino at the front face of the TPC (other)
103  float _nu_other_t; /// Time of the neutrino (other)
104 };
105 
106 
107 FluxReaderAna::FluxReaderAna(fhicl::ParameterSet const& p)
108  : EDAnalyzer{p}
109 {
110 
111  _flux_label = p.get<std::string>("FluxLabel", "flux");
112 
113  _baseline = p.get<float>("Baseline"); // cm, distance from detector to target position
114  _x_shift = p.get<float>("XShift", 0.); // cm, detector shift along X w.r.t. beamline coordinate system
115 
116  _nu_intersection_z = p.get<float>("NuIntersectionZ", 0.);
117  _nu_other_intersection_z = p.get<float>("NuOtherIntersectionZ");
118  // 49000 is ICARUS location in SBND coordinate system (600 - 110)
119  // 36000 is MicroBooNE location in SBND coordinate system (470 - 110)
120 
121  _apply_position_cuts = p.get<bool>("ApplyPositionCuts", false);
122  _x_cut = p.get<float>("XCut"); // cm
123  _y_cut = p.get<float>("YCut"); // cm
124 
125 
126  art::ServiceHandle<art::TFileService> fs;
127  _tree = fs->make<TTree>("tree", "");
128  _tree->Branch("nu_hit", &_nu_hit, "nu_hit/O");
129  _tree->Branch("nu_pdg", &_nu_pdg, "nu_pdg/I");
130  _tree->Branch("nu_e", &_nu_e, "nu_e/F");
131  _tree->Branch("nu_t", &_nu_t, "nu_t/F");
132  _tree->Branch("nu_w", &_nu_w, "nu_w/F");
133  _tree->Branch("nu_x", &_nu_x, "nu_x/F");
134  _tree->Branch("nu_y", &_nu_y, "nu_y/F");
135  _tree->Branch("nu_z", &_nu_z, "nu_z/F");
136  _tree->Branch("nu_vx", &_nu_vx, "nu_vx/F");
137  _tree->Branch("nu_vy", &_nu_vy, "nu_vy/F");
138  _tree->Branch("nu_vz", &_nu_vz, "nu_vz/F");
139  _tree->Branch("nu_px", &_nu_px, "nu_px/F");
140  _tree->Branch("nu_py", &_nu_py, "nu_py/F");
141  _tree->Branch("nu_pz", &_nu_pz, "nu_pz/F");
142  _tree->Branch("nu_decay", &_nu_decay, "nu_decay/I");
143  _tree->Branch("nu_dk2gen", &_nu_dk2gen, "nu_dk2gen/F");
144  _tree->Branch("nu_p_angle", &_nu_p_angle, "nu_p_angle/F");
145  _tree->Branch("nu_p_type", &_nu_p_type, "nu_p_type/I");
146  _tree->Branch("nu_p_dpx", &_nu_p_dpx, "nu_p_dpx/F");
147  _tree->Branch("nu_p_dpy", &_nu_p_dpy, "nu_p_dpy/F");
148  _tree->Branch("nu_p_dpz", &_nu_p_dpz, "nu_p_dpz/F");
149  _tree->Branch("nu_p_e", &_nu_p_e, "nu_p_e/F");
150  _tree->Branch("nu_r", &_nu_r, "nu_r/F");
151  _tree->Branch("nu_oaa", &_nu_oaa, "nu_oaa/F");
152 
153  _tree->Branch("nu_other_x", &_nu_other_x, "nu_other_x/F");
154  _tree->Branch("nu_other_y", &_nu_other_y, "nu_other_y/F");
155  _tree->Branch("nu_other_z", &_nu_other_z, "nu_other_z/F");
156  _tree->Branch("nu_other_t", &_nu_other_t, "nu_other_t/F");
157 }
158 
159 void FluxReaderAna::analyze(art::Event const& e)
160 {
161 
162  art::Handle< std::vector<simb::MCFlux> > mcFluxHandle;
163  e.getByLabel(_flux_label, mcFluxHandle);
164  std::vector<simb::MCFlux> const& fluxlist = *mcFluxHandle;
165 
166  art::Handle< std::vector<simb::MCTruth> > mctruthHandle;
167  e.getByLabel(_flux_label, mctruthHandle);
168  std::vector<simb::MCTruth> const& mclist = *mctruthHandle;
169 
170  for(unsigned int inu = 0; inu < mclist.size(); inu++) {
171  simb::MCParticle nu = mclist[inu].GetNeutrino().Nu();
172  simb::MCFlux flux = fluxlist[inu];
173 
174  // std::cout << "This neutrino has vtx " << nu.Vx() << ", " << nu.Vy() << ", " << nu.Vz() << std::endl;
175  // std::cout << "This neutrino has dir " << nu.Px() << ", " << nu.Py() << ", " << nu.Pz() << std::endl;
176 
177  // Calculate the position of the neutrino at Z = _nu_intersection_z
178  TVector3 intersection = GetIntersection(TVector3(nu.Vx(),nu.Vy(),nu.Vz()),
179  TVector3(nu.Px(),nu.Py(),nu.Pz()),
181  // Calculate the distance from the original to the new position
182  float dist = (TVector3(nu.Vx(),nu.Vy(),nu.Vz()) - intersection).Mag();
183 
184  // Do the same for an additional Z
185  TVector3 intersection_other = GetIntersection(TVector3(nu.Vx(),nu.Vy(),nu.Vz()),
186  TVector3(nu.Px(),nu.Py(),nu.Pz()),
188  float other_dist = (TVector3(nu.Vx(),nu.Vy(),nu.Vz()) - intersection_other).Mag();
189 
190  float light_speed = TMath::C() / 1e7; // cm / ns
191 
192  _nu_pdg = nu.PdgCode();
193  _nu_e = nu.E();
194  _nu_t = nu.T() + dist / light_speed;
195  _nu_w = flux.fnimpwt;
196  _nu_x = intersection.X();
197  _nu_y = intersection.Y();
198  _nu_z = intersection.Z();
199  _nu_vx = nu.Vx();
200  _nu_vy = nu.Vy();
201  _nu_vz = nu.Vz();
202  _nu_px = nu.Px();
203  _nu_py = nu.Py();
204  _nu_pz = nu.Pz();
205  _nu_decay = flux.fndecay;
206  _nu_dk2gen = flux.fdk2gen;
207  _nu_p_type = flux.fptype;
208  _nu_p_dpx = flux.fpdpx;
209  _nu_p_dpy = flux.fpdpy;
210  _nu_p_dpz = flux.fpdpz;
211  float parent_mass = _pdg_database->GetParticle(_nu_p_type)->Mass();
212  _nu_p_e = std::sqrt(flux.fpdpx * flux.fpdpx +
213  flux.fpdpy * flux.fpdpy +
214  flux.fpdpz * flux.fpdpz +
215  parent_mass * parent_mass);
216  _nu_p_angle = TVector3(_nu_px, _nu_py, _nu_pz).Angle(TVector3(_nu_p_dpx, _nu_p_dpy, _nu_p_dpz));
217  _nu_r = std::sqrt((_nu_x - _x_shift) * (_nu_x - _x_shift) + _nu_y * _nu_y);
218  _nu_oaa = std::atan(_nu_r * _nu_r / _baseline);
219 
220  _nu_other_x = intersection_other.X();
221  _nu_other_y = intersection_other.Y();
222  _nu_other_z = intersection_other.Z();
223  _nu_other_t = nu.T() + other_dist / light_speed;
224 
225  if (_apply_position_cuts) {
226  if (_nu_x < -_x_cut || _nu_x > _x_cut || _nu_y < -_y_cut || _nu_y > _y_cut) {
227  continue;
228  }
229  }
230 
231  _tree->Fill();
232  }
233 }
234 
235 TVector3 FluxReaderAna::GetIntersection(TVector3 nu_pos, TVector3 nu_dir, float z_location) {
236 
237  TVector3 plane_point(0, 0, z_location);
238  TVector3 plane_normal(0, 0, 1);
239 
240  TVector3 diff = nu_pos - plane_point;
241  double prod1 = diff.Dot(plane_normal);
242  double prod2 = nu_dir.Dot(plane_normal);
243  double prod3 = prod1 / prod2;
244  return nu_pos - nu_dir * prod3;
245 
246 }
247 
248 DEFINE_ART_MODULE(FluxReaderAna)
float _nu_vz
Y poisition of neutrino at neutrino production point.
pdgs p
Definition: selectors.fcl:22
void analyze(art::Event const &e) override
std::string _flux_label
Label for flux dataproduct (to be set via fcl)
float _nu_p_dpy
Neutrino parent momentum x.
float _nu_other_x
Neutrino off axis angle.
float _nu_p_dpx
Neutrino parent pdg.
float _nu_py
X momentum of neutrino.
float _nu_vy
X poisition of neutrino at neutrino production point.
float _nu_dk2gen
Neutrino parent decay code.
float _nu_other_intersection_z
Additional Z position where to evaluate the flux (to be set via fcl)
int _nu_p_type
Angle between neutrino and parent direction.
float _nu_other_t
Z poisition of neutrino at the front face of the TPC (other)
float _nu_other_y
X poisition of neutrino at the front face of the TPC (other)
float _nu_oaa
Neutrino r.
FluxReaderAna & operator=(FluxReaderAna const &)=delete
float _nu_r
Neutrino parent energy.
float _nu_px
Z poisition of neutrino at neutrino production point.
TVector3 GetIntersection(TVector3 nu_pos, TVector3 nu_dir, float z_location=0)
Returns the intersection point with the front face of the TPC.
float _nu_y
X poisition of neutrino at the front face of the TPC.
float _nu_z
Y poisition of neutrino at the front face of the TPC.
float _nu_intersection_z
Z position where to evaluate the flux (to be set via fcl)
float _nu_x
Neutrino weight.
int _nu_pdg
True if the neutrino hit the requested volumes.
float _nu_p_e
Neutrino parent momentum x.
float _nu_w
Time of the neutrino.
float _nu_t
Energy of the neutrino.
const TDatabasePDG * _pdg_database
constexpr double dist(const TReal *x, const TReal *y, const unsigned int dimension)
float _nu_vx
Z poisition of neutrino at the front face of the TPC.
int _nu_decay
Z momentum of neutrino.
do i e
float _nu_p_angle
Distance from decay to ray origin.
float _nu_p_dpz
Neutrino parent momentum x.
FluxReaderAna(fhicl::ParameterSet const &p)
float _nu_pz
Y momentum of neutrino.
BEGIN_PROLOG SN nu
float _nu_e
PDG of neutrino.
art framework interface to geometry description
float _nu_other_z
Y poisition of neutrino at the front face of the TPC (other)