drawTimePlots.C
Go to the documentation of this file.
1 #ifdef __CINT__
2 void drawTimePlots(std::string samdef, unsigned int timestamp, unsigned int nDays, std::string naming){
3  std::cout << "Sorry, you must run in compiled mode" << std::endl;
4 }
5 #else
6 
7 #include "CAFAna/Cuts/Cuts.h"
10 #include "CAFAna/Cuts/NumuCuts2018.h"
11 #include "CAFAna/Core/Loaders.h"
13 #include "CAFAna/Core/Spectrum.h"
15 #include "CAFAna/Vars/NumuVars.h"
16 #include "CAFAna/Vars/Vars.h"
17 #include "CAFAna/Analysis/Calcs.h"
18 #include "CAFAna/Analysis/Plots.h"
19 
21 
22 #include "TStyle.h"
23 #include "TCanvas.h"
24 #include "TGraph.h"
25 #include "TGraphAsymmErrors.h"
26 #include "TFile.h"
27 #include "TH1.h"
28 #include "TH2.h"
29 #include "TLegend.h"
30 #include "TLatex.h"
31 #include "TProfile.h"
32 
33 #include "CustomFunctions.h"
34 
35 using namespace ana;
36 
37 void drawTimePlots(std::string samdef, unsigned int timestamp, unsigned int nDays, std::string naming)
38 {
39 
40  std::cout << "Drawing plots..." << std::endl;
41 
42  gStyle->SetStatFormat("6.3g"); // Significant figures. Fix if this is not satisfactory
43  gStyle->SetPaintTextFormat("6.3g");
44  gStyle->SetFitFormat("6.3g");
45 
46  // Output directory
47  const std::string outDir = "/nova/ana/users/novadq/BeamMonitoring/Plots/time/";
48  // Loader for data
49  const std::string fFiles = "defname: " + samdef + Form(" and online.subrunendtime >= '%u' and online.subrunendtime <= '%u'", timestamp - 86400*nDays, timestamp);
50 
51  const Binning kTimeBinning = Binning::Simple(nDays<3 ? 24*nDays : nDays,timestamp - 86400*nDays,timestamp);
52 
53  SpectrumLoader loader(fFiles);
54 
55  //Spill cuts
57 
58  /// ====================================================================== ///
59 
60  const Var kSpillPOT([](const caf::SRProxy *sr)
61  {
62  return sr->spill.spillpot/1e12;
63  });
64 
65  const Var kTime = SIMPLEVAR(hdr.unixtime);
66 
67  const Cut kNumuNDCut = kNumuCutND2018;
68 
69  /// ====================================================================== ///
70 
71  struct Plot
72  {
75  Binning bins;
76  Var var;
77  Cut cut;
78  };
79 
80  const int kNumPlots = 9;
81 
82  Plot plots[kNumPlots] = {
83  {";Central Time;Spill POT (#times10^{12})", "spillPOT", Binning::Simple(100,0,100), kSpillPOT, kNoCut},
84  {";Central Time;Reconstructed neutrino energy (GeV)","ccE", Binning::Simple(20,0,5), kCCE, kNumuNDCut},
85  {";Central Time;Reconstructed hadronic energy (GeV)","hadE", Binning::Simple(30,0,3), kHadE, kNumuNDCut},
86  {";Central Time;Reconstructed muon energy (GeV)","muonE", Binning::Simple(45,0,4.5), kMuE, kNumuNDCut},
87  {";Central Time;Hadronic energy fraction","hadEfrac", Binning::Simple(20,0,1.0), kHadEFrac, kNumuNDCut},
88  {";Central Time;No. of hits","nhit", Binning::Simple(25,0,250), kNHit, kNumuNDCut},
89  {";Central Time;Vertex X (m)","startX", Binning::Simple(40,-2,2), kTrkStartX, kNumuNDCut},
90  {";Central Time;Vertex Y (m)","startY", Binning::Simple(40,-2,2), kTrkStartY, kNumuNDCut},
91  {";Central Time;Vertex Z (m)","startZ", Binning::Simple(24,0,12), kTrkStartZ, kNumuNDCut}
92  };
93 
94  /// ====================================================================== ///
95 
96  Spectrum* hData[kNumPlots];
97 
98  for(int i = 0; i < kNumPlots; ++i){
99  hData[i] = new Spectrum(plots[i].label, loader, kTimeBinning, kTime,
100  plots[i].bins, plots[i].var, plots[i].cut);
101  } // end loop over i
102 
103  // GO!
104  loader.Go();
105 
106  for(int i = 0; i < kNumPlots; ++i){
107 
108  std::string fullName = plots[i].fname+"_"+naming;
109  std::string dim1Name = plots[i].fname+"_1D_"+naming;
110 
111  TCanvas* c = new TCanvas("c",(fullName).c_str());
112 
113  c->SetBottomMargin(0.15);
114  c->SetLeftMargin(0.15);
115  c->SetRightMargin(0.15);
116 
117  TH2* Histogram = hData[i]->ToTH2(hData[i]->POT());
118  Histogram->SetName(("Sig_"+fullName).c_str());
119  Histogram->GetXaxis()->CenterTitle();
120  Histogram->GetYaxis()->CenterTitle();
121  Histogram->GetXaxis()->SetTimeDisplay(1);
122 
123  if (nDays >= 15)
124  Histogram->GetXaxis()->SetTimeFormat("%y-%m-%d%F1970-01-01 00:00:00");
125  else if(nDays > 3 && nDays < 15)
126  Histogram->GetXaxis()->SetTimeFormat("%y-%m-%d-%H:%M%F1970-01-01 00:00:00");
127  else
128  Histogram->GetXaxis()->SetTimeFormat("%H:%M%F1970-01-01 00:00:00");
129 
130  unsigned int minBin = Histogram->GetXaxis()->FindBin(timestamp - 86400*nDays);
131  unsigned int maxBin = Histogram->GetXaxis()->FindBin(timestamp);
132 
133  double minZoom = Histogram->GetXaxis()->GetBinLowEdge(minBin-1);
134  double maxZoom = Histogram->GetXaxis()->GetBinLowEdge(maxBin+2);
135 
136  Histogram->GetXaxis()->SetRangeUser(minZoom, maxZoom);
137 
138  TProfile* ProfileEdge = Histogram->ProfileX((fullName+"ProfileEdge").c_str());
139  TProfile* ProfileFull = Histogram->ProfileX((fullName+"ProfileFull").c_str());
140  ProfileEdge->SetMarkerStyle(24);
141  ProfileEdge->SetMarkerColor(kBlack);
142  ProfileEdge->SetLineColor(kBlack);
143  ProfileEdge->SetMarkerSize(1.4);
144  ProfileFull->SetMarkerStyle(20);
145  ProfileFull->SetMarkerColor(kYellow);
146  ProfileFull->SetMarkerSize(1.4);
147 
148  Histogram->Draw("COLZ");
149  ProfileFull->Draw("same");
150  ProfileEdge->Draw("same");
151 
152  TLatex* ExposureLabel = new TLatex(0.20,0.80,Form("%6.3g POT",hData[i]->POT()) );
153 
154  ExposureLabel->SetNDC();
155  ExposureLabel->SetTextSize(0.04);
156  ExposureLabel->Draw();
157 
158  lastUpdated(timestamp);
159 
160  // c->SaveAs(("timeplots/"+fullName+".png").c_str());
161  // c->SaveAs(("timeplots/"+fullName+".root").c_str());
162  c->SaveAs((outDir+fullName+".png").c_str());
163  // c->SaveAs((outDir+fullName+".root").c_str());
164 
165  /// Now the 1-D projection ///
166 
167  c->Clear();
168 
169  TH1* Histo1D = Histogram->ProjectionY((dim1Name).c_str());
170 
171  Histo1D->SetMarkerStyle(20);
172 
173  if(nDays > 300)
174  {
175  Histo1D->SetFillColor(kCyan);
176  Histo1D->Draw("hist");
177  }
178  else
179  {
180  Histo1D->Draw("EX0");
181  }
182 
183  // Histo1D->SaveAs(("timeplots/"+dim1Name+".root").c_str());
184  Histo1D->SaveAs((outDir+dim1Name+".root").c_str());
185 
186  TFile* fLoad;
187  TH1* hLoad;
188  TLegend* ll;
189 
190  if(i>5) ll = new TLegend(0.39,0.20,0.64,0.45);
191  else ll = new TLegend(0.55,0.55,0.8,0.8);
192  ll->SetFillColor(10);
193 
194  ll->AddEntry(Histo1D,"ND data","lp");
195 
196  if(nDays < 300)
197  {
198  std::string dim1Load = plots[i].fname+"_1D_Year";
199  //fLoad = new TFile(("timeplots/"+dim1Load+".root").c_str());
200  fLoad = new TFile((outDir+dim1Load+".root").c_str());
201  hLoad = (TH1*) fLoad->Get(dim1Load.c_str());
202  hLoad->SetFillColor(0);
203  hLoad->SetLineColor(kRed);
204  hLoad->SetMarkerColor(kRed);
205  double fScaling = Histo1D->Integral()*1./hLoad->Integral();
206  hLoad->Scale(fScaling);
207  hLoad->Draw("hist same");
208  ll->AddEntry(hLoad,"Yearly Avg. (scaled)","l");
209  }
210 
211  ll->Draw();
212 
213  lastUpdated(timestamp);
214 
215  //c->SaveAs(("timeplots/"+dim1Name+".png").c_str());
216  c->SaveAs((outDir+dim1Name+".png").c_str());
217 
218  c->Close();
219 
220  } // End loop over i
221 
222  std::cout << "All plots finished!" << std::endl;
223 
224 } // End of function
225 
226 #endif
const Var kHadE
Definition: NumuVars.h:23
caf::Proxy< caf::SRSpill > spill
Definition: SRProxy.h:2143
enum BeamMode kRed
TH2 * ToTH2(double exposure, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Spectrum must be 2D to obtain TH2.
Definition: Spectrum.cxx:165
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
caf::Proxy< float > spillpot
Definition: SRProxy.h:1407
const Cut kNumuCutND2018
Definition: NumuCuts2018.h:41
Proxy for caf::StandardRecord.
Definition: SRProxy.h:2126
const Var kTrkStartY([](const caf::SRProxy *sr){if(sr->trk.kalman.ntracks< 1) return-10.0f;return sr->trk.kalman.tracks[0].start.Y()/100;})
Definition: NumuVars.h:52
void SetSpillCut(const SpillCut &cut)
void drawTimePlots(std::string samdef, unsigned int timestamp, unsigned int nDays, std::string naming)
Definition: drawTimePlots.C:37
std::string outDir
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:40
const Var kTime
Definition: VarsAndCuts.h:33
const char * label
void lastUpdated(unsigned int timestamp)
====================================================================== ///
Definition: CutFlow_Data.C:28
const Var kSpillPOT
Return the POT for the spill.
Definition: SpillVars.cxx:33
const Var kHadEFrac
Definition: NumuVars.h:24
const int kNumPlots
Definition: GetSpectra.h:1
const Var kNHit
Definition: Vars.cxx:71
const std::vector< Plot > plots
const Var kTrkStartZ([](const caf::SRProxy *sr){if(sr->trk.kalman.ntracks< 1) return-10.0f;return sr->trk.kalman.tracks[0].start.Z()/100;})
Definition: NumuVars.h:53
const Var kCCE
Definition: NumuVars.h:21
caf::StandardRecord * sr
virtual void Go() override
Load all the registered spectra.
loader
Definition: demo0.py:10
std::vector< float > Spectrum
Definition: Constants.h:610
const Var kTrkStartX([](const caf::SRProxy *sr){if(sr->trk.kalman.ntracks< 1) return-10.0f;return sr->trk.kalman.tracks[0].start.X()/100;})
Definition: NumuVars.h:51
OStream cout
Definition: OStream.cxx:6
std::vector< double > POT
const Binning bins
Definition: NumuCC_CPiBin.h:8
std::string fname
Definition: CutFlow_Data.C:30
const Cut cut
Definition: exporter_fd.C:30
Collaborates with Spectrum and OscillatableSpectrum to fill spectra from CAF files.
#define SIMPLEVAR(CAFNAME)
For Vars where literally all you need is a single CAF variable.
Definition: Var.h:88
const SpillCut kStandardSpillCuts
Apply this unless you&#39;re doing something special.
Definition: SpillCuts.h:49
const Var kMuE
Definition: NumuVars.h:22
static Binning Simple(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:107
const Cut kNoCut
The simplest possible cut: pass everything, used as a default.
Definition: Cut.h:109
enum BeamMode string