All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Selection.cc
Go to the documentation of this file.
1 #include <string>
2 #include <vector>
3 #include <TChain.h>
4 #include <TTree.h>
5 
6 #include <map>
7 #include <cmath>
8 #include <iostream>
9 #include <ctime>
10 #include <cassert>
11 
12 #include <TFile.h>
13 #include <TParameter.h>
14 #include <TVector3.h>
15 #include <TH1D.h>
16 #include <TH2D.h>
17 #include <TH3D.h>
18 #include <THStack.h>
19 #include <TROOT.h>
20 #include <TCanvas.h>
21 #include <TMath.h>
22 #include <TCanvas.h>
23 #include <core/Event.hh>
24 
25 #include "Selection.h"
26 #include "SetEvent.h"
27 #include "../uScript/api.h"
28 #include "../uScript/value.h"
29 
30 namespace ana {
31 namespace SBNOsc {
32  void Selection::Initialize(fhicl::ParameterSet *config) {
33  std::cout << "Input Configuration:\n";
34  std::cout << config->to_indented_string() << std::endl;
35  fOutputFile = new TFile(config->get<std::string>("OutputFile", "output.root").c_str(), "CREATE");
36  fOutputFile->cd();
37  fCRTHitDistance = config->get<double>("CRTHitDistance", -1);
38 
39  fFileIndex = 0;
40 
41  fCutConfig = config->get<fhicl::ParameterSet>("Cuts");
42 
43  fDoNormalize = config->get<bool>("DoNormalize", false);
44  fGoalPOT = config->get<double>("GoalPOT", 0.);
45  fFillAllTracks = config->get<bool>("FillAllTracks", true);
46 
47  fUseCalorimetry = config->get<bool>("UseCalorimetry", true);
48 
49  fHistogramPostfix = config->get<std::string>("HistogramPostfix", "");
50 
51  std::vector<std::vector<std::string>> track_selector_strings = config->get<std::vector<std::vector<std::string>>>("TrackSelectors", {{""}});
52  std::vector<std::vector<std::string>> track_selector_names = config->get<std::vector<std::vector<std::string>>>("TrackSelectorNames", {{""}});
53 
54  assert(track_selector_strings.size() == track_selector_names.size());
55 
56  fTrackSelectorNames = numu::MultiplyNames(track_selector_names, '/');
57  fTrackSelectors = numu::MultiplyTrackSelectors(track_selector_strings);
58 
59  assert(fTrackSelectorNames.size() == fTrackSelectors.size());
60 
61  std::vector<std::string> track_profile_values = config->get<std::vector<std::string>>("TrackProfileValues", {});
62  for (const std::string &source: track_profile_values) {
63  fTrackProfileValues.push_back(uscript::compile<numu::RecoTrack, numu::TrueParticle, unsigned>("track", "particle", "mctype", source.c_str()));
64  }
65 
66  std::vector<std::string> track_profile_value_names = config->get<std::vector<std::string>>("TrackProfileValueNames", {});
67  std::vector<std::tuple<unsigned, float, float>> track_profile_xranges = config->get<std::vector<std::tuple<unsigned, float, float>>>("TrackProfileXRanges", {});
68 
69  assert(track_profile_value_names.size() == track_profile_xranges.size());
70  assert(fTrackProfileValues.size() == track_profile_xranges.size());
71 
72  if (fDoNormalize) {
73  fNormalize.Initialize(config->get<fhicl::ParameterSet>("Normalize", {}));
74  }
75  fROC.Initialize();
76  fRecoEvent = NULL;
77 
78  fCRTGeo = new sbnd::CRTGeoAlg(fProviderManager->GetGeometryProvider(), fProviderManager->GetAuxDetGeometryProvider());
79 
80  fCuts.Initialize(fCutConfig, fProviderManager->GetGeometryProvider());
81 
82  if (fDoNormalize) {
83  fCosmicHistograms.Initialize(fProviderManager->GetGeometryProvider(), *fCRTGeo, fCuts, "Cosmic", fTrackSelectorNames, track_profile_value_names, track_profile_xranges);
84  }
85  fHistograms.Initialize(fProviderManager->GetGeometryProvider(), *fCRTGeo, fCuts, fHistogramPostfix, fTrackSelectorNames, track_profile_value_names, track_profile_xranges);
86  }
87 
88  void Selection::FileSetup(TFile *f, TTree *eventTree) {
89  eventTree->SetBranchAddress("reco_event", &fRecoEvent);
90  fCuts.Initialize(fCutConfig, fProviderManager->GetGeometryProvider());
91  TParameter<int> *file_type = (TParameter<int> *)f->Get("MCType");
92  fFileType = (numu::MCType) file_type->GetVal();
93 
94  if (fDoNormalize) {
95  if (fFileType == numu::fIntimeCosmic) {
96  fHistsToFill = &fCosmicHistograms;
97  }
98  else if (fFileType == numu::fOverlay) {
99  fHistsToFill = &fHistograms;
100  }
101  else assert(false);
102  }
103  else {
104  fHistsToFill = &fHistograms;
105  }
106  fFileIndex += 1;
107 
108  CRTHistos crt_from_file;
109  crt_from_file.Get(*f, "_all");
110  if (fFileType == numu::fIntimeCosmic) {
111  fCRTCosmicHistos.Add(crt_from_file);
112  }
113  else if (fFileType == numu::fOverlay) {
114  fCRTNeutrinoHistos.Add(crt_from_file);
115  }
116  }
117 
118  void Selection::ProcessSubRun(const SubRun *subrun) {
119  if (fDoNormalize && fFileType == numu::fOverlay) {
120  fNormalize.AddNeutrinoSubRun(*subrun);
121  }
122  }
123 
124  void Selection::ProcessFileMeta(const FileMeta *meta) {
125  if (fDoNormalize && fFileType == numu::fIntimeCosmic) {
126  fNormalize.AddCosmicFile(*meta);
127  }
128  }
129 
130  void Selection::ProcessEvent(const event::Event *core_event) {
131  std::cout << "Event: " << core_event->metadata.eventID << std::endl;
132 
133  // set stuff in the event
134  SetEvent(*fRecoEvent, *core_event, fCuts, fFileType, fUseCalorimetry);
135 
136  for (const numu::RecoInteraction &reco: fRecoEvent->reco) {
137  std::cout << "Event type: " << Histograms::mode2Str(reco.match.mode) << std::endl;
138  const std::vector<size_t> &tracks = reco.slice.tracks;
139  for (size_t ind: tracks) {
140  const numu::RecoTrack &track = fRecoEvent->tracks.at(ind);
141  const numu::RecoParticle &t_particle = reco.slice.particles.at(ind);
142  std::cout << "Reco track: ";
143  std::cout << "length: " << track.length << " PID: " << track.match.match_pdg << " Chi2 m: " << track.chi2_muon << " Chi2 p: " << track.chi2_proton << " n daughter: " << t_particle.daughters.size() << std::endl;
144  for (size_t d_ind: t_particle.daughters) {
145  if (fRecoEvent->tracks.count(d_ind)) {
146  std::cout << "Daughter track: ";
147  const numu::RecoTrack &d_track = fRecoEvent->tracks.at(ind);
148  const numu::RecoParticle &d_t_particle = reco.slice.particles.at(ind);
149  std::cout << "length: " << d_track.length << " PID: " << d_track.match.match_pdg << " Chi2 m: " << d_track.chi2_muon << " Chi2 p: " << d_track.chi2_proton <<" n daughter: " << d_t_particle.daughters.size() << std::endl;
150  }
151  else {
152  std::cout << "Shower Daughter\n";
153  }
154  }
155  }
156  }
157 
158  fROC.Fill(fCuts, *fRecoEvent, fFileType == numu::fIntimeCosmic);
159  fHistsToFill->Fill(*fRecoEvent, *core_event, fCuts, fTrackSelectors, fTrackProfileValues, fFillAllTracks);
160 
161  if (fDoNormalize) {
162  if (fFileType == numu::fIntimeCosmic) fNormalize.AddCosmicEvent(*core_event);
163  else fNormalize.AddNeutrinoEvent(*core_event);
164  }
165 
166  }
167 
168  void Selection::Finalize() {
169  fOutputFile->cd();
170  std::cout << "Finalizing!\n";
171  if (fDoNormalize) {
172  std::cout << "Normalizing!\n";
173  // save scale per 1e20 POT
174  TParameter<float> scale_neutrino("ScaleNeutrino", fNormalize.ScaleNeutrino(1.e20));
175  TParameter<float> scale_cosmic("ScaleCosmic", fNormalize.ScaleCosmic(1.e20));
176  scale_neutrino.Write();
177  scale_cosmic.Write();
178  std::cout << "Scale Neutrino: " << fNormalize.ScaleNeutrino(1.e20) << std::endl;
179  std::cout << "Scale Cosmic: " << fNormalize.ScaleCosmic(1.e20) << std::endl;
180 
181  fHistograms.Scale(fNormalize.ScaleNeutrino(fGoalPOT));
182  fCosmicHistograms.Scale(fNormalize.ScaleCosmic(fGoalPOT));
183 
184  fCRTCosmicHistos.Scale(fNormalize.ScaleNeutrino(fGoalPOT));
185  fCRTNeutrinoHistos.Scale(fNormalize.ScaleCosmic(fGoalPOT));
186 
187  fHistograms.Add(fCosmicHistograms);
188  fROC.Normalize(fNormalize.ScaleNeutrino(fGoalPOT), fNormalize.ScaleCosmic(fGoalPOT));
189  }
190  fROC.BestCuts();
191  fROC.Write();
192  fHistograms.Write();
193  fCRTCosmicHistos.Write();
194  fCRTNeutrinoHistos.Write();
195 
196  }
197  } // namespace SBNOsc
198 } // namespace ana
199 
201 
InteractionMode mode
Mode of the interaction.
float chi2_muon
Chi2 of dE/dx to muon hypotheis. Combined agaisnt all planes.
Definition: RecoTrack.h:43
Metadata metadata
Event metadata.
Definition: Event.hh:230
ClusterModuleLabel join with tracks
std::vector< numu::TrackSelector > fTrackSelectors
Definition: Selection.h:72
do source
TruthMatch match
Info for mathing to truth.
Definition: RecoEvent.h:42
void Get(TFile &f, const std::string &postfix)
Definition: CRTHisto.cc:16
process_name use argoneut_mc_hitfinder track
process_name opflashCryoW ana
std::vector< size_t > daughters
Daughters of the particle in the &quot;particle flow&quot;. Value represents index into pandora information...
Definition: RecoParticle.h:17
Normalize fNormalize
Definition: Selection.h:64
std::vector< TrackSelector > MultiplyTrackSelectors(const std::vector< std::vector< std::string >> &track_function_strings)
process_name standard_reco_uboone reco
int eventID
Event ID.
Definition: Event.hh:52
std::vector< std::string > fTrackSelectorNames
Definition: Selection.h:73
#define DECLARE_SBN_POSTPROCESSOR(classname)
float length
Length of track.
Definition: RecoTrack.h:48
The standard subrun data definition.
Definition: SubRun.hh:23
fhicl::ParameterSet fCutConfig
Definition: Selection.h:65
The standard event data definition.
Definition: Event.hh:228
TrackTruthMatch match
Truth matching information.
Definition: RecoTrack.h:57
RecoSlice slice
Particle content of the interaction.
Definition: RecoEvent.h:39
std::map< size_t, RecoParticle > particles
Map of particle index to particle information.
Definition: RecoEvent.h:28
std::string fHistogramPostfix
Definition: Selection.h:80
std::vector< size_t > tracks
List of track indices contained in this slice.
Definition: RecoEvent.h:29
void SetEvent(numu::RecoEvent &event, const event::Event &core, const ana::SBNOsc::Cuts &cuts, numu::MCType file_type, bool use_calorimetry=true)
Definition: SetEvent.cc:5
MCType
Definition: MCType.h:5
void Initialize(const fhicl::ParameterSet &fcl)
Definition: Normalize.cc:5
int match_pdg
PDG of the MCParticle this track matches to.
void Initialize(fhicl::ParameterSet *config)
Definition: Selection.cc:32
std::vector< numu::TrackFunction > fTrackProfileValues
Definition: Selection.h:75
std::vector< std::string > MultiplyNames(const std::vector< std::vector< std::string >> &strings, char join='_')
BEGIN_PROLOG could also be cout
Metadata for each input file.
Definition: FileMeta.hh:16
float chi2_proton
Chi2 of dE/dx to proton hypothesis. Combined against all planes.
Definition: RecoTrack.h:40