LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
print_genie_weights.C
Go to the documentation of this file.
1 // Example gallery-based ROOT macro that retrieves and prints the contents of
2 // the MCEventWeight data product from an artroot-format file.
3 //
4 // Revised 17 March 2021 by Steven Gardiner <gardiner@fnal.gov>
5 //
6 // Make sure that the gallery ups product is set up before using this macro.
7 #include <iostream>
8 #include <string>
9 
11 
12 const std::string genie_producer_label("generator");
13 const std::string eventweight_producer_label("genieeventweightTest");
14 
15 void print_weights(const std::string& filename)
16 {
17 
18  std::vector<std::string> filenames{filename};
19 
20  gallery::Event ev(filenames);
21 
22  size_t event_count = 1;
23  for (ev.toBegin(); !ev.atEnd(); ++ev) {
24 
25  std::cout << "art event " << event_count << '\n';
26 
27  // Use the GENIE producer label here
28  auto mctruth_handle = ev.getValidHandle<std::vector<simb::MCTruth>>(genie_producer_label);
29  auto gtruth_handle = ev.getValidHandle<std::vector<simb::GTruth>>(genie_producer_label);
30 
31  // Use the EventWeight producer label here
32  auto weights_handle =
33  ev.getValidHandle<std::vector<evwgh::MCEventWeight>>(eventweight_producer_label);
34 
35  // Loop through these objects for each neutrino vertex in the event
36  for (size_t v = 0u; v < mctruth_handle->size(); ++v) {
37 
38  const simb::MCTruth& mc = mctruth_handle->at(v);
39  const simb::GTruth& gt = gtruth_handle->at(v);
40  const evwgh::MCEventWeight& mc_weights = weights_handle->at(v);
41 
42  // Extract desired information
43  const auto& nu = mc.GetNeutrino();
44  if (nu.Nu().NumberTrajectoryPoints() > 0) {
45  double E_nu = nu.Nu().E(0);
46  bool cc = (nu.CCNC() == 0);
47  if (cc)
48  std::cout << "CC";
49  else
50  std::cout << "NC";
51 
52  int mode = nu.Mode();
53  if (mode == 0) {
54  if (cc)
55  std::cout << "QE";
56  else
57  std::cout << "EL";
58  }
59  else if (mode == 1)
60  std::cout << "RES";
61  else if (mode == 2)
62  std::cout << "DIS";
63  else if (mode == 3)
64  std::cout << "COH";
65  else if (mode == 10)
66  std::cout << "MEC";
67  else
68  std::cout << " other";
69  std::cout << " interaction with neutrino energy = " << E_nu << " GeV\n";
70  }
71 
72  // Loop over all of the weights in the MCEventWeight object
73  std::cout << "Weights\n";
74  for (const auto& pair : mc_weights.fWeight) {
75  std::string knob_name = pair.first;
76  std::vector<double> weights = pair.second;
77 
78  std::cout << " " << knob_name << ":\n";
79  for (size_t u = 0u; u < weights.size(); ++u) {
80  double w = weights.at(u);
81  std::cout << " universe #" << u << " has weight = " << w << '\n';
82  }
83  }
84  }
85 
86  ++event_count;
87  }
88 }
double E(const int i=0) const
Definition: MCParticle.h:234
const simb::MCNeutrino & GetNeutrino() const
Definition: MCTruth.h:77
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:146
Definition of data types for geometry description.
std::map< std::string, std::vector< double > > fWeight
Definition: MCEventWeight.h:9
Event generator information.
Definition: MCTruth.h:32
Float_t w
Definition: plot.C:20