getStabilitySpectra.C
Go to the documentation of this file.
1 #include "CAFAna/Cuts/Cuts.h"
4 #include "CAFAna/Cuts/NumuCuts2018.h"
5 #include "CAFAna/Core/Loaders.h"
7 #include "CAFAna/Core/Spectrum.h"
9 #include "CAFAna/Vars/NumuVars.h"
10 #include "CAFAna/Vars/Vars.h"
11 #include "CAFAna/Analysis/Calcs.h"
12 #include "CAFAna/Analysis/Plots.h"
13 
14 #include "TStyle.h"
15 #include "TCanvas.h"
16 #include "TGraph.h"
17 #include "TGraphErrors.h"
18 #include "TGraphAsymmErrors.h"
19 #include "TFile.h"
20 #include "TH1.h"
21 #include "TH2.h"
22 #include "TLegend.h"
23 #include "TLatex.h"
24 #include "TProfile.h"
25 #include "TSpectrum.h"
26 
29 
30 using namespace ana;
31 
32 void getStabilitySpectra(std::string samDef, unsigned int endTime, unsigned int nDays, unsigned int fixedBinStart = 0, std::string outDir = "")
33 {
34  //APPLY TIME CUTS TO DATASET, DEFINE THE LOADER AND SET SPILL CUTS.
35  unsigned int startTime = endTime - 86400*nDays;
36  const std::string fFiles = samDef;
37  //const std::string fFiles = "dataset_def_name_newest_snapshot " + samDef + Form(" and online.subrunendtime >= '%u' and online.subrunendtime <= '%u'", startTime, endTime);
38  SpectrumLoader loader(fFiles);
40 
41  unsigned int nBins = 0;
42  if(fixedBinStart!=0){
43  unsigned int nSeconds = endTime - fixedBinStart;
44  nBins = (unsigned int)std::floor(nSeconds/86400.) + 1;
45  }
46 
47  const Binning kTimeBinning = fixedBinStart == 0 ? Binning::Simple(nDays, startTime, endTime)
48  : Binning::Simple(nBins, fixedBinStart, fixedBinStart+86400*nBins);
49  const Cut kNumuNDCut = kNumuCutND2018;
50 
51  //PLOT SPILL TIME FOR A PARTICULAR PERIOD SO THAT WE HAVE A POT TO NORMALISE BY.
52  //CUT USING getYearAndMonth.
53  std::vector<TH1D*> vec_hPOT;
54 
55  //DEFINE SPILL HISTOGRAMS
56  for(int i = 0; i < kNumPeriods; i++)
57  {
58  TH1D *hPOT = new TH1D(Form("hPOT_%i_%i", datasets.at(i).fYear, datasets.at(i).fMonth), ";Central Time;POT (10^{15})",
59  fixedBinStart == 0 ? nDays : nBins,
60  fixedBinStart == 0 ? startTime : fixedBinStart,
61  fixedBinStart == 0 ? endTime : fixedBinStart+86400*nBins);
62  vec_hPOT.push_back(hPOT);
63 
64  loader.AddSpillHistogram(vec_hPOT.at(i), kSpillTime, getYearAndMonth(datasets[i].fYear, datasets[i].fMonth), kPOTe15);
65  }
66 
67 
68  //DEFINE A TFILE TO WRITE SPECTRA TO.
69  TFile *f_Out = new TFile((TString)(outDir+"BeamMonitoring_StabilitySpectra.root"),"RECREATE");
70  TDirectory *dir_POT = f_Out->mkdir("SpillHistPOT");
71 
72  //DEFINE A CONTAINER TO BOOK-KEEP THE SPECTRA.
73  std::map<unsigned int,std::vector<Spectrum*>> map_PeriodToSpectra;
74  std::map<TString,TDirectory*> map_NameToDir;
75 
76  for(unsigned int i = 0; i < vec_hPOT.size(); i++)
77  {
78  for(unsigned int j = 0; j < vec_Plots.size(); j++)
79  {
80  Spectrum *spec = new Spectrum(vec_Plots.at(j).fLabelX, vec_Plots.at(j).fLabelY, loader, kTimeBinning, kTime, vec_Plots.at(j).fBins, vec_Plots.at(j).fVar,
81  kYear == datasets[i].fYear && kMonth == datasets[i].fMonth && vec_Plots.at(j).fCut);
82  map_PeriodToSpectra[i].push_back(spec);
83 
84  if(i==0)
85  {
86  TDirectory *dir_Specs = f_Out->mkdir(("Specs_"+vec_Plots.at(j).fName).c_str());
87  TDirectory *dir_Hists = f_Out->mkdir(("Hists_"+vec_Plots.at(j).fName).c_str());
88  map_NameToDir[("Specs_"+vec_Plots.at(j).fName).c_str()] = dir_Specs;
89  map_NameToDir[("Hists_"+vec_Plots.at(j).fName).c_str()] = dir_Hists;
90  }
91  }
92  }
93 
94  loader.Go();
95 
96  for(auto it : map_PeriodToSpectra)
97  {
98  for(unsigned int i = 0; i < it.second.size(); i++)
99  {
100  it.second.at(i)->SaveTo(map_NameToDir[("Specs_"+vec_Plots.at(i).fName).c_str()]->mkdir(Form("%i_%i_%i_%i", datasets.at(it.first).fYear, datasets.at(it.first).fMonth, datasets.at(it.first).fStyle, datasets.at(it.first).fColor)));
101 
102  map_NameToDir[("Hists_"+vec_Plots.at(i).fName).c_str()]->cd();
103  TH2 *h_2D = it.second.at(i)->ToTH2(it.second.at(i)->POT());
104  h_2D->SetName(Form("%i_%i_%i_%i", datasets.at(it.first).fYear, datasets.at(it.first).fMonth, datasets.at(it.first).fStyle, datasets.at(it.first).fColor));
105  h_2D->SetTitle(datasets.at(it.first).fLabel.c_str());
106  h_2D->Write();
107  }
108 
109  dir_POT->cd();
110  vec_hPOT.at(it.first)->Write();
111  }
112 
113  std::cout << "SPECTRA AND TH2s MADE!" << std::endl;
114 }
int nBins
Definition: plotROC.py:16
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:16
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
set< int >::iterator it
void getStabilitySpectra(std::string samDef, unsigned int endTime, unsigned int nDays, unsigned int fixedBinStart=0, std::string outDir="")
const SpillVar kSpillTime([](const caf::SRSpillProxy *spill){return spill->spilltimesec;})
const Cut kNumuCutND2018
Definition: NumuCuts2018.h:41
void SetSpillCut(const SpillCut &cut)
std::string outDir
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:40
const Var kTime
Definition: VarsAndCuts.h:33
virtual void AddSpillHistogram(TH1 *h, const SpillVar &var, const SpillCut &cut, const SpillVar &wei=kSpillUnweighted)
Uses include counting the total POT or spills in a run.
const int kNumPeriods
Definition: Periods.h:8
SpillCut getYearAndMonth(unsigned int iYear, unsigned int iMonth)
Definition: VarsAndCuts.h:71
virtual void Go() override
Load all the registered spectra.
const double j
Definition: BetheBloch.cxx:29
loader
Definition: demo0.py:10
std::vector< float > Spectrum
Definition: Constants.h:610
OStream cout
Definition: OStream.cxx:6
Collaborates with Spectrum and OscillatableSpectrum to fill spectra from CAF files.
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:11
const SpillVar kPOTe15([](const caf::SRSpillProxy *spill){return spill->spillpot/1e15;})
const SpillCut kStandardSpillCuts
Apply this unless you&#39;re doing something special.
Definition: SpillCuts.h:49
const Var kYear
Definition: VarsAndCuts.h:34
std::vector< Dataset > datasets
Definition: Periods.h:3
static Binning Simple(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:107
const Var kMonth
Definition: VarsAndCuts.h:35
std::vector< Plot > vec_Plots
enum BeamMode string