getStabilitySpectra.C
Go to the documentation of this file.
1 #include "CAFAna/Cuts/Cuts.h"
5 #include "CAFAna/Core/Loaders.h"
7 #include "CAFAna/Core/Spectrum.h"
10 #include "CAFAna/Vars/Vars.h"
11 #include "CAFAna/Analysis/Calcs.h"
12 #include "CAFAna/Analysis/Plots.h"
14 
15 #include "TStyle.h"
16 #include "TCanvas.h"
17 #include "TGraph.h"
18 #include "TGraphErrors.h"
19 #include "TGraphAsymmErrors.h"
20 #include "TFile.h"
21 #include "TH1.h"
22 #include "TH2.h"
23 #include "TLegend.h"
24 #include "TLatex.h"
25 #include "TProfile.h"
26 #include "TSpectrum.h"
27 
30 
31 #include <iostream>
32 using namespace ana;
33 
34 void getStabilitySpectra(std::string samDef, unsigned int endTime, unsigned int nDays, unsigned int fixedBinStart = 0, std::string s_FileAppend = "")
35 {
36 
37  //APPLY TIME CUTS TO DATASET, DEFINE THE LOADER AND SET SPILL CUTS.
38  unsigned int startTime = endTime - 86400*nDays;
39 
40  //const std::string fFiles = "dataset_def_name_newest_snapshot " + samDef + Form(" and online.subrunendtime >= '%u' and online.subrunendtime <= '%u'", startTime, endTime);
41  SpectrumLoader loader(samDef);
43 
44  unsigned int nBins = 0;
45  if(fixedBinStart!=0){
46  unsigned int nSeconds = endTime - fixedBinStart;
47  nBins = (unsigned int)std::floor(nSeconds/86400.) + 1;
48  }
49 
50  const Binning kTimeBinning = fixedBinStart == 0 ? Binning::Simple(nDays, startTime, endTime)
51  : Binning::Simple(nBins, fixedBinStart, fixedBinStart+86400*nBins);
52 
53  //PLOT SPILL TIME FOR A PARTICULAR PERIOD SO THAT WE HAVE A POT TO NORMALISE BY.
54  //CUT USING getYearAndMonth.
55  std::vector<TH1D*> vec_hPOT;
56 
57  //DEFINE SPILL HISTOGRAMS
58  for(int i = 0; i < kNumPeriods; i++)
59  {
60  TH1D *hPOT = new TH1D(Form("hPOT_%i_%i", datasets.at(i).fYear, datasets.at(i).fMonth), ";Central Time;POT (10^{15})",
61  fixedBinStart == 0 ? nDays : nBins,
62  fixedBinStart == 0 ? startTime : fixedBinStart,
63  fixedBinStart == 0 ? endTime : fixedBinStart+86400*nBins);
64  vec_hPOT.push_back(hPOT);
65 
66  loader.AddSpillHistogram(vec_hPOT.at(i), kSpillTime, getYearAndMonth(datasets[i].fYear, datasets[i].fMonth), kPOTe15);
67  }
68 
69 
70  //DEFINE A TFILE TO WRITE SPECTRA TO.
71  TFile *f_Out = new TFile((TString)("BeamMonitoring_StabilitySpectra.root"),"RECREATE");
72 //(TString)(s_FileAppend+"/BeamMonitoring_StabilitySpectra.root"),"RECREATE");
73  TDirectory *dir_POT = f_Out->mkdir("SpillHistPOT");
74 
75  //DEFINE A CONTAINER TO BOOK-KEEP THE SPECTRA.
76  std::map<unsigned int,std::vector<Spectrum*>> map_PeriodToSpectra;
77  std::map<TString,TDirectory*> map_NameToDir;
78 
79  for(unsigned int i = 0; i < vec_hPOT.size(); i++)
80  {
81  for(unsigned int j = 0; j < vec_Plots.size(); j++)
82  {
83  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,
84  kYear == datasets[i].fYear && kMonth == datasets[i].fMonth && vec_Plots.at(j).fCut);
85  map_PeriodToSpectra[i].push_back(spec);
86 
87 
88  if(i==0)
89  {
90  TDirectory *dir_Specs = f_Out->mkdir(("Specs_"+vec_Plots.at(j).fName).c_str());
91  TDirectory *dir_Hists = f_Out->mkdir(("Hists_"+vec_Plots.at(j).fName).c_str());
92  map_NameToDir[("Specs_"+vec_Plots.at(j).fName).c_str()] = dir_Specs;
93  map_NameToDir[("Hists_"+vec_Plots.at(j).fName).c_str()] = dir_Hists;
94  }
95  }
96  }
97 
98  loader.Go();
99 
100  for(auto it : map_PeriodToSpectra)
101  {
102  for(unsigned int i = 0; i < it.second.size(); i++)
103  {
104 
105 
106  std::string dirName = std::to_string(datasets.at(it.first).fYear) + "_" + std::to_string(datasets.at(it.first).fMonth) + "_" + std::to_string(datasets.at(it.first).fStyle) +
107  "_" + std::to_string(datasets.at(it.first).fColor).c_str();
108 
109  it.second.at(i)->SaveTo( map_NameToDir[("Specs_"+vec_Plots.at(i).fName).c_str()], dirName );
110  map_NameToDir[("Hists_"+vec_Plots.at(i).fName).c_str()]->cd();
111  TH2 *h_2D = it.second.at(i)->ToTH2(it.second.at(i)->POT());
112  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));
113  h_2D->SetTitle(datasets.at(it.first).fLabel.c_str());
114  h_2D->Write();
115  }
116 
117  dir_POT->cd();
118  vec_hPOT.at(it.first)->Write();
119  }
120 
121  std::cout << "SPECTRA AND TH2s MADE!" << std::endl;
122 }
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
const SpillVar kSpillTime([](const caf::SRSpillProxy *spill){return spill->spilltimesec;})
std::vector< Plot > vec_Plots
Definition: VarsAndCuts.h:111
std::vector< Dataset > datasets
Definition: Periods.h:3
void SetSpillCut(const SpillCut &cut)
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.
SpillCut getYearAndMonth(unsigned int iYear, unsigned int iMonth)
Definition: VarsAndCuts.h:71
const int kNumPeriods
Definition: Periods.h:8
virtual void Go() override
Load all the registered spectra.
const double j
Definition: BetheBloch.cxx:29
void getStabilitySpectra(std::string samDef, unsigned int endTime, unsigned int nDays, unsigned int fixedBinStart=0, std::string s_FileAppend="")
loader
Definition: demo0.py:10
std::vector< float > Spectrum
Definition: Constants.h:728
OStream cout
Definition: OStream.cxx:6
std::string dirName
Definition: PlotSpectra.h:47
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::string to_string(ModuleType mt)
Definition: ModuleType.h:32
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
enum BeamMode string