SpectrumHandler.h
Go to the documentation of this file.
1 ///////////////////////////
2 // SpectrumHandler.h
3 // Author: Connor Johnson
4 // Date Created: 10/10/2018
5 // Class to handle bulk spectra creation and filling.
6 // Serves as a wrapper for the general looping for Spectrum creation
7 // Use case: assigning datasets, vars, cuts, weights.
8 // Will then loop through all combinations and create / save a seperate Spectrum for each.
9 // To be done: implement logic for 2D Spectrums.
10 ///////////////////////////
11 
12 ////////////////////////////////////////////
13 // Usage: This class requires many function calls in a certain order.
14 // For convenience, an example valid call-order is shown below.
15 ////////////////////////////////////////////
16 
17 // // Create the SpectrumHandler object
18 // ana::SpectrumHandler * spechandler = new ana::SpectrumHandler();
19 
20 // // Load the datasets
21 // spechandler->SetLoaders(datasets, dataset_labels); // Necessary first step: define the loaders to be used
22 // spechandler->SetSpillCuts(spillcut); // Only needed if using non-default spill cuts
23 
24 // // Set Vars, Cuts, Weights, and SystShifts (all of them necessary)
25 // spechandler->SetVars(vars, vars_labels, vars_titles, vars_bins); // Always needed (require axis-titles and binning)
26 // spechandler->SetCuts(cuts, cuts_labels); // Always needed
27 // spechandler->SetWeights(ana::kUnweighted); // Allow using only one weight
28 // spechandler->SetShifts(ana::kNoShift); // Allow using only one shift
29 
30 // // Create and fill the spectrums:
31 // spechandler->CreateSpectrums(); // Creates the Spectrum objects
32 // spechandler->Go(); // Call Go() on each loader object
33 
34 // // Save the spectrums
35 // spechandler->SaveSpectrums(fout);
36 
37 #pragma once
38 
39 // Standard includes
40 #include <string>
41 #include <vector>
42 
43 // CAFE includes
44 #include "CAFAna/Core/Binning.h"
45 #include "CAFAna/Core/Var.h"
46 #include "CAFAna/Core/HistAxis.h"
47 #include "CAFAna/Core/MultiVar.h"
48 #include "CAFAna/Core/Cut.h"
49 #include "CAFAna/Core/Spectrum.h"
51 #include "CAFAna/Core/SystShifts.h"
52 
53 // ROOT includes
54 #include <TROOT.h>
55 #include <TFile.h>
56 
57 namespace ana{
58  // Put the SpectrumHandler class in the ana namespace
59  class SpectrumHandler;
60 
61  // Define the SpecHelp progress in order (start unloaded, define loaders, ready for filling, went)
63  kSpecHelpUnloaded, // Default
64  kSpecHelpLoaders, // Loaders ready
65  kSpecHelpReady, // Spectrums are fully defined, not yet called go
66  kSpecHelpWent // All loaders have had go() called.
67  };
68 
69  // Not implemented: defines the verbosity levels (in increasing descriptiveness)
75  };
76 }
77 
79 
80 public:
81 
82  ////////////////////////////////////////////
83  // Core public member functions
84  ////////////////////////////////////////////
85 
86  // Constructors / destructors
87 
88  // Default constructor
90  SpectrumHandler(const std::vector<SpectrumLoaderBase*> ls, const std::vector<std::string> dsls);
91 
92  // Default destructor
93  // ~SpectrumHandler(); // Not implemented: Currently using compiler-made destructor
94 
95  ///////////////////////////////
96  // Spectrum Loading and Setting
97  ///////////////////////////////
98 
99  // Loaders
100  bool SetLoaders( const std::vector<std::string>& ds, const std::vector<std::string>& dsnames);
101  bool SetLoader( const std::string ds, const std::string dslab = "") {return SetLoaders(std::vector<std::string> {ds}, std::vector<std::string> {dslab});};
102  bool SetLoaders( const std::vector<SpectrumLoaderBase*> ls, const std::vector<std::string> llabels);
103  bool SetLoader( SpectrumLoaderBase* l, const std::string llabel = "") {return SetLoaders(std::vector<SpectrumLoaderBase*> {l}, std::vector<std::string> {llabel});};
104  bool SetSpillCuts( const std::vector<SpillCut> spillcuts);
105  bool SetSpillCuts( const SpillCut cut);
106 
107  // Loader access. Reason for this: allow adding more Spectrums to existing Loaders to prevent running the same files multiple times.
108  std::vector<SpectrumLoaderBase*> GetLoaders( const std::vector<std::string> labels);
110 
111  // Vars, cuts, weights, and shifts
112  bool SetVars( const std::vector<Var> v, const std::vector<std::string> vl, const std::vector<std::string> vt, const std::vector<Binning> vb);
113  bool SetCuts( const std::vector<Cut> c, const std::vector<std::string> cl);
114  bool SetWeights( const std::vector<Var> w, const std::vector<std::string> wl);
115  bool SetWeights( const Var w, const std::string wl = "") {return SetWeights(std::vector<Var> {w}, std::vector<std::string> {wl});};
116  bool SetShifts( const std::vector<SystShifts> s, const std::vector<std::string> sl);
117  bool SetShifts( const SystShifts s, const std::string sl = "") {return SetShifts(std::vector<SystShifts> {s}, std::vector<std::string> {sl});};
118 
119  bool SetHistAxes( const std::vector<HistAxis> h, const std::vector<std::string> hl);
120  bool SetHistAxes( const HistAxis h, const std::string hl = "") {return SetHistAxes(std::vector<HistAxis> {h}, std::vector<std::string> {hl});};
121 
122  // Creating Spectrums and filling them
123  bool CheckReady();
124  bool CreateSpectrums();
125  bool Go(); // Wrapper for a loop over loader[i]->Go() with some checks
126  bool Gone(); // Return true if ANY of the loaders return Gone() == true.
127  bool SaveSpectrums(TFile * f);
128 
129  // Return the given status (will need to compare to ana::spec_handle_status values, since they are not provided explicitly)
130  int GetStatus() {return fStatus;};
131 
132 private:
133  ////////////////////////////////////////////
134  // Core private members
135  ////////////////////////////////////////////
136 
137  // Store the status of the SpectrumHandler (see enum ana::spec_handle_status)
139 
140  // Store the number of loaders/vars/cuts/weights/shifts
141  unsigned int fNLoaders = 0;
142  unsigned int fNVars = 0;
143  unsigned int fNCuts = 0;
144  unsigned int fNWeights = 0;
145  unsigned int fNShifts = 0;
146  unsigned int fNHistAxes = 0;
147 
148  // Label holders. Vars and Cuts MUST have labels. Vars need an axis title. Loader, weight, and shift labels can be empty if there is only one.
149  std::vector<std::string> fLoader_labels; // Can be empty if only one loader
150  std::vector<std::string> fVar_labels; // Cannot be empty
151  std::vector<std::string> fVar_titles; // Cannot be empty
152  std::vector<std::string> fCut_labels; // Cannot be empty
153  std::vector<std::string> fWeight_labels; // Can be empty if nweights == 1
154  std::vector<std::string> fShift_labels; // Can be empty if nshifts == 1
155  std::vector<std::string> fHistAxis_labels; // Can be empty if using Var methods
156 
157  // CAFE Object holders, other than Spectrums
158  std::vector <SpectrumLoaderBase*> fLoaders; // All loaders.
159  std::vector <Var> fVars; // All vars to loop over. Require at least one.
160  std::vector <Binning> fVar_bins; // Binning for each Var. (size should be equal to num of vars)
161  std::vector <Cut> fCuts; // All cuts to loop over.
162  std::vector <Var> fWeights; // All weights to loop over.
163  std::vector <SystShifts> fShifts; // All shifts to loop over
164  std::vector <HistAxis> fHistAxes; // All HistAxis to loop over
165 
166  // Spectrum Holder.
167  // Reason for this monstrousity of a 5D vector: easy to allow loops through ANY part of Spectrums
168  // Vector order: datasets -> vars -> cuts -> shifts -> weights
169  std::vector< std::vector< std::vector< std::vector< std::vector< Spectrum* > > > > > fSpecs;
170  std::vector< std::vector< std::vector< std::vector< std::vector< Spectrum* > > > > > fHistAxisSpecs;
171 
172  ////////////////////////////////////////////
173  // Core private functions
174  ////////////////////////////////////////////
175  void CreateVarSpectrums();
177 
178  void SaveVarSpectrums(TDirectory * d, unsigned int il);
179  void SaveHistAxisSpectrums(TDirectory * d, unsigned int il);
180 };
std::vector< std::string > fHistAxis_labels
std::vector< Binning > fVar_bins
std::vector< Var > fWeights
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:20
SpectrumLoaderBase * GetLoader(const std::string label)
bool SetWeights(const Var w, const std::string wl="")
bool SaveSpectrums(TFile *f)
bool SetVars(const std::vector< Var > v, const std::vector< std::string > vl, const std::vector< std::string > vt, const std::vector< Binning > vb)
spec_handle_status
bool SetLoader(SpectrumLoaderBase *l, const std::string llabel="")
bool SetShifts(const std::vector< SystShifts > s, const std::vector< std::string > sl)
std::vector< SpectrumLoaderBase * > fLoaders
std::vector< std::string > fWeight_labels
bool SetSpillCuts(const std::vector< SpillCut > spillcuts)
spec_handle_log_level
const char * label
std::vector< Cut > fCuts
void SaveVarSpectrums(TDirectory *d, unsigned int il)
std::vector< std::string > fShift_labels
const XML_Char * s
Definition: expat.h:262
std::vector< SpectrumLoaderBase * > GetLoaders(const std::vector< std::string > labels)
bool SetHistAxes(const HistAxis h, const std::string hl="")
Float_t d
Definition: plot.C:236
std::vector< std::string > fVar_titles
void SaveHistAxisSpectrums(TDirectory *d, unsigned int il)
A very simple service to remember what detector we&#39;re working in.
bool SetHistAxes(const std::vector< HistAxis > h, const std::vector< std::string > hl)
std::vector< SystShifts > fShifts
std::vector< std::string > fVar_labels
bool SetLoaders(const std::vector< std::string > &ds, const std::vector< std::string > &dsnames)
bool SetShifts(const SystShifts s, const std::string sl="")
Base class for the various types of spectrum loader.
const Cut cut
Definition: exporter_fd.C:30
std::vector< std::vector< std::vector< std::vector< std::vector< Spectrum * > > > > > fSpecs
def ls(target="")
Definition: g4zmq.py:69
bool SetLoader(const std::string ds, const std::string dslab="")
std::vector< std::vector< std::vector< std::vector< std::vector< Spectrum * > > > > > fHistAxisSpecs
std::vector< std::string > fLoader_labels
std::vector< HistAxis > fHistAxes
Template for Cut and SpillCut.
Definition: Cut.h:15
std::vector< std::string > fCut_labels
bool SetCuts(const std::vector< Cut > c, const std::vector< std::string > cl)
Float_t w
Definition: plot.C:20
bool SetWeights(const std::vector< Var > w, const std::vector< std::string > wl)
std::vector< Var > fVars
enum BeamMode string