All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackReducer/example_macro.cc
Go to the documentation of this file.
1 #include "TFile.h"
2 #include "TH1F.h"
3 #include "TTreeReader.h"
4 #include "TTreeReaderValue.h"
5 
6 #include "Data.h"
7 
8 void read_file() {
9  // open the input file
10  TFile f("./data.root");
11 
12  // maximum number of events to read from file
13  int max_event = -1;
14 
15  // setup a "TTreeReader" to read from the "sbnana" Tree
16  TTreeReader myReader("sbnana", &f);
17 
18  // setup to read the branch "tracks"
19  // Each instance on the branch has a
20  // "Tracks" object which will have all the data on
21  // the event that you need
22  TTreeReaderValue<Tracks> tracks(myReader, "tracks");
23 
24  TFile out("output.root", "CREATE");
25 
26  // histogram which will store muon energies
27  TH1F *muon_energy = new TH1F("muon_energy", "muon_energy", 50, 0, 5);
28 
29  // histogram which will store distance from reconstructed start
30  // to true start
31  TH1F *start_distance = new TH1F("start_distance", "start_distance", 50, 0, 5);
32 
33  int event_ind = 0;
34  // Loop over the sbnana Tree
35  while (myReader.Next()) {
36  // break if we are done
37  if (max_event >= 0 && event_ind == max_event) break;
38 
39  // We are going to make two plots:
40  // A distribution of the true muon energies
41  // and a distribution of the distance from the true
42  // muon start point to the reconstructed muon start point
43 
44  // The input simulation generates a single muon in the
45  // detector which then goes on to create a host of other
46  // particles. To distinguish the initial particle
47  // (which is the one we want) it is given a process name
48  // of "primary"
49  //
50  // So to find our muon, we can search through the list
51  // of particles for one with the process name "primary"
52 
53  int primary_index = -2;
54  for (unsigned i = 0; i < tracks->truth.size(); i++) {
55  const TrueParticle &particle = tracks->truth[i];
56  if (particle.process == "primary") {
57  std::cout << "energy: " << particle.energy << std::endl;
58  muon_energy->Fill(particle.energy);
59  primary_index = i;
60  break;
61  }
62  }
63 
64  for (const Track &track: tracks->reco_tracks) {
65  // check that this is the track that matches
66  // to the true primary muon
67  if (track.truth_match == primary_index) {
68  TVector3 true_start = &tracks->truth[primary_index].trajectory[0][0]; // convert array to TVector
69  TVector3 track_start = &track.trajectory[0][0];
70  start_distance->Fill((true_start - track_start).Mag());
71  break;
72  }
73  }
74 
75  event_ind += 1;
76  }
77  out.cd();
78  muon_energy->Write();
79  start_distance->Write();
80 }
ClusterModuleLabel join with tracks
std::vector< std::array< float, 3 > > trajectory
The reconstructed trajectory [cm].
Definition: Data.h:8
int truth_match
Match to true particle. -1 if no truth match exists. Otherwise, is the index into the truth list...
Definition: Data.h:11
process_name use argoneut_mc_hitfinder track
Definition: Data.h:7
float energy
Initial energy of the particle [GeV].
Definition: Data.h:26
void read_file()
std::string process
Process which created this particle.
Definition: Data.h:29
BEGIN_PROLOG could also be cout