File indexing completed on 2025-08-06 08:11:58
0001 #include <iostream>
0002 #include <fstream>
0003 #include <string>
0004 #include <TSQLServer.h>
0005 #include <TSQLResult.h>
0006 #include <TSQLRow.h>
0007 #include <TSystem.h>
0008 #include <TDatime.h>
0009 #include <fstream>
0010 #include <TCanvas.h>
0011 #include <TFile.h>
0012 #include <TGraph.h>
0013 #include <TH2F.h>
0014 #include <TDatime.h>
0015
0016 #include <fun4all/Fun4AllUtils.h>
0017 R__LOAD_LIBRARY(libfun4all.so)
0018
0019
0020 const int MAXRUNS = 20000;
0021 TFile *savefile;
0022 TFile *tfile[MAXRUNS];
0023 TTree *t;
0024 Double_t runindex[MAXRUNS];
0025 TCanvas *ac[100];
0026 TFile *refFile_all;
0027 TString dir;
0028
0029
0030 TGraph *graph;
0031 TGraph *gt_chBych;
0032 TGraph *gq_chBych;
0033
0034
0035 TH2 *h2_mbdtt[MAXRUNS];
0036 Double_t T_current;
0037 TFile *refFile;
0038 TH2 *h2_ref;
0039 Double_t T_ref;
0040 Double_t deltaT;
0041
0042
0043 TH1 *h_mbdq[MAXRUNS][128];
0044 Double_t Q_current[MAXRUNS][128];
0045 TH1 *h_ref_Q[128];
0046 Double_t Q_ref[128];
0047 Double_t Q_ratio[MAXRUNS][128];
0048
0049
0050 TH1 *h_mbdt[MAXRUNS][128];
0051
0052
0053 Double_t T_current_all[MAXRUNS][128];
0054 TH1 *h_ref_all[128];
0055 Double_t T_ref_all[128];
0056 Double_t deltaT_all[MAXRUNS][128];
0057
0058
0059
0060 UInt_t runSeconds;
0061 Int_t startSeconds;
0062
0063
0064 TCanvas *g_Canvas;
0065 TCanvas *gt_Canvas;
0066 TCanvas *gq_Canvas;
0067
0068 unsigned int get_time( Double_t run_number)
0069 {
0070
0071 TString servername = "pgsql://sphnxdaqdbreplica.sdcc.bnl.gov:5432/daq";
0072 TString username = "phnxrc";
0073 TString password = "";
0074 TSQLServer* dbserver = TSQLServer::Connect(servername, username, password);
0075
0076 if (!dbserver)
0077 {
0078 cout << "Failed to connect " << endl;
0079 return -1;
0080 }
0081
0082
0083 if (dbserver->IsConnected())
0084 {
0085 cout << "Connected to the db" << endl;
0086 }
0087 else
0088 {
0089 cout << "Failed to connect to db" << endl;
0090 return -1;
0091 }
0092
0093 TDatime dt;
0094
0095
0096 TString query = "SELECT ctime FROM filelist WHERE filename LIKE '%";
0097 query += run_number;
0098 query += "%';";
0099
0100
0101 TSQLResult* res = dbserver->Query(query);
0102
0103
0104 if (res==nullptr)
0105 {
0106 cout << "Failed to query the database for runnumber " << run_number << endl;
0107 return 0;
0108 }
0109
0110 TSQLRow* row = res->Next();
0111 if (row==nullptr)
0112 {
0113 cout << "No entry found in the database for runnumber " << run_number << endl;
0114 delete res;
0115 return 0;
0116 }
0117
0118 dt.Set( row->GetField(0) );
0119
0120
0121
0122
0123 delete row;
0124 delete res;
0125
0126
0127 dbserver->Close();
0128 delete dbserver;
0129
0130 return dt.Convert();
0131
0132 }
0133
0134 void plot_deltT_vs_time(const TDatime& dt, const char *flistname = "analyzed_Laser_data2023_To_current.list")
0135 {
0136 ifstream flist(flistname);
0137 TString dstfname;
0138 int nruns = 0;
0139 TString name;
0140 TString title;
0141 int icv = 0;
0142 savefile = new TFile(dir + "Tlaser.root", "RECREATE");
0143
0144
0145 graph = new TGraph();
0146 gt_chBych = new TGraph();
0147 gq_chBych = new TGraph();
0148
0149
0150
0151 TDatime startTime(dt);
0152
0153
0154 TDatime runTime;
0155 startTime.Convert();
0156
0157
0158 while (flist >> dstfname)
0159 {
0160 tfile[nruns] = new TFile(dstfname, "READ");
0161 title = dstfname;
0162
0163
0164 h2_mbdtt[nruns] = (TH2 *)tfile[nruns]->Get("h2_mbdtt");
0165
0166 if (h2_mbdtt[nruns] != nullptr)
0167 {
0168
0169 T_current = h2_mbdtt[nruns]->GetMean(1);
0170
0171
0172 refFile = new TFile("LASER_UNCALMBD-00040759-0000.root", "READ");
0173 h2_ref = (TH2 *)refFile->Get("h2_mbdtt");
0174 T_ref = h2_ref->GetMean(1);
0175
0176
0177 deltaT = T_current - T_ref;
0178
0179
0180 runTime.Set(dt.Convert() + nruns * 86400);
0181
0182
0183
0184 pair<int, int> runseg = Fun4AllUtils::GetRunSegment(dstfname.Data());
0185 int run_number = runseg.first;
0186
0187
0188
0189
0190 runSeconds = get_time( run_number);
0191
0192
0193
0194
0195
0196 if ( runSeconds==0 )
0197 {
0198 cout << "ERROR, no timestamp for run " << run_number << endl;
0199 continue;
0200 }
0201
0202
0203 graph->SetPoint(nruns,(Double_t)runSeconds, deltaT);
0204
0205 delete refFile;
0206
0207
0208 for (int ipmt=0; ipmt<128; ipmt++)
0209 {
0210
0211 name = "h_mbdt"; name += ipmt;
0212
0213 h_mbdt[nruns][ipmt] = (TH1*)tfile[nruns]->Get(name);
0214
0215
0216
0217
0218 name = "h_mbdq"; name += ipmt;
0219 h_mbdq[nruns][ipmt] = (TH1*)tfile[nruns]->Get(name);
0220
0221
0222
0223
0224 if (h_mbdt[nruns][ipmt] != nullptr)
0225 {
0226
0227
0228 T_current_all[nruns][ipmt] = h_mbdt[nruns][ipmt]->GetMean(1);
0229
0230 Q_current[nruns][ipmt] = h_mbdq[nruns][ipmt]->GetMean(1);
0231
0232
0233 refFile_all = new TFile("LASER_UNCALMBD-00041145-0000.root", "READ");
0234
0235 name = "h_mbdt"; name += ipmt;
0236 h_ref_all[ipmt] = (TH1*)refFile_all->Get(name);
0237 T_ref_all[ipmt] = h_ref_all[ipmt]->GetMean(1);
0238
0239
0240 name = "h_mbdq"; name += ipmt;
0241 h_ref_Q[ipmt] = (TH1*)refFile_all->Get(name);
0242 Q_ref[ipmt] = h_ref_Q[ipmt]->GetMean(1);
0243
0244
0245
0246
0247 deltaT_all[nruns][ipmt] = T_current_all[nruns][ipmt] - T_ref_all[ipmt];
0248
0249 Q_ratio[nruns][ipmt] = Q_current[nruns][ipmt] / Q_ref[ipmt];
0250
0251
0252
0253
0254
0255
0256
0257 gt_chBych->SetPoint(nruns*128+ipmt,(Double_t)runSeconds, deltaT_all[nruns][ipmt]);
0258
0259 gq_chBych->SetPoint(nruns*128+ipmt,(Double_t)runSeconds, Q_ratio[nruns][ipmt] );
0260
0261 refFile_all->Close();
0262 delete refFile_all;
0263
0264
0265 }
0266
0267 }
0268
0269
0270 }
0271 else
0272 {
0273 cout << "Histogram 'h2_mbdtt' not found in file: " << dstfname << endl;
0274 }
0275
0276 nruns++;
0277 icv++;
0278
0279 }
0280 if(tfile[nruns] != nullptr)
0281 {
0282 tfile[nruns]->Close();
0283 delete tfile[nruns];
0284 }
0285
0286
0287
0288 g_Canvas = new TCanvas("deltaT_graph", "Delta-T vs Time", 1000, 500);
0289 g_Canvas->cd();
0290 g_Canvas->Update();
0291 graph->SetTitle("Delta-T vs Date");
0292 graph->GetXaxis()->SetTimeDisplay(1);
0293
0294
0295 graph->GetXaxis()->SetTimeFormat("%m/%d/%Y ");
0296
0297 graph->GetXaxis()->SetLabelSize(0.02);
0298 graph->GetXaxis()->SetTimeOffset(0,"gmt");
0299 graph->GetXaxis()->SetTitle("Date");
0300 graph->GetYaxis()->SetTitle("Delta-T[ns]");
0301 graph->SetMarkerSize(0.6);
0302 graph->SetMarkerStyle(20);
0303 graph->SetMarkerColor(2.);
0304 graph->Draw("AP");
0305 g_Canvas->SaveAs("Delta-T_vs_Time.png");
0306
0307
0308
0309 gt_Canvas= new TCanvas("deltaT_graph_128ch", "Delta-T vs Date for 128 ch ", 1000, 500);
0310 gt_chBych->SetTitle("Delta-T vs Date");
0311 gt_chBych->GetXaxis()->SetTitle("Date");
0312 gt_chBych->GetYaxis()->SetTitle("Delta-T[ns]");
0313 gt_chBych->GetXaxis()->SetTimeDisplay(1);
0314 gt_chBych->GetXaxis()->SetTimeFormat("%m/%d/%Y ");
0315 gt_chBych->GetXaxis()->SetLabelSize(0.02);
0316 gt_chBych->SetFillColor(kBlue);
0317
0318
0319 gt_chBych->GetXaxis()->SetTimeOffset(0,"gmt");
0320 gt_chBych->SetMarkerSize(0.2);
0321 gt_chBych->SetMarkerStyle(5);
0322 gt_chBych->SetMarkerColor(2.);
0323 gt_chBych->Draw("AP");
0324 gt_Canvas->SaveAs("Delta-T_vs_Time128ch.png");
0325
0326
0327
0328
0329 gq_Canvas= new TCanvas("Q_ratio_graph_128ch", "Q_ratio vs Date for 128 ", 1000, 500);
0330 gq_chBych->SetTitle("Q_ratio vs Date");
0331 gq_chBych->GetXaxis()->SetTitle("Date");
0332 gq_chBych->GetYaxis()->SetTitle("Q_ratio[ADC]");
0333 gq_chBych->GetXaxis()->SetTimeDisplay(1);
0334 gq_chBych->GetXaxis()->SetTimeFormat("%Y/%m/%d ");
0335 gq_chBych->GetXaxis()->SetLabelSize(0.02);
0336 gq_chBych->GetXaxis()->SetTimeOffset(0,"gmt");
0337 gq_chBych->SetMarkerSize(0.2);
0338 gq_chBych->SetMarkerStyle(5);
0339 gq_chBych->SetMarkerColor(kBlue);
0340 gq_chBych->Draw("AP");
0341 gq_Canvas->SaveAs("Q_ration_vs_Time128ch.png");
0342
0343
0344
0345 gPad->Modified();
0346 savefile->Write();
0347 savefile->Close();
0348
0349 cout << "Processed " << nruns << " runs." << endl;
0350 }
0351
0352 int plot_deltT_vs_Timestamp()
0353 { Double_t run_number;
0354 TDatime dt;
0355 get_time(run_number);
0356 plot_deltT_vs_time(dt);
0357 return 0;
0358 }