NearlinePlotMakerJPD.h
Go to the documentation of this file.
1 #ifndef _NLPlotMkr
2 #define _NLPlotMkr
3 
4 #include <fstream>
5 #include <TH1F.h>
6 #include <TH2F.h>
7 #include <TGraph.h>
8 #include <TGraphErrors.h>
9 #include <TTree.h>
10 #include <TCanvas.h>
11 #include <TFile.h>
12 #include <TPaveText.h>
13 #include <TLegend.h>
14 #include <TStyle.h>
15 #include <TSystem.h>
16 #include <TDatime.h>
17 #include <TMultiGraph.h>
18 #include <TGaxis.h>
19 #include <TPaveStats.h>
20 
21 // REMOVE includes below to be removed when I add partition to the OnMon header
22 #include <TSQLServer.h>
23 #include <TSQLResult.h>
24 #include <TSQLStatement.h>
25 #include <iostream>
26 #include <iomanip>
27 #include <sstream>
28 #include <string>
29 #include <cstring>
30 
31 // NOTE: Go through ALL of the above and remove what isn't necessary!!!
32 
33 
34 using namespace std;
35 
36 //
37 // Define global variables to be used in NearlinePlotMaker.C
38 //
39 
40 ifstream inFile;
41 
42 TDatime *TNowGMT; // current GMT time
43 TDatime *TNow; // current local time
44 TDatime *SRtime;
45 Int_t GMToffset;
46 
47 unsigned int run;
48 unsigned int subrun;
49 
50 UInt_t LastRun;
51 UInt_t LastSR;
52 
53 
54 
55 struct NearlinePlot{
56 
57  TH1F* fHistogram;
58  TGraph* fGraphTime;
59  std::string fHistName;
62  std::vector<float> fMetricVec;
63  std::vector<float> fTimeVec;
64  int fPlotCount;
65 
66  bool fRMSPlot;
68 
69  NearlinePlot(int Npoint, std::string this_hist_name, std::string this_output_name, std::string hist_title, int num_bins, int min_x, int max_x, bool RMS=false);
70  bool AddHistogram(TFile const & file, TTree* header, int Xstrtime, int Xsrtime, int XNow, int GMToffset);
71  TCanvas* makeHistoCanvas(std::string can_name, std::string can_title, int width, int height, TPaveText* updateText);
72  TCanvas* makeGraphTimeCanvas(std::string can_name, std::string can_title, int width, int height);
73 
74 
75 };
76 
77 
78 NearlinePlot::NearlinePlot(int Npoint, std::string this_hist_name, std::string this_output_name, std::string hist_title, int num_bins, int min_x, int max_x, bool RMS)
79 {
80  fNumPoints = Npoint;
81  fHistName = this_hist_name;
82  fOutputName = this_output_name;
83  fMetricVec = std::vector<float>(Npoint, 0);
84  fTimeVec = std::vector<float>(Npoint, 0);
85  fPlotCount = 0;
86  fRMSPlot=RMS;
87  fHistogram = new TH1F(fHistName.c_str(), hist_title.c_str(), num_bins, min_x, max_x);
88  fGraphTime = 0;
89 }
90 
91 bool NearlinePlot::AddHistogram(TFile const & file, TTree* header, int Xstrtime, int Xsrtime, int XNow, int GMToffset){
92  TH1F *hist_temp = (TH1F*)file.FindObjectAny(fHistName.c_str());
93  if(hist_temp != 0){
94  fHistogram->Add(hist_temp,1.0);
95  }
96  else return false;
97 
98  if(hist_temp != 0 && header != 0 && Xsrtime != XNow - GMToffset) {
99  fTimeVec.at(fPlotCount) = Xsrtime;
100  if(fRMSPlot) fMetricVec.at(fPlotCount) = hist_temp->GetRMS(1);
101  else fMetricVec.at(fPlotCount) = hist_temp->GetMean(1);
102  fPlotCount++;
103  return true;
104  }
105 
106  return false;
107 
108 }
109 
110 TCanvas* NearlinePlot::makeHistoCanvas(std::string can_name, std::string can_title, int width, int height, TPaveText* updateText){
111  TCanvas *can = new TCanvas(can_name.c_str(), can_title.c_str(), width, height);
112  can->cd();
113  can->SetLogy();
114  gStyle->SetOptStat(111111);
115  fHistogram->SetLineWidth(2);
116  fHistogram->SetLineColor(kRed);
117  updateText->Draw();
118 
119  return can;
120 }
121 
122 TCanvas* NearlinePlot::makeGraphTimeCanvas(std::string can_name, std::string can_title, int width, int height, TPaveTest* updateText){
123  TCanvas* can = new TCanvas(can_name.c_str(), can_title.c_str(), width, height);
124  can->cd();
125  gPad->SetGridx();
126  fGraphTime = new TGraph(fPlotCount);
127  for(int i=0;i<fPlotCount;i++) fGraphTime->SetPoint(i, fTimeVec.at(i), fMetricVec.at(i));
128 
129  fGraphTime->SetTitle(can_title.c_str());
130  fGraphTime->SetMarkerColor(kBlue);
131  fGraphTime->GetXaxis()->SetTimeDisplay(1);
132  fGraphTime->GetXaxis()->SetLabelSize(0.03);
133  fGraphTime->GetXaxis()->SetTimeFormat(taxis_labels.c_str());
134  fGraphTime->GetXaxis()->SetLimits(time_ago,XNow);
135  fGraphTime->GetXaxis()->SetTitle("(central time)");
136  fGraphTime->Draw("A*");
137 
138  updateText->Draw();
139 
140  int maxtime = 0;
141  double max = 0.0, ave = 0.0;
142  TPaveText *LastPoint = new TPaveText(0.3,0.88,0.93,0.93,"NDC");
143  LastPoint->SetLineColor(1);
144  LastPoint->SetFillColor(0);
145  LastPoint->SetBorderSize(1);
146  char lptext[128];
147  maxtime = 0;
148  max = 0.0;
149  ave = 0.0;
150  for(int i = 0; i < fPlotCount; ++i) {
151  ave += (double)fMetricVec.at(i);
152  if(fTimeVec.at(i) > maxtime) {
153  maxtime = fTimeVec.at(i);
154  max = fMetricVec.at(i);
155  }
156  }
157  if(fPlotCount > 0) ave = ave/(double)fPlotCount;
158  sprintf(lptext,"Last Point = %f / Average = %f",max,ave);
159  LastPoint->Clear();
160  LastPoint->AddText(lptext);
161 
162 
163  LastPoint->Draw();
164 
165  return can;
166 
167 }
168 
169 
170 #endif
TCanvas * makeHistoCanvas(TPaveText *updateText, int width=1200, int height=800)
std::string string
Definition: nybbler.cc:12
TCanvas * makeGraphTimeCanvas(std::string can_name, std::string can_title, int width, int height)
STL namespace.
unsigned int run
TDatime * TNow
UInt_t LastSR
static int max(int a, int b)
ifstream inFile
Int_t GMToffset
unsigned int subrun
NearlinePlot(std::string this_hist_name, NearlinePlotInfo this_plot_info, NearlinePlotEnables this_plot_enable=NearlinePlotEnables(), NearlinePlotLogScale this_plot_log_scale=NearlinePlotLogScale())
bool AddHistogram(TFile const &file, TTree *header, int Xsrtime, int XNow, int GMToffset, int time_ago)
UInt_t LastRun
std::string fOutputName
TDatime * TNowGMT
TDatime * SRtime