pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixMonitor.cc
1 #include <iostream>
2 #include "PixMonitor.hh"
3 #include "log.h"
4 #include <cstdlib>
5 
6 #include "rsstools.hh"
7 #include "shist256.hh"
8 
9 #include <TString.h>
10 #include <TTimeStamp.h>
11 #include <TDirectory.h>
12 #include <TFile.h>
13 
14 using namespace std;
15 using namespace pxar;
16 
17 // ----------------------------------------------------------------------
18 PixMonitor::PixMonitor(pxarCore *a): fApi(a), fIana(0.), fIdig(0.) {
19 }
20 
21 // ----------------------------------------------------------------------
22 PixMonitor::~PixMonitor() {
23  LOG(logDEBUG) << "PixMonitor dtor";
24 }
25 
26 // ----------------------------------------------------------------------
27 void PixMonitor::init() {
28  LOG(logDEBUG) << "PixMonitor init";
29 }
30 
31 // ----------------------------------------------------------------------
32 void PixMonitor::dumpSummaries() {
33  gFile->cd();
34  LOG(logDEBUG) << "PixMonitor::dumpSummaries";
35  if (fMeasurements.size() < 1) return;
36  ULong_t begSec = fMeasurements[0].first;
37  ULong_t endSec = fMeasurements[fMeasurements.size()-1].first+1;
38  TTimeStamp ts(begSec);
39  // cout << "begSec: " << begSec << " endSec: " << endSec << " endSec-begSec: " << endSec-begSec << " nbins: " << endSec-begSec << endl;
40  TH1D *ha = new TH1D("HA", Form("analog current measurements, start: %s / sec:%ld", ts.AsString("lc"), begSec), endSec-begSec, 0., endSec-begSec);
41  ha->SetXTitle(Form("seconds after %s", ts.AsString("lc")));
42  ha->SetTitleSize(0.03, "X");
43  ha->SetTitleOffset(1.5, "X");
44 
45  TH1D *hd = new TH1D("HD", Form("digital current measurements, start: %s / sec:%ld", ts.AsString("lc"), begSec), endSec-begSec, 0., endSec-begSec);
46  hd->SetXTitle(Form("seconds after %s", ts.AsString("lc")));
47  hd->SetTitleSize(0.03, "X");
48  hd->SetTitleOffset(1.5, "X");
49 
50  for (unsigned int i = 0; i < fMeasurements.size(); ++i) {
51  int ibin = fMeasurements[i].first - begSec;
52  ha->SetBinContent(ibin+1, fMeasurements[i].second.first);
53  hd->SetBinContent(ibin+1, fMeasurements[i].second.second);
54  }
55 
56  ha->Draw();
57  hd->Draw();
58 
59  ha->SetDirectory(gFile);
60  ha->Write();
61 
62  hd->SetDirectory(gFile);
63  hd->Write();
64 }
65 
66 // ----------------------------------------------------------------------
67 void PixMonitor::update() {
68  int NBINS(10);
69  fIana = fApi->getTBia();
70  fIdig = fApi->getTBid();
71 
72  TTimeStamp ts;
73  ULong_t seconds = ts.GetSec();
74 
75  TH1D *ha = (TH1D*)gDirectory->Get("ha");
76  if (0 == ha) {
77  ha = new TH1D("ha", Form("analog current, start: %s / sec:%ld", ts.AsString("lc"), seconds), NBINS, 0, NBINS);
78  ha->SetXTitle(Form("seconds after %s", ts.AsString("lc")));
79  ha->SetTitleSize(0.03, "X");
80  ha->SetTitleOffset(1.5, "X");
81  }
82  int histMinSec = getHistMinSec(ha);
83  int ibin = seconds - histMinSec;
84  // if (0 == ibin) ibin = 1;
85  if (ibin > ha->GetNbinsX()) ha = extendHist(ha, ibin);
86  ha->SetBinContent(ibin+1, fIana);
87 
88  TH1D *hd = (TH1D*)gDirectory->Get("hd");
89  if (0 == hd) {
90  hd = new TH1D("hd", Form("digital current, start: %s / sec:%ld", ts.AsString("lc"), seconds), NBINS, 0, NBINS);
91  hd->SetXTitle(Form("seconds after %s", ts.AsString("lc")));
92  hd->SetTitleSize(0.03, "X");
93  hd->SetTitleOffset(1.5, "X");
94  }
95  if (ibin > hd->GetNbinsX()) hd = extendHist(hd, ibin);
96  hd->SetBinContent(ibin+1, fIdig);
97 
98  fMeasurements.push_back(make_pair(seconds, make_pair(fIana, fIdig)));
99 
100 
101 }
102 
103 // ----------------------------------------------------------------------
104 void PixMonitor::drawHist(string hname) {
105 
106  ULong_t begSec = fMeasurements[0].first;
107  ULong_t endSec = fMeasurements[fMeasurements.size()-1].first+1;
108  TTimeStamp ts(begSec);
109 
110  string title;
111  if (hname == "iana") title = "analog";
112  if (hname == "idig") title = "digital";
113  TH1D *ha = (TH1D*)gDirectory->Get("HA");
114  if (ha) delete ha;
115  ha = new TH1D("HA", Form("%s current measurements, start: %s / sec:%ld", title.c_str(), ts.AsString("lc"), begSec),
116  endSec-begSec, 0., endSec-begSec);
117  ha->SetXTitle(Form("seconds after %s", ts.AsString("lc")));
118  ha->SetTitleSize(0.03, "X");
119  ha->SetTitleOffset(1.5, "X");
120  ha->SetMinimum(0.);
121  ha->SetMarkerStyle(20);
122  ha->SetMarkerSize(1);
123 
124 
125  if (hname == "iana") {
126  for (unsigned int i = 0; i < fMeasurements.size(); ++i) {
127  int ibin = fMeasurements[i].first - begSec;
128  ha->SetBinContent(ibin+1, fMeasurements[i].second.first);
129  }
130  }
131 
132  if (hname == "idig") {
133  for (unsigned int i = 0; i < fMeasurements.size(); ++i) {
134  int ibin = fMeasurements[i].first - begSec;
135  ha->SetBinContent(ibin+1, fMeasurements[i].second.second);
136  }
137  }
138 
139  ha->Draw("p");
140 
141 }
142 
143 
144 
145 // ----------------------------------------------------------------------
146 TH1D* PixMonitor::extendHist(TH1D *h, int ibin) {
147  if (0 == h) return 0;
148  int nbins = h->GetNbinsX();
149  TH1D *hnew = new TH1D("nhew", h->GetTitle(), ibin + 10, 0, ibin + 10);
150  for (int i = 1; i <= nbins; ++i) {
151  hnew->SetBinContent(i, h->GetBinContent(i));
152  hnew->SetXTitle(h->GetXaxis()->GetTitle());
153  hnew->SetTitleSize(0.03, "X");
154  hnew->SetTitleOffset(1.5, "X");
155  }
156  string hname = h->GetName();
157  delete h;
158  hnew->SetName(hname.c_str());
159  return hnew;
160 }
161 
162 
163 // ----------------------------------------------------------------------
164 UInt_t PixMonitor::getHistMinSec(TH1D *h) {
165  string htitle = h->GetTitle();
166  string::size_type s1 = htitle.rfind("sec:");
167  string sseconds = htitle.substr(s1+4);
168  UInt_t seconds = atoi(sseconds.c_str());
169  return seconds;
170 }
171 
double getTBid()
Definition: api.cc:434
double getTBia()
Definition: api.cc:424