Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:20:24

0001 #include "CemcMonDraw.h"
0002 
0003 #include <onlmon/OnlMonClient.h>
0004 
0005 #include <onlmon/triggerEnum.h>
0006 
0007 #include <calobase/TowerInfoDefs.h>
0008 
0009 #include <TAxis.h>  // for TAxis
0010 #include <TCanvas.h>
0011 #include <TFile.h>
0012 #include <TFrame.h>
0013 #include <TGraph.h>
0014 #include <TGraphErrors.h>
0015 #include <TH1.h>
0016 #include <TH2.h>
0017 #include <TLegend.h>
0018 #include <TLine.h>
0019 #include <TPad.h>
0020 #include <TPaveText.h>
0021 #include <TProfile.h>
0022 #include <TProfile2D.h>
0023 #include <TQObject.h>
0024 #include <TROOT.h>
0025 #include <TStyle.h>
0026 #include <TSystem.h>
0027 #include <TText.h>
0028 
0029 #include <cstring>  // for memset
0030 #include <ctime>
0031 #include <fstream>
0032 #include <iostream>  // for operator<<, basic_ostream, basic_os...
0033 #include <sstream>
0034 #include <vector>  // for vector
0035 
0036 CemcMonDraw::CemcMonDraw(const std::string &name)
0037   : OnlMonDraw(name)
0038 {
0039   return;
0040 }
0041 
0042 int CemcMonDraw::Init()
0043 {
0044   std::cout << "initializing" << std::endl;
0045 
0046   cemcStyle = new TStyle("cemcStyle", "cemcStyle");
0047   gStyle->SetOptStat(0);
0048   Int_t font = 42;  // Helvetica
0049   cemcStyle->SetLabelFont(font, "x");
0050   cemcStyle->SetTitleFont(font, "x");
0051   cemcStyle->SetLabelFont(font, "y");
0052   cemcStyle->SetTitleFont(font, "y");
0053   cemcStyle->SetLabelFont(font, "z");
0054   cemcStyle->SetTitleFont(font, "z");
0055   cemcStyle->SetOptStat(0);
0056   cemcStyle->SetPadTickX(1);
0057   cemcStyle->SetPadTickY(1);
0058 
0059   gROOT->SetStyle("cemcStyle");
0060   gROOT->ForceStyle();
0061 
0062   const char *CEMCcalib = getenv("CEMCCALIB");
0063   TFile *inputTemplate = nullptr;
0064   TFile *inputTemplate_alltrig = nullptr;
0065   if (!CEMCcalib)
0066   {
0067     std::cout << "CEMCCALIB environment variable not set, empty refence will be used" << std::endl;
0068     h2_template_hit = new TH2D("templateHit", "", 96, 0, 96, 256, 0, 256);
0069   }
0070   else
0071   {
0072     // std::string Templatefilename=std::string(CEMCcalib)+"/"+"Template_40929_100ADC_hits.root";
0073     // std::string Templatefilename=std::string(CEMCcalib)+"/"+"Template_40929_50ADC_hits.root";
0074     // std::string Templatefilename = std::string(CEMCcalib) + "/" + "Template_40929_30ADC_hits.root";
0075     // std::string Templatefilename = std::string(CEMCcalib) + "/" + "Template_43451_100ADC_hits.root";
0076     // std::string Templatefilename = std::string(CEMCcalib) + "/" + "Template_46915_100ADC_hits.root";
0077     // std::string Templatefilename = std::string(CEMCcalib) + "/" + "Template_52876_100ADC_hits.root";
0078     // std::string Templatefilename = std::string(CEMCcalib) + "/" + "Template_54117_100ADC_hits.root";
0079     //std::string Templatefilename = std::string(CEMCcalib) + "/" + "Template_54373_100ADC_hits.root";
0080     //std::string Templatefilename = std::string(CEMCcalib) + "/" + "Template_67535_100ADC_hits.root";
0081     std::string Templatefilename = std::string(CEMCcalib) + "/" + "Template_69238_100ADC_hits.root";
0082 
0083     inputTemplate = new TFile(Templatefilename.c_str(), "READ");
0084     if (!inputTemplate->IsOpen())
0085     {
0086       std::cout << Templatefilename << " could not be opened. Empty reference will be used" << std::endl;
0087       h2_template_hit = new TH2D("templateHit", "", 96, 0, 96, 256, 0, 256);
0088     }
0089     else
0090     {
0091       h2_template_hit = (TH2 *) inputTemplate->Get("h2_cemc_hits_template");
0092       if (!h2_template_hit)
0093       {
0094         std::cout << "h2_cemc_hits_template could not be retrieved from file. Empty reference will be used" << std::endl;
0095         h2_template_hit = new TH2D("templateHit", "", 96, 0, 96, 256, 0, 256);
0096       }
0097     }
0098     // std::string Templatefilename_alltrig = std::string(CEMCcalib) + "/" + "Template_49435_100ADC_alltrig.root";
0099     std::string Templatefilename_alltrig = std::string(CEMCcalib) + "/" + "Template_69238_100ADC_alltrig.root";
0100     inputTemplate_alltrig = new TFile(Templatefilename_alltrig.c_str(), "READ");
0101     if (!inputTemplate_alltrig->IsOpen())
0102     {
0103       std::cout << Templatefilename_alltrig << " could not be opened. Empty reference will be used" << std::endl;
0104       h2_template_hit_alltrig = new TH2D("templateHit_alltrig", "", 96, 0, 96, 256, 0, 256);
0105     }
0106     else
0107     {
0108       h2_template_hit_alltrig = (TH2 *) inputTemplate_alltrig->Get("h2_cemc_rmhits_alltrig");
0109       if (!h2_template_hit_alltrig)
0110       {
0111         std::cout << "h2_cemc_hits_template could not be retrieved from file. Empty reference will be used" << std::endl;
0112         h2_template_hit_alltrig = new TH2D("templateHit_alltrig", "", 96, 0, 96, 256, 0, 256);
0113       }
0114     }
0115   }
0116 
0117   h1_zs = new TH1F("h1_zs", "unsuppressed rate ", 100, 0, 1.1);
0118   h1_zs_low = new TH1F("h1_zs_low", "unsuppressed rate ", 100, 0, 1.1);
0119   h1_zs_high = new TH1F("h1_zs_high", "unsuppressed rate ", 100, 0, 1.1);
0120 
0121   // MakeZSPalette();
0122 
0123   return 0;
0124 }
0125 
0126 int CemcMonDraw::MakeCanvas(const std::string &name)
0127 {
0128   OnlMonClient *cl = OnlMonClient::instance();
0129   int xsize = cl->GetDisplaySizeX();
0130   int ysize = cl->GetDisplaySizeY();
0131   if (name == "CemcMon1")
0132   {
0133     // xpos (-1) negative: do not draw menu bar
0134     TC[0] = new TCanvas(name.c_str(), "CemcMon Tower Hits", -1, ysize, xsize / 3, ysize);
0135     // root is pathetic, whenever a new TCanvas is created root piles up
0136     // 6kb worth of X11 events which need to be cleared with
0137     // gSystem->ProcessEvents(), otherwise your process will grow and
0138     // grow and grow but will not show a definitely lost memory leak
0139     gSystem->ProcessEvents();
0140     Pad[0] = new TPad("cemcpad0", "hit map", 0., 0.15, 1., 0.95);
0141     Pad[0]->Draw();
0142     // this one is used to plot the run number on the canvas
0143     transparent[0] = new TPad("transparent0", "this does not show", 0, 0, 1, 1);
0144     transparent[0]->SetFillStyle(4000);
0145     transparent[0]->Draw();
0146 
0147     // warning
0148     warning[0] = new TPad("warning0", "hot tower warnings", 0, 0, 1, 0.15);
0149     warning[0]->SetFillStyle(4000);
0150     warning[0]->Draw();
0151     TC[0]->SetEditable(false);
0152   }
0153   else if (name == "CemcMon2")
0154   {
0155     // xpos negative: do not draw menu bar
0156     TC[1] = new TCanvas(name.c_str(), "Expert: CemcMon2 Packet Information", -1, ysize, xsize / 3, ysize);
0157     gSystem->ProcessEvents();
0158     Pad[1] = new TPad("cemcpad1", "packet event check", 0.0, 0.6, 1.0 / 2, 0.95, 0);
0159     Pad[2] = new TPad("cemcpad2", "packet size", 0.0, 0.3, 1.0 / 2, 0.6, 0);
0160     Pad[3] = new TPad("cemcpad3", "packet channels", 0.0, 0.0, 1.0 / 2, 0.3, 0);
0161     Pad[1]->Draw();
0162     Pad[2]->Draw();
0163     Pad[3]->Draw();
0164     // this one is used to plot the run number on the canvas
0165     transparent[1] = new TPad("transparent1", "this does not show", 0, 0, 1., 1);
0166     transparent[1]->SetFillStyle(4000);
0167     transparent[1]->Draw();
0168 
0169     // packet warnings
0170     warning[1] = new TPad("warning1", "packet warnings", 0.5, 0, 1, 1);
0171     warning[1]->SetFillStyle(4000);
0172     warning[1]->Draw();
0173     TC[1]->SetEditable(false);
0174   }
0175   else if (name == "CemcMon3")
0176   {
0177     TC[2] = new TCanvas(name.c_str(), "CemcMon Waveform Processing", -1, ysize, xsize / 3, ysize);
0178     gSystem->ProcessEvents();
0179     Pad[4] = new TPad("cemcpad4", "who needs this?", 0.0, 0.6, 1.0, 0.95, 0);
0180     Pad[5] = new TPad("cemcpad5", "who needs this?", 0.0, 0.3, 1.0, 0.6, 0);
0181     Pad[6] = new TPad("cemcpad6", "who needs this?", 0.0, 0.0, 1.0, 0.3, 0);
0182     Pad[4]->Draw();
0183     Pad[5]->Draw();
0184     Pad[6]->Draw();
0185     // this one is used to plot the run number on the canvas
0186     transparent[2] = new TPad("transparent3", "this does not show", 0, 0, 1, 1);
0187     transparent[2]->SetFillStyle(4000);
0188     transparent[2]->Draw();
0189     TC[2]->SetEditable(false);
0190   }
0191   else if (name == "CemcMon4")
0192   {
0193     TC[3] = new TCanvas(name.c_str(), "Expert: CemcMon Waveform Processing", -1, ysize, xsize / 3, ysize);
0194     gSystem->ProcessEvents();
0195     Pad[7] = new TPad("cemcpad7", "who needs this?", 0.0, 0.6, 1.0, 0.95, 0);
0196     Pad[8] = new TPad("cemcpad8", "who needs this?", 0.0, 0.3, 1.0, 0.6, 0);
0197     Pad[9] = new TPad("cemcpad9", "who needs this?", 0.0, 0.0, 1.0, 0.3, 0);
0198     Pad[7]->Draw();
0199     Pad[8]->Draw();
0200     Pad[9]->Draw();
0201     // this one is used to plot the run number on the canvas
0202     transparent[3] = new TPad("transparent3", "this does not show", 0, 0, 1, 1);
0203     transparent[3]->SetFillStyle(4000);
0204     transparent[3]->Draw();
0205     TC[3]->SetEditable(false);
0206   }
0207   else if (name == "CemcMon5")
0208   {
0209     TC[4] = new TCanvas(name.c_str(), "Expert: Trigger Info", -1, ysize, xsize / 3, ysize);
0210     gSystem->ProcessEvents();
0211     Pad[10] = new TPad("cemcpad10", "who needs this?", 0.0, 0.6, 0.45, 0.95, 0);
0212     Pad[11] = new TPad("cemcpad11", "who needs this?", 0.45, 0.6, 0.9, 0.95, 0);
0213     Pad[12] = new TPad("cemcpad12", "who needs this?", 0.0, 0.3, 0.45, 0.6, 0);
0214     Pad[13] = new TPad("cemcpad13", "who needs this?", 0.45, 0.3, 0.9, 0.6, 0);
0215     Pad[14] = new TPad("cemcpad14", "who needs this?", 0.0, 0.0, 0.9, 0.3, 0);
0216     Pad[10]->Draw();
0217     Pad[11]->Draw();
0218     Pad[12]->Draw();
0219     Pad[13]->Draw();
0220     Pad[14]->Draw();
0221     // this one is used to plot the run number on the canvas
0222     transparent[4] = new TPad("transparent4", "this does not show", 0, 0, 1, 1);
0223     transparent[4]->SetFillStyle(4000);
0224     transparent[4]->Draw();
0225     TC[4]->SetEditable(false);
0226   }
0227   else if (name == "CemcMonServerStats")
0228   {
0229     TC[5] = new TCanvas(name.c_str(), "CemcMon6 Server Stats", -1, 0, xsize, ysize);
0230     gSystem->ProcessEvents();
0231     // this one is used to plot the run number on the canvas
0232     transparent[5] = new TPad("transparent5", "this does not show", 0, 0, 1, 1);
0233     transparent[5]->SetFillColor(kGray);
0234     transparent[5]->Draw();
0235     TC[5]->SetEditable(false);
0236   }
0237   else if (name == "CemcAllTrigHits")
0238   {
0239     // xpos (-1) negative: do not draw menu bar
0240     TC[6] = new TCanvas(name.c_str(), "CemcMon Tower Hits all trig", -1, ysize, xsize / 3, ysize);
0241     // root is pathetic, whenever a new TCanvas is created root piles up
0242     // 6kb worth of X11 events which need to be cleared with
0243     // gSystem->ProcessEvents(), otherwise your process will grow and
0244     // grow and grow but will not show a definitely lost memory leak
0245     gSystem->ProcessEvents();
0246     Pad[15] = new TPad("cemcpad15", "hit map", 0., 0.15, 1., 0.95);
0247     Pad[15]->Draw();
0248     // this one is used to plot the run number on the canvas
0249     transparent[6] = new TPad("transparent6", "this does not show", 0, 0, 1, 1);
0250     transparent[6]->SetFillStyle(4000);
0251     transparent[6]->Draw();
0252 
0253     // warning
0254     warning[2] = new TPad("warning6", "hot tower warnings", 0, 0, 1, 0.15);
0255     warning[2]->SetFillStyle(4000);
0256     warning[2]->Draw();
0257     TC[6]->SetEditable(false);
0258   }
0259 
0260   // Commented until potential replacement with TProfile3D
0261   // else if (name == "CemcPopup"){
0262   //   PopUpCanvas = new TCanvas(name.c_str(),"Waveforms Expert",-xsize-0.3,0,xsize*0.3,ysize*0.9);
0263   //   gSystem->ProcessEvents();
0264   //   double step=1./8;
0265   //   for(int i=0; i<8; i++){
0266   //     for(int j=0; j<8; j++){
0267   //    PopUpPad[i][j]=new TPad(Form("PopUpPad%d_%d",i,j),"this does not show",i*step,j*step,(i+1)*step,(j+1)*step);
0268   //    PopUpPad[i][j]->Draw();
0269   //     }
0270   //   }
0271   //   PopUpTransparent=new TPad("PopUpTransparent","this does not show",0,0,1,1);
0272   //   PopUpTransparent->SetFillStyle(4000);
0273   //   PopUpTransparent->Draw();
0274   //   PopUpCanvas->SetEditable(false);
0275   // }
0276   // else if (name == "CemcMon7" ){
0277   //   TC[6] = new TCanvas(name.c_str(),"Basic waveform summary Expert", -xsize/3 , 0, xsize/3, ysize*0.9);
0278   //   gSystem->ProcessEvents();
0279   //   Pad[15]=new TPad("cemcpad15","who needs this?",0.0,0.475,0.5,0.950);
0280   //   Pad[15]->SetRightMargin(0.15);
0281   //   Pad[15]->Draw();
0282   //   Pad[16]=new TPad("cemcpad16","who needs this?",0.5,0.475,1.0,0.950);
0283   //   Pad[16]->Draw();
0284   //   Pad[17]=new TPad("cemcpad17","who needs this?",0.0,0.0,0.5,0.475);
0285   //   Pad[17]->SetRightMargin(0.15);
0286   //   Pad[17]->Draw();
0287   //   Pad[18]=new TPad("cemcpad18","who needs this?",0.5,0.0,1.0,0.475);
0288   //   Pad[18]->Draw();
0289   //   transparent[6] = new TPad("transparent6", "this does not show", 0, 0, 1, 1);
0290   //   transparent[6]->SetFillStyle(4000);
0291   //   transparent[6]->Draw();
0292   //   TC[6]->SetEditable(0);
0293   // }
0294   else if (name == "CemcMon8")
0295   {
0296     TC[7] = new TCanvas(name.c_str(), "Channel unsuppressed event fraction", -1, ysize, xsize / 3, ysize);
0297     gSystem->ProcessEvents();
0298     Pad[19] = new TPad("cemcpad19", "who needs this?", 0.00, 0.3, 1.00, 0.95);
0299     Pad[19]->SetRightMargin(0.15);
0300     Pad[19]->Draw();
0301     Pad[20] = new TPad("hcalpad24", "1d zs rate", 0.0, 0.0, 1.00, 0.3);
0302     Pad[20]->SetRightMargin(0.15);
0303     Pad[20]->Draw();
0304     transparent[7] = new TPad("transparent7", "this does not show", 0, 0, 1, 1);
0305     transparent[7]->SetFillStyle(4000);
0306     transparent[7]->Draw();
0307     TC[7]->SetEditable(false);
0308   }
0309   else if (name == "CemcBadChi2")
0310   {
0311     TC[8] = new TCanvas(name.c_str(), "Bad Chi2", -1, ysize, xsize / 2, ysize);
0312     gSystem->ProcessEvents();
0313     Pad[21] = new TPad("cemcpad21", "who needs this?", 0.00, 0.00, 1.00, 0.95);
0314     Pad[21]->SetRightMargin(0.15);
0315     Pad[21]->Draw();
0316     transparent[8] = new TPad("transparent8", "this does not show", 0, 0, 1, 1);
0317     transparent[8]->SetFillStyle(4000);
0318     transparent[8]->Draw();
0319     TC[8]->SetEditable(false);
0320   }
0321   else if (name == "CemcNoiseRMS")
0322   {
0323     // xpos (-1) negative: do not draw menu bar
0324     TC[9] = new TCanvas(name.c_str(), "CemcMon Noise RMS per tower", -1, ysize, xsize / 3, ysize);
0325     // root is pathetic, whenever a new TCanvas is created root piles up
0326     // 6kb worth of X11 events which need to be cleared with
0327     // gSystem->ProcessEvents(), otherwise your process will grow and
0328     // grow and grow but will not show a definitely lost memory leak
0329     gSystem->ProcessEvents();
0330     Pad[22] = new TPad("cemcpad22", "rms map", 0., 0.15, 1., 0.95);
0331     Pad[22]->Draw();
0332     // this one is used to plot the run number on the canvas
0333     transparent[9] = new TPad("transparent6", "this does not show", 0, 0, 1, 1);
0334     transparent[9]->SetFillStyle(4000);
0335     transparent[9]->Draw();
0336 
0337     // warning
0338     warning[3] = new TPad("warning6", "hot tower warnings", 0, 0, 1, 0.15);
0339     warning[3]->SetFillStyle(4000);
0340     warning[3]->Draw();
0341     TC[9]->SetEditable(false);
0342   }
0343 
0344   return 0;
0345 }
0346 
0347 int CemcMonDraw::Draw(const std::string &what)
0348 {
0349   int iret = 0;
0350   int idraw = 0;
0351   if (what == "ALL" || what == "FIRST")
0352   {
0353     iret += DrawFirst(what);
0354     idraw++;
0355   }
0356 
0357   if (what == "ALL" || what == "SECOND")
0358   {
0359     iret += DrawSecond(what);
0360     idraw++;
0361   }
0362   if (what == "ALL" || what == "THIRD")
0363   {
0364     iret += DrawThird(what);
0365     idraw++;
0366   }
0367   if (what == "ALL" || what == "FOURTH")
0368   {
0369     iret += DrawFourth(what);
0370     idraw++;
0371   }
0372 
0373   if (what == "ALL" || what == "FIFTH")
0374   {
0375     iret += DrawFifth(what);
0376     idraw++;
0377   }
0378 
0379   // if (what == "ALL" || what == "SIXTH")
0380   //{
0381   //   iret += DrawSixth(what);
0382   //   idraw++;
0383   // }
0384 
0385   if (what == "ALL" || what == "SERVERSTATS")
0386   {
0387     iret += DrawServerStats();
0388     idraw++;
0389   }
0390   if (what == "ALL" || what == "ALLTRIGHITS")
0391   {
0392     iret += DrawAllTrigHits(what);
0393     idraw++;
0394   }
0395   if (what == "ALL" || what == "BADCHI2")
0396   {
0397     iret += DrawBadChi2(what);
0398     idraw++;
0399   }
0400 
0401   if (what == "ALL" || what == "NOISERMS")
0402   {
0403     iret += DrawNoiseRMS(what);
0404     idraw++;
0405   }
0406 
0407   // DO NOT CHANGE THE ORDER, DrawSeventh crashes DrawServerStats with an X11 error in the virtual framebuffer in the html
0408   // DO NOT ADD ANY OTHER METHOD AFTER THIS which gets called by "ALL"
0409   if (what == "ALL")
0410   {
0411     // iret += DrawSeventh("SEVENTH");
0412     // idraw++;
0413     iret += DrawSeventh("ALLTRIGZS");
0414     idraw++;
0415   }
0416   if (what == "SEVENTH" || what == "ALLTRIGZS")
0417   {
0418     iret += DrawSeventh(what);
0419     idraw++;
0420   }
0421 
0422   if (!idraw)
0423   {
0424     std::cout << __PRETTY_FUNCTION__ << " Unimplemented Drawing option: " << what << std::endl;
0425     iret = -1;
0426   }
0427   if (std::fabs(iret) != idraw)  // at least one succeeded
0428   {
0429     return 0;
0430   }
0431   return iret;
0432 }
0433 
0434 int CemcMonDraw::DrawFirst(const std::string & /* what */)
0435 {
0436   OnlMonClient *cl = OnlMonClient::instance();
0437   // watch the absolute insanity as we merge all these
0438   // histograms from across seven different machines
0439   if (!h_cemc_datahits)
0440   {
0441     h_cemc_datahits = new TH2D("h_cemc_datahits", "", 96, 0, 96, 256, 0, 256);
0442   }
0443   else
0444   {
0445     h_cemc_datahits->Reset();
0446   }
0447   TH2 *htmp2d;
0448   int deadservercount = 0;
0449   int Nservers = 0;
0450   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
0451   {
0452     Nservers++;
0453     htmp2d = (TH2 *) cl->getHisto(*server, "h2_cemc_rmhits");
0454     // htmp2d=(TH2 *) cl->getHisto(*server, "h2_cemc_rm");
0455     if (htmp2d)
0456     {
0457       h_cemc_datahits->Add(htmp2d);
0458     }
0459     else
0460     {
0461       deadservercount++;
0462     }
0463   }
0464   int avgevents = 0;
0465   int neventhist = 0;
0466   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
0467   {
0468     TH1 *h_eventSource = cl->getHisto(*server, "h1_event");
0469     if (h_eventSource)
0470     {
0471       avgevents += h_eventSource->GetEntries();
0472       neventhist++;
0473     }
0474   }
0475   if (neventhist)
0476   {
0477     avgevents /= neventhist;
0478   }
0479 
0480   // TH2 *hist1[m_ServerSet.size()];
0481   // const int nHists = 4;
0482   // int start[nHists];
0483   // start[0] = -1;
0484   // int i = 0;
0485   // for (auto server = ServerBegin(); server != ServerEnd(); ++server)
0486   //{
0487   //   hist1[i] = (TH2 *) cl->getHisto(*server, "h2_cemc_rm");
0488   //   if (hist1[i] && start[0] == -1)
0489   //   {
0490   //     start[0] = i;
0491   //   }
0492   //   if (start[0] > -1 && hist1[i])
0493   //   {
0494   //     hist1[i]->SetName(Form("h2_cemc_rm_%d", i));
0495   //     if (i != start[0])
0496   //     {
0497   //       hist1[start[0]]->Add(hist1[i], 1);
0498   //     }
0499   //   }
0500   //   i++;
0501   // }
0502   //
0503   // TH2 *h2_cemc_mean[m_ServerSet.size()];
0504   // start[1] = -1;
0505   // i = 0;
0506   // for (auto server = ServerBegin(); server != ServerEnd(); ++server)
0507   //{
0508   //   h2_cemc_mean[i] = (TH2 *) cl->getHisto(*server, "h2_cemc_mean");
0509   //   if (h2_cemc_mean[i] && start[1] == -1)
0510   //   {
0511   //     start[1] = i;
0512   //   }
0513   //   if (start[1] > -1 && h2_cemc_mean[i])
0514   //   {
0515   //     h2_cemc_mean[i]->SetName(Form("h2_cemc_mean_%d", i));
0516   //     if (i != start[1])
0517   //     {
0518   //       h2_cemc_mean[start[1]]->Add(h2_cemc_mean[i], 1);
0519   //     }
0520   //   }
0521   //   i++;
0522   // }
0523   //
0524   // TH1 *h_event[m_ServerSet.size()];
0525   // TH1 *h_eventSource[m_ServerSet.size()];
0526   // start[2] = -1;
0527   // float maxEvent = 1;
0528   // i = 0;
0529   // for (auto server = ServerBegin(); server != ServerEnd(); ++server)
0530   //{
0531   //   h_eventSource[i] = cl->getHisto(*server, "h1_event");
0532   //   if (h_eventSource[i] && start[2] == -1)
0533   //   {
0534   //     start[2] = i;
0535   //   }
0536   //   if (start[2] > -1 && h_eventSource[i])
0537   //   {
0538   //     h_event[i] =  h_eventSource[i]->Clone();
0539   //
0540   //     h_event[i]->SetName(Form("h1_event_%d", i));
0541   //     if (/*i != start[2]*/ h_event[i]->GetEntries() > maxEvent)
0542   //     {
0543   //       // h_event[start[2]] -> Add(h_event[i],1);
0544   //       maxEvent = h_event[i]->GetEntries();
0545   //     }
0546   //   }
0547   //   i++;
0548   // }
0549 
0550   if (!gROOT->FindObject("CemcMon1"))
0551   {
0552     MakeCanvas("CemcMon1");
0553   }
0554 
0555   if (deadservercount == Nservers)
0556   {
0557     DrawDeadServer(transparent[0]);
0558     TC[0]->SetEditable(false);
0559     return -1;
0560   }
0561 
0562   // TH1 *adcCount[m_ServerSet.size()];
0563   // start[3] = -1;
0564   // i = 0;
0565   // for (auto server = ServerBegin(); server != ServerEnd(); ++server)
0566   //{
0567   //   adcCount[i] = cl->getHisto(*server, "h1_cemc_adc");
0568   //
0569   //   if (adcCount[i] && start[3] == -1)
0570   //   {
0571   //     start[3] = i;
0572   //   }
0573   //   if (start[3] > -1 && adcCount[i])
0574   //   {
0575   //     adcCount[i]->SetName(Form("h1_cemc_adc_%d", i));
0576   //     if (i != start[3])
0577   //     {
0578   //       adcCount[start[3]]->Add(adcCount[i], 1);
0579   //     }
0580   //   }
0581   // }
0582 
0583   // if (maxEvent > 0)
0584   //{
0585   //   if (maxEvent > templateDepth)
0586   //   {
0587   //     h2_cemc_mean[start[1]]->Scale(1. / templateDepth);
0588   //   }
0589   //   else
0590   //   {
0591   //     h2_cemc_mean[start[1]]->Scale(1. / maxEvent);
0592   //   }
0593   //   if (adcCount[start[3]]->GetMean())
0594   //   {
0595   //     h2_cemc_mean[start[1]]->Scale(1. / adcCount[start[3]]->GetMean());
0596   //   }
0597   //
0598   //   if (adcCount[start[3]]->GetMean())
0599   //   {
0600   //     hist1[start[0]]->Scale(1. / adcCount[start[3]]->GetMean());
0601   //   }
0602   //
0603   //   for (int k = 0; k < nTowersEta; k++)
0604   //   {
0605   //     for (int j = 0; j < nTowersPhi; j++)
0606   //     {
0607   //       // if(k < 8) continue;
0608   //       if (h2_cemc_mean[start[1]]->GetBinContent(k + 1, j + 1) < 0.75 && hist1[start[0]]->GetBinContent(k + 1, j + 1) < 0.75)
0609   //       {
0610   //         hist1[start[0]]->SetBinContent(k + 1, j + 1, h2_cemc_mean[start[1]]->GetBinContent(k + 1, j + 1));
0611   //       }
0612   //       else
0613   //       {
0614   //         hist1[start[0]]->SetBinContent(k + 1, j + 1, hist1[start[0]]->GetBinContent(k + 1, j + 1) / h2_cemc_mean[start[1]]->GetBinContent(k + 1, j + 1));
0615   //       }
0616   //     }
0617   //   }
0618   // }
0619   // else
0620   //{
0621   //   for (int k = 0; k < nTowersEta; k++)
0622   //   {
0623   //     for (int j = 0; j < nTowersPhi; j++)
0624   //     {
0625   //       // if(k < 8) continue;
0626   //
0627   //       hist1[start[0]]->SetBinContent(k + 1, j + 1, 0);
0628   //     }
0629   //   }
0630   // }
0631 
0632   TC[0]->SetEditable(true);
0633   TC[0]->Clear("D");
0634   Pad[0]->cd();
0635 
0636   // hist1[start[0]]->GetXaxis()->SetTitle("eta index");
0637   // hist1[start[0]]->GetYaxis()->SetTitle("phi index");
0638   // hist1[start[0]]->GetZaxis()->SetTitle("Tower Running Mean/ Histogram Running Mean");
0639   // hist1[start[0]]->GetXaxis()->CenterTitle();
0640   // hist1[start[0]]->GetYaxis()->CenterTitle();
0641   // hist1[start[0]]->GetZaxis()->CenterTitle();
0642   // hist1[start[0]]->GetXaxis()->SetNdivisions(12, kFALSE);
0643   // hist1[start[0]]->GetYaxis()->SetNdivisions(32, kFALSE);
0644   //
0645   // float tsize = 0.03;
0646   // hist1[start[0]]->GetXaxis()->SetLabelSize(tsize);
0647   // hist1[start[0]]->GetYaxis()->SetLabelSize(tsize);
0648   // hist1[start[0]]->GetYaxis()->SetTitleOffset(1.4);
0649   // hist1[start[0]]->GetZaxis()->SetLabelSize(tsize);
0650   // hist1[start[0]]->GetXaxis()->SetTitleSize(tsize);
0651   // hist1[start[0]]->GetYaxis()->SetTitleSize(tsize);
0652   // hist1[start[0]]->GetXaxis()->SetTickLength(0.02);
0653   h_cemc_datahits->Divide(h2_template_hit);
0654   h_cemc_datahits->GetXaxis()->SetTitle("eta index");
0655   h_cemc_datahits->GetYaxis()->SetTitle("phi index");
0656   h_cemc_datahits->GetZaxis()->SetTitle("Tower Running Mean/Tower reference");
0657   h_cemc_datahits->GetXaxis()->CenterTitle();
0658   h_cemc_datahits->GetYaxis()->CenterTitle();
0659   h_cemc_datahits->GetZaxis()->CenterTitle();
0660   h_cemc_datahits->GetXaxis()->SetNdivisions(12, kFALSE);
0661   h_cemc_datahits->GetYaxis()->SetNdivisions(32, kFALSE);
0662 
0663   float tsize = 0.03;
0664   h_cemc_datahits->GetXaxis()->SetLabelSize(tsize);
0665   h_cemc_datahits->GetYaxis()->SetLabelSize(tsize);
0666   h_cemc_datahits->GetYaxis()->SetTitleOffset(1.4);
0667   h_cemc_datahits->GetZaxis()->SetLabelSize(tsize);
0668   h_cemc_datahits->GetXaxis()->SetTitleSize(tsize);
0669   h_cemc_datahits->GetYaxis()->SetTitleSize(tsize);
0670   h_cemc_datahits->GetXaxis()->SetTickLength(0.02);
0671 
0672   gPad->SetTopMargin(0.08);
0673   gPad->SetBottomMargin(0.07);
0674   gPad->SetLeftMargin(0.08);
0675   gPad->SetRightMargin(0.12);
0676 
0677   // modify palette to black, green, and red
0678   // Int_t palette[3] = {kGray + 2, 8, 2};
0679   Int_t palette[4] = {kBlack, kBlue, 8, 2};
0680   cemcStyle->SetPalette(4, palette);
0681   gROOT->SetStyle("cemcStyle");
0682   gROOT->ForceStyle();
0683   gStyle->SetPalette(4, palette);
0684   double_t levels[5] = {0, 0.01, 0.5, 2, 4};
0685   h_cemc_datahits->GetZaxis()->SetRangeUser(0, 4);
0686   h_cemc_datahits->SetContour(5, levels);
0687   gStyle->SetOptStat(0);
0688   h_cemc_datahits->DrawCopy("colz");
0689   // h2_cemc_mean[start[1]]->DrawCopy("colz");
0690   TLine line_sector[32];
0691   for (int i_line = 0; i_line < 32; i_line++)
0692   {
0693     line_sector[i_line] = TLine(0, (i_line + 1) * 8, 96, (i_line + 1) * 8);
0694     line_sector[i_line].SetLineColor(1);
0695     line_sector[i_line].SetLineWidth(1);
0696     line_sector[i_line].SetLineStyle(1);
0697   }
0698 
0699   const int numVertDiv = 12;
0700   int dEI = 96 / numVertDiv;
0701   TLine l_board[numVertDiv - 1];
0702   for (int il = 1; il < numVertDiv; il++)
0703   {
0704     l_board[il - 1] = TLine(dEI * il, 0, dEI * il, 256);
0705     l_board[il - 1].SetLineColor(1);
0706     l_board[il - 1].SetLineWidth(1);
0707     l_board[il - 1].SetLineStyle(1);
0708     if (il == 6)
0709     {
0710       l_board[il - 1].SetLineWidth(2);
0711     }
0712   }
0713 
0714   for (int i_line = 0; i_line < 32; i_line++)
0715   {
0716     line_sector[i_line].DrawLine(0, (i_line + 1) * 8, 96, (i_line + 1) * 8);
0717   }
0718 
0719   for (int il = 1; il < numVertDiv; il++)
0720   {
0721     l_board[il - 1].DrawLine(dEI * il, 0, dEI * il, 256);
0722   }
0723 
0724   // FindHotTower(warning[0], hist1[start[0]]);
0725   FindHotTower(warning[0], h_cemc_datahits, true);
0726   TText PrintRun;
0727   PrintRun.SetTextFont(62);
0728   PrintRun.SetTextSize(0.03);
0729   PrintRun.SetNDC();          // set to normalized coordinates
0730   PrintRun.SetTextAlign(23);  // center/top alignment
0731   std::ostringstream runnostream;
0732   std::ostringstream runnostream2;
0733   std::ostringstream runnostream3;
0734   std::string runstring;
0735   std::pair<time_t, int> evttime = cl->EventTime("CURRENT");
0736   // fill run number and event time into string
0737   runnostream << ThisName << ": tower occupancy running mean/template";
0738   runnostream2 << " threshold: 100ADC, Run " << cl->RunNumber() << ", Event: " << avgevents;
0739   runnostream3 << "Time: " << ctime(&evttime.first);
0740 
0741   transparent[0]->cd();
0742   runstring = runnostream.str();
0743   PrintRun.SetTextColor(evttime.second);
0744   PrintRun.DrawText(0.5, 0.99, runstring.c_str());
0745   runstring = runnostream2.str();
0746   PrintRun.DrawText(0.5, 0.96, runstring.c_str());
0747   runstring = runnostream3.str();
0748   PrintRun.DrawText(0.5, 0.93, runstring.c_str());
0749 
0750   TC[0]->Update();
0751   TC[0]->Show();
0752   TC[0]->SetEditable(false);
0753   if (save)
0754   {
0755     TC[0]->SaveAs("plots/towerHits.pdf");
0756   }
0757   return 0;
0758 }
0759 
0760 int CemcMonDraw::DrawAllTrigHits(const std::string & /* what */)
0761 {
0762   OnlMonClient *cl = OnlMonClient::instance();
0763   // watch the absolute insanity as we merge all these
0764   // histograms from across seven different machines
0765   if (!h_cemc_datahits)
0766   {
0767     h_cemc_datahits = new TH2D("h_cemc_datahits", "", 96, 0, 96, 256, 0, 256);
0768   }
0769   else
0770   {
0771     h_cemc_datahits->Reset();
0772   }
0773   TH2 *htmp2d;
0774   int deadservercount = 0;
0775   int Nservers = 0;
0776   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
0777   {
0778     Nservers++;
0779     htmp2d = (TH2 *) cl->getHisto(*server, "h2_cemc_rmhits_alltrig");
0780 
0781     if (htmp2d)
0782     {
0783       h_cemc_datahits->Add(htmp2d);
0784     }
0785     else
0786     {
0787       deadservercount++;
0788     }
0789   }
0790   int avgevents = 0;
0791   int neventhist = 0;
0792   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
0793   {
0794     TH1 *h_eventSource = cl->getHisto(*server, "h1_event");
0795     if (h_eventSource)
0796     {
0797       avgevents += h_eventSource->GetEntries();
0798       neventhist++;
0799     }
0800   }
0801   if (neventhist)
0802   {
0803     avgevents /= neventhist;
0804   }
0805 
0806   if (!gROOT->FindObject("CemcAllTrigHits"))
0807   {
0808     MakeCanvas("CemcAllTrigHits");
0809   }
0810 
0811   if (deadservercount == Nservers)
0812   {
0813     DrawDeadServer(transparent[6]);
0814     TC[6]->SetEditable(false);
0815     return -1;
0816   }
0817 
0818   TC[6]->SetEditable(true);
0819   TC[6]->Clear("D");
0820   Pad[15]->cd();
0821 
0822   h_cemc_datahits->Divide(h2_template_hit_alltrig);
0823 
0824   h_cemc_datahits->GetXaxis()->SetTitle("eta index");
0825   h_cemc_datahits->GetYaxis()->SetTitle("phi index");
0826   h_cemc_datahits->GetZaxis()->SetTitle("Tower Running Mean with all trig");
0827   h_cemc_datahits->GetXaxis()->CenterTitle();
0828   h_cemc_datahits->GetYaxis()->CenterTitle();
0829   h_cemc_datahits->GetZaxis()->CenterTitle();
0830   h_cemc_datahits->GetXaxis()->SetNdivisions(12, kFALSE);
0831   h_cemc_datahits->GetYaxis()->SetNdivisions(32, kFALSE);
0832 
0833   float tsize = 0.03;
0834   h_cemc_datahits->GetXaxis()->SetLabelSize(tsize);
0835   h_cemc_datahits->GetYaxis()->SetLabelSize(tsize);
0836   h_cemc_datahits->GetYaxis()->SetTitleOffset(1.4);
0837   h_cemc_datahits->GetZaxis()->SetLabelSize(tsize);
0838   h_cemc_datahits->GetXaxis()->SetTitleSize(tsize);
0839   h_cemc_datahits->GetYaxis()->SetTitleSize(tsize);
0840   h_cemc_datahits->GetXaxis()->SetTickLength(0.02);
0841   h_cemc_datahits->GetZaxis()->SetTitleOffset(1.6);
0842 
0843   gPad->SetTopMargin(0.08);
0844   gPad->SetBottomMargin(0.07);
0845   gPad->SetLeftMargin(0.08);
0846   gPad->SetRightMargin(0.2);
0847 
0848   // modify palette to black, green, and red
0849 
0850   Int_t palette[4] = {kBlack, kBlue, 8, 2};
0851   cemcStyle->SetPalette(4, palette);
0852   gROOT->SetStyle("cemcStyle");
0853   gROOT->ForceStyle();
0854   gStyle->SetPalette(4, palette);
0855   double_t levels[5] = {0, 0.01, 0.5, 2, 4};
0856   h_cemc_datahits->GetZaxis()->SetRangeUser(0, 4);
0857   h_cemc_datahits->SetContour(5, levels);
0858 
0859   gStyle->SetOptStat(0);
0860   h_cemc_datahits->DrawCopy("colz");
0861   // h2_cemc_mean[start[1]]->DrawCopy("colz");
0862   TLine line_sector[32];
0863   for (int i_line = 0; i_line < 32; i_line++)
0864   {
0865     line_sector[i_line] = TLine(0, (i_line + 1) * 8, 96, (i_line + 1) * 8);
0866     line_sector[i_line].SetLineColor(1);
0867     line_sector[i_line].SetLineWidth(1);
0868     line_sector[i_line].SetLineStyle(1);
0869   }
0870 
0871   const int numVertDiv = 12;
0872   int dEI = 96 / numVertDiv;
0873   TLine l_board[numVertDiv - 1];
0874   for (int il = 1; il < numVertDiv; il++)
0875   {
0876     l_board[il - 1] = TLine(dEI * il, 0, dEI * il, 256);
0877     l_board[il - 1].SetLineColor(1);
0878     l_board[il - 1].SetLineWidth(1);
0879     l_board[il - 1].SetLineStyle(1);
0880     if (il == 6)
0881     {
0882       l_board[il - 1].SetLineWidth(2);
0883     }
0884   }
0885 
0886   for (int i_line = 0; i_line < 32; i_line++)
0887   {
0888     line_sector[i_line].DrawLine(0, (i_line + 1) * 8, 96, (i_line + 1) * 8);
0889   }
0890 
0891   for (int il = 1; il < numVertDiv; il++)
0892   {
0893     l_board[il - 1].DrawLine(dEI * il, 0, dEI * il, 256);
0894   }
0895 
0896   FindHotTower(warning[2], h_cemc_datahits, true);
0897   TText PrintRun;
0898   PrintRun.SetTextFont(62);
0899   PrintRun.SetTextSize(0.03);
0900   PrintRun.SetNDC();          // set to normalized coordinates
0901   PrintRun.SetTextAlign(23);  // center/top alignment
0902   std::ostringstream runnostream;
0903   std::ostringstream runnostream2;
0904   std::ostringstream runnostream3;
0905   std::string runstring;
0906   std::pair<time_t, int> evttime = cl->EventTime("CURRENT");
0907   // fill run number and event time into string
0908   runnostream << ThisName << ": tower occupancy/template running mean with all trig";
0909   runnostream2 << " threshold: 100ADC, Run " << cl->RunNumber();
0910   runnostream3 << "Time: " << ctime(&evttime.first);
0911 
0912   transparent[6]->cd();
0913   runstring = runnostream.str();
0914   PrintRun.SetTextColor(evttime.second);
0915   PrintRun.DrawText(0.5, 0.99, runstring.c_str());
0916   runstring = runnostream2.str();
0917   PrintRun.DrawText(0.5, 0.96, runstring.c_str());
0918   runstring = runnostream3.str();
0919   PrintRun.DrawText(0.5, 0.93, runstring.c_str());
0920 
0921   TC[6]->Update();
0922   TC[6]->Show();
0923   TC[6]->SetEditable(false);
0924   if (save)
0925   {
0926     TC[6]->SaveAs("plots/towerHits.pdf");
0927   }
0928   return 0;
0929 }
0930 
0931 int CemcMonDraw::DrawNoiseRMS(const std::string & /* what */)
0932 {
0933   // 9 22 3
0934   OnlMonClient *cl = OnlMonClient::instance();
0935   // watch the absolute insanity as we merge all these
0936   // histograms from across seven different machines
0937   if (!h2_noiserms)
0938   {
0939     h2_noiserms = new TH2D("h2_noiserms", "", 96, 0, 96, 256, 0, 256);
0940   }
0941   else
0942   {
0943     h2_noiserms->Reset();
0944   }
0945   if (!p2_noiserms)
0946   {
0947     p2_noiserms = new TProfile2D("p2_noiserms", "", 96, 0, 96, 256, 0, 256, "S");
0948   }
0949   else
0950   {
0951     p2_noiserms->Reset();
0952   }
0953   TProfile2D *htmp2d;
0954   int deadservercount = 0;
0955   int Nservers = 0;
0956   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
0957   {
0958     Nservers++;
0959     htmp2d = (TProfile2D *) cl->getHisto(*server, "p2_pre_post");
0960 
0961     if (htmp2d)
0962     {
0963       p2_noiserms->Add(htmp2d);
0964     }
0965     else
0966     {
0967       deadservercount++;
0968     }
0969   }
0970 
0971   if (!gROOT->FindObject("CemcNoiseRMS"))
0972   {
0973     MakeCanvas("CemcNoiseRMS");
0974   }
0975 
0976   if (deadservercount == Nservers)
0977   {
0978     DrawDeadServer(transparent[9]);
0979     TC[9]->SetEditable(false);
0980     return -1;
0981   }
0982 
0983   // loop over p2_noiserms and set the error to h2_cemc_noiserms
0984   for (int i = 1; i <= p2_noiserms->GetNbinsX(); i++)
0985   {
0986     for (int j = 1; j <= p2_noiserms->GetNbinsY(); j++)
0987     {
0988       double error = p2_noiserms->GetBinError(i, j);
0989       h2_noiserms->SetBinContent(i, j, error);
0990     }
0991   }
0992 
0993   TC[9]->SetEditable(true);
0994   TC[9]->Clear("D");
0995   Pad[22]->cd();
0996 
0997   h2_noiserms->GetXaxis()->SetTitle("eta index");
0998   h2_noiserms->GetYaxis()->SetTitle("phi index");
0999   h2_noiserms->GetZaxis()->SetTitle("Tower Noise RMS with all trig");
1000   h2_noiserms->GetXaxis()->CenterTitle();
1001   h2_noiserms->GetYaxis()->CenterTitle();
1002   h2_noiserms->GetZaxis()->CenterTitle();
1003   h2_noiserms->GetXaxis()->SetNdivisions(12, kFALSE);
1004   h2_noiserms->GetYaxis()->SetNdivisions(32, kFALSE);
1005 
1006   float tsize = 0.03;
1007   h2_noiserms->GetXaxis()->SetLabelSize(tsize);
1008   h2_noiserms->GetYaxis()->SetLabelSize(tsize);
1009   h2_noiserms->GetYaxis()->SetTitleOffset(1.4);
1010   h2_noiserms->GetZaxis()->SetLabelSize(tsize);
1011   h2_noiserms->GetXaxis()->SetTitleSize(tsize);
1012   h2_noiserms->GetYaxis()->SetTitleSize(tsize);
1013   h2_noiserms->GetXaxis()->SetTickLength(0.02);
1014   h2_noiserms->GetZaxis()->SetTitleOffset(1.6);
1015 
1016   gPad->SetTopMargin(0.08);
1017   gPad->SetBottomMargin(0.07);
1018   gPad->SetLeftMargin(0.08);
1019   gPad->SetRightMargin(0.2);
1020 
1021   gStyle->SetPalette(57);
1022   h2_noiserms->GetZaxis()->SetRangeUser(0, 100);
1023 
1024   gStyle->SetOptStat(0);
1025   h2_noiserms->DrawCopy("colz");
1026   TLine line_sector[32];
1027   for (int i_line = 0; i_line < 32; i_line++)
1028   {
1029     line_sector[i_line] = TLine(0, (i_line + 1) * 8, 96, (i_line + 1) * 8);
1030     line_sector[i_line].SetLineColor(1);
1031     line_sector[i_line].SetLineWidth(1);
1032     line_sector[i_line].SetLineStyle(1);
1033   }
1034 
1035   const int numVertDiv = 12;
1036   int dEI = 96 / numVertDiv;
1037   TLine l_board[numVertDiv - 1];
1038   for (int il = 1; il < numVertDiv; il++)
1039   {
1040     l_board[il - 1] = TLine(dEI * il, 0, dEI * il, 256);
1041     l_board[il - 1].SetLineColor(1);
1042     l_board[il - 1].SetLineWidth(1);
1043     l_board[il - 1].SetLineStyle(1);
1044     if (il == 6)
1045     {
1046       l_board[il - 1].SetLineWidth(2);
1047     }
1048   }
1049 
1050   for (int i_line = 0; i_line < 32; i_line++)
1051   {
1052     line_sector[i_line].DrawLine(0, (i_line + 1) * 8, 96, (i_line + 1) * 8);
1053   }
1054 
1055   for (int il = 1; il < numVertDiv; il++)
1056   {
1057     l_board[il - 1].DrawLine(dEI * il, 0, dEI * il, 256);
1058   }
1059   FindGainMode(warning[3], h2_noiserms);
1060   // FindHotTower(warning[3], h_cemc_datahits, true);
1061   TText PrintRun;
1062   PrintRun.SetTextFont(62);
1063   PrintRun.SetTextSize(0.03);
1064   PrintRun.SetNDC();          // set to normalized coordinates
1065   PrintRun.SetTextAlign(23);  // center/top alignment
1066   std::ostringstream runnostream;
1067   std::ostringstream runnostream2;
1068   std::ostringstream runnostream3;
1069   std::string runstring;
1070   std::pair<time_t, int> evttime = cl->EventTime("CURRENT");
1071   // fill run number and event time into string
1072   runnostream << ThisName << ": tower negative post - pre RMS all trig";
1073   runnostream2 << "Run " << cl->RunNumber();
1074   runnostream3 << "Time: " << ctime(&evttime.first);
1075 
1076   transparent[9]->cd();
1077   runstring = runnostream.str();
1078   PrintRun.SetTextColor(evttime.second);
1079   PrintRun.DrawText(0.5, 0.99, runstring.c_str());
1080   runstring = runnostream2.str();
1081   PrintRun.DrawText(0.5, 0.96, runstring.c_str());
1082   runstring = runnostream3.str();
1083   PrintRun.DrawText(0.5, 0.93, runstring.c_str());
1084 
1085   TC[9]->Update();
1086   TC[9]->Show();
1087   TC[9]->SetEditable(false);
1088   if (save)
1089   {
1090     TC[9]->SaveAs("plots/towerNoiseRMS.pdf");
1091   }
1092   return 0;
1093 }
1094 
1095 int CemcMonDraw::DrawSecond(const std::string & /* what */)
1096 {
1097   OnlMonClient *cl = OnlMonClient::instance();
1098   gStyle->SetOptStat(0);
1099   TH1 *h_event[m_ServerSet.size()];
1100   TH1 *h_eventSource[m_ServerSet.size()];
1101   int start[4];
1102   start[0] = -1;
1103   float maxEvent = -1;
1104   int i = 0;
1105   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1106   {
1107     h_eventSource[i] = cl->getHisto(*server, "h1_event");
1108     if (h_eventSource[i] && start[0] == -1)
1109     {
1110       start[0] = i;
1111     }
1112     if (start[0] > -1 && h_eventSource[i])
1113     {
1114       h_event[i] = (TH1 *) h_eventSource[i]->Clone();
1115       h_event[i]->SetName(Form("h1_event_%d", i));
1116       if (/*i != start[0] &&*/ h_event[i]->GetEntries() > maxEvent)
1117       {
1118         // h_event[start[0]] -> Add(h_event[i],1);
1119         maxEvent = h_event[i]->GetEntries();
1120       }
1121     }
1122     i++;
1123   }
1124 
1125   TH1 *h1_packet_number[m_ServerSet.size()];
1126   start[1] = -1;
1127   i = 0;
1128   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1129   {
1130     h1_packet_number[i] = cl->getHisto(*server, "h1_packet_number");
1131     if (h1_packet_number[i] && start[1] == -1)
1132     {
1133       start[1] = i;
1134     }
1135     if (start[1] > -1 && h1_packet_number[i])
1136     {
1137       h1_packet_number[i]->SetName(Form("h1_papcket_number_%d", i));
1138       if (i != start[1])
1139       {
1140         h1_packet_number[start[1]]->Add(h1_packet_number[i], 1);
1141       }
1142     }
1143     i++;
1144   }
1145 
1146   TH1 *h1_packet_length[m_ServerSet.size()];
1147   start[2] = -1;
1148   i = 0;
1149   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1150   {
1151     h1_packet_length[i] = cl->getHisto(*server, "h1_packet_length");
1152     if (h1_packet_length[i] && start[2] == -1)
1153     {
1154       start[2] = i;
1155     }
1156     if (start[2] > -1 && h1_packet_length[i])
1157     {
1158       h1_packet_length[i]->SetName(Form("h1_papcket_length_%d", i));
1159       if (i != start[2])
1160       {
1161         h1_packet_length[start[2]]->Add(h1_packet_length[i], 1);
1162       }
1163     }
1164     i++;
1165   }
1166 
1167   TH1 *h1_packet_chans[m_ServerSet.size()];
1168   start[3] = -1;
1169   i = 0;
1170   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1171   {
1172     h1_packet_chans[i] = cl->getHisto(*server, "h1_packet_chans");
1173     if (h1_packet_chans[i] && start[3] == -1)
1174     {
1175       start[3] = i;
1176     }
1177     if (start[3] > -1 && h1_packet_chans[i])
1178     {
1179       h1_packet_chans[i]->SetName(Form("h1_papcket_chans_%d", i));
1180       if (i != start[3])
1181       {
1182         h1_packet_chans[start[3]]->Add(h1_packet_chans[i], 1);
1183       }
1184     }
1185     i++;
1186   }
1187 
1188   if (start[0] < 0 || start[1] < 0 || start[2] < 0)
1189   {
1190     DrawDeadServer(transparent[1]);
1191     TC[1]->SetEditable(false);
1192     return -1;
1193   }
1194 
1195   // h_event[start[0]] -> Scale(1./divisor);
1196 
1197   if (maxEvent > 0)
1198   {
1199     // h1_packet_number[start[1]] -> Scale(1./h_event[start[0]] -> GetBinContent(1));
1200     // h1_packet_length[start[2]] -> Scale(1./h_event[start[0]] -> GetBinContent(1));
1201     // h1_packet_chans[start[3]] -> Scale(1./h_event[start[0]] -> GetBinContent(1));
1202     h1_packet_number[start[1]]->Scale(1. / maxEvent);
1203     h1_packet_length[start[2]]->Scale(1. / maxEvent);
1204     h1_packet_chans[start[3]]->Scale(1. / maxEvent);
1205   }
1206 
1207   if (!gROOT->FindObject("CemcMon2"))
1208   {
1209     MakeCanvas("CemcMon2");
1210   }
1211 
1212   TC[1]->SetEditable(true);
1213   TC[1]->Clear("D");
1214 
1215   TLine *one = new TLine(6000.5, 1, 6128.5, 1);
1216   one->SetLineStyle(7);
1217 
1218   TLine *goodSize = new TLine(6000.5, 1565, 6128.5, 1565);
1219   goodSize->SetLineStyle(7);
1220 
1221   TLine *goodChans = new TLine(6000.5, 192, 6128.5, 192);
1222   goodChans->SetLineStyle(7);
1223 
1224   float param = 0.95;
1225   // float param = 0.99;
1226 
1227   TLegend *leg = new TLegend(0.3, 0.20, 0.95, 0.50);
1228   leg->SetFillStyle(0);
1229   leg->SetBorderSize(0);
1230 
1231   TLine *warnLineOne = new TLine(6000.5, param * 1, 6128.5, param * 1);
1232   warnLineOne->SetLineStyle(7);
1233   warnLineOne->SetLineColor(2);
1234 
1235   leg->AddEntry(warnLineOne, Form("%g%% Threshold", param * 100), "l");
1236 
1237   TLine *warnLineOneS = new TLine(6000.5, param * 1, 6128.5, param * 1);
1238   warnLineOneS->SetLineStyle(10);
1239   warnLineOneS->SetLineColor(2);
1240 
1241   leg->AddEntry(warnLineOneS, Form("%g%% Threshold, High Eta, South", 100 * param), "l");
1242 
1243   TLine *warnLineSize = new TLine(6000.5, param * 5981., 6128.5, param * 5981.);
1244   warnLineSize->SetLineStyle(7);
1245   warnLineSize->SetLineColor(2);
1246 
1247   TLine *warnLineSizeS = new TLine(6000.5, param * 3991., 6128.5, param * 3991.);
1248   warnLineSizeS->SetLineStyle(10);
1249   warnLineSizeS->SetLineColor(2);
1250 
1251   TLine *warnLineChans = new TLine(6000.5, param * 192., 6128.5, param * 192.);
1252   warnLineChans->SetLineStyle(7);
1253   warnLineChans->SetLineColor(2);
1254 
1255   TLine *warnLineChansS = new TLine(6000.5, param * 128., 6128.5, param * 128.);
1256   warnLineChansS->SetLineStyle(10);
1257   warnLineChansS->SetLineColor(2);
1258 
1259   Pad[1]->cd();
1260   float tsize = 0.08;
1261   h1_packet_number[start[1]]->GetYaxis()->SetRangeUser(0.0, 1.3);
1262   std::vector<std::vector<int>> badPackets;
1263   badPackets.push_back(getBadPackets(h1_packet_number[start[1]], 0, param));
1264 
1265   h1_packet_number[start[1]]->GetXaxis()->SetNdivisions(510, kTRUE);
1266   h1_packet_number[start[1]]->GetXaxis()->SetTitle("packet #");
1267   h1_packet_number[start[1]]->GetYaxis()->SetTitle("% Of Events Present");
1268   h1_packet_number[start[1]]->GetXaxis()->SetLabelSize(tsize - 0.01);
1269   h1_packet_number[start[1]]->GetYaxis()->SetLabelSize(tsize - 0.01);
1270   h1_packet_number[start[1]]->GetXaxis()->SetTitleSize(tsize - 0.01);
1271   h1_packet_number[start[1]]->GetYaxis()->SetTitleSize(tsize - 0.01);
1272   h1_packet_number[start[1]]->GetXaxis()->SetTitleOffset(1);
1273   gPad->SetBottomMargin(0.16);
1274   gPad->SetLeftMargin(0.16);
1275   gPad->SetRightMargin(0.05);
1276   gPad->SetLeftMargin(0.15);
1277   gPad->SetTicky();
1278   gPad->SetTickx();
1279   h1_packet_number[start[1]]->DrawCopy("hist");
1280   one->Draw("same");
1281   warnLineOne->Draw("same");
1282   leg->Draw("same");
1283 
1284   Pad[2]->cd();
1285   badPackets.push_back(getBadPackets(h1_packet_length[start[2]], 1, 0));  // Not cutting on packet length anymore because of zero suppression
1286 
1287   h1_packet_length[start[2]]->GetXaxis()->SetNdivisions(510, kTRUE);
1288   h1_packet_length[start[2]]->GetXaxis()->SetTitle("packet #");
1289   h1_packet_length[start[2]]->GetYaxis()->SetTitle("Average Packet Size");
1290   h1_packet_length[start[2]]->GetXaxis()->SetLabelSize(tsize - .01);
1291   h1_packet_length[start[2]]->GetYaxis()->SetLabelSize(tsize);
1292   h1_packet_length[start[2]]->GetXaxis()->SetTitleSize(tsize - .01);
1293   h1_packet_length[start[2]]->GetYaxis()->SetTitleSize(tsize);
1294   h1_packet_length[start[2]]->GetXaxis()->SetTitleOffset(1);
1295   h1_packet_length[start[2]]->GetYaxis()->SetTitleOffset(0.9);
1296   h1_packet_length[start[2]]->GetYaxis()->SetRangeUser(0, 4000);
1297   gPad->SetBottomMargin(0.16);
1298   gPad->SetLeftMargin(0.16);
1299   gPad->SetRightMargin(0.05);
1300   gPad->SetLeftMargin(0.15);
1301   gPad->SetTicky();
1302   gPad->SetTickx();
1303   h1_packet_length[start[2]]->DrawCopy("hist");
1304   goodSize->Draw("same");
1305   // warnLineSize -> Draw("same");
1306   // warnLineSizeS -> Draw("same");
1307 
1308   Pad[3]->cd();
1309   h1_packet_chans[start[3]]->GetYaxis()->SetRangeUser(0, 212);
1310   badPackets.push_back(getBadPackets(h1_packet_chans[start[3]], 2, param));
1311   h1_packet_chans[start[3]]->GetXaxis()->SetNdivisions(510, kTRUE);
1312   h1_packet_chans[start[3]]->GetXaxis()->SetTitle("packet #");
1313   h1_packet_chans[start[3]]->GetYaxis()->SetTitle("Average # of Channels");
1314   h1_packet_chans[start[3]]->GetXaxis()->SetLabelSize(tsize - .01);
1315   h1_packet_chans[start[3]]->GetYaxis()->SetLabelSize(tsize);
1316   h1_packet_chans[start[3]]->GetXaxis()->SetTitleSize(tsize - .01);
1317   h1_packet_chans[start[3]]->GetYaxis()->SetTitleSize(tsize);
1318   h1_packet_chans[start[3]]->GetXaxis()->SetTitleOffset(0.8);
1319   h1_packet_chans[start[3]]->GetYaxis()->SetTitleOffset(0.8);
1320   gPad->SetBottomMargin(0.16);
1321   gPad->SetLeftMargin(0.16);
1322   gPad->SetRightMargin(0.05);
1323   gPad->SetLeftMargin(0.15);
1324   gPad->SetTicky();
1325   gPad->SetTickx();
1326   h1_packet_chans[start[3]]->DrawCopy("hist");
1327   goodChans->Draw("same");
1328   warnLineChans->Draw("same");
1329   warnLineChansS->Draw("same");
1330 
1331   warning[1]->cd();
1332   TLegend *badPacks = new TLegend(0, 1 / 4., 1, 0.9);
1333   TPaveText *title = new TPaveText(0, 0.9, 1, 0.95);
1334   title->AddText("Bad Packets");
1335   badPacks->SetNColumns(8);
1336   badPacks->SetTextSize(0.03);
1337   float badboys = 0;
1338   if (maxEvent > 0)
1339   {
1340     for (int k = 0; k < 3; k++)
1341     {
1342       for (int j = 0; j < (int) badPackets[k].size(); j++)
1343       {
1344         // there's most certainly a better way to do this but it's 5:00 on day 5 of owl shift
1345         // just want to prevent a packet showing up multiple times and crowding the screen
1346         if (badPackets[k][j] == 0)
1347         {
1348           continue;  // need this to prevent seg faulting
1349         }
1350 
1351         if (k == 0)
1352         {
1353           badPacks->AddEntry("", Form("%d", badPackets[k][j]), "");
1354           badboys++;
1355         }
1356         else if (k == 1)
1357         {
1358           if (!(std::count(badPackets[k - 1].begin(), badPackets[k - 1].end(), badPackets[k][j])))
1359           {
1360             badPacks->AddEntry("", Form("%d", badPackets[k].at(j)), "");
1361             badboys++;
1362           }
1363         }
1364         else if (k == 2)
1365         {
1366           if (!(std::count(badPackets[k - 1].begin(), badPackets[k - 1].end(), badPackets[k][j])))
1367           {
1368             badPacks->AddEntry("", Form("%d", badPackets[k].at(j)), "");
1369             badboys++;
1370           }
1371           if (!(std::count(badPackets[k - 2].begin(), badPackets[k - 2].end(), badPackets[k][j])) && !(std::count(badPackets[k - 1].begin(), badPackets[k - 1].end(), badPackets[k][j])))
1372           {
1373             badPacks->AddEntry("", Form("%d", badPackets[k].at(j)), "");
1374             badboys++;
1375           }
1376         }
1377       }
1378     }
1379   }
1380   badPacks->Draw();
1381   title->Draw();
1382   TPaveText *desc = new TPaveText(0, 0, 1, 0.25);
1383   desc->AddText(Form("Currently %.2g%% of packets are reporting a problem", badboys / 128. * 100));
1384   desc->AddText("Packets will be reported bad as above for the following reasons:");
1385   desc->AddText(Form("A packet appears in less than %g %% of events", param * 100));
1386   // desc -> AddText(Form("A packet is less than %g %% of size 5981",param*100));
1387   desc->AddText(Form("A packet sees fewer than %g %% of 192 channels", param * 100));
1388   desc->Draw();
1389   TText PrintRun;
1390   PrintRun.SetTextFont(62);
1391   PrintRun.SetTextSize(0.02);
1392   PrintRun.SetNDC();          // set to normalized coordinates
1393   PrintRun.SetTextAlign(23);  // center/top alignment
1394   std::ostringstream runnostream;
1395   std::string runstring;
1396   std::ostringstream runnostream2;
1397   std::pair<time_t, int> evttime = cl->EventTime("CURRENT");
1398   // fill run number and event time into string
1399 
1400   runnostream << "Packet Information";
1401   runnostream2 << " Run " << cl->RunNumber() << ", Time: " << ctime(&evttime.first);
1402   transparent[1]->cd();
1403 
1404   runstring = runnostream.str();
1405   PrintRun.SetTextColor(evttime.second);
1406   PrintRun.DrawText(0.5, .99, runstring.c_str());
1407 
1408   runstring = runnostream2.str();
1409   PrintRun.DrawText(0.5, .966, runstring.c_str());
1410   TC[1]->Update();
1411   TC[1]->Show();
1412   TC[1]->SetEditable(false);
1413   if (save)
1414   {
1415     TC[1]->SaveAs("plots/packets.pdf");
1416   }
1417   return 0;
1418 }
1419 
1420 int CemcMonDraw::DrawThird(const std::string & /* what */)
1421 {
1422   OnlMonClient *cl = OnlMonClient::instance();
1423 
1424   TH2 *h2_waveform_twrAvg[m_ServerSet.size()];
1425   int start[3];
1426   start[0] = -1;
1427   int i = 0;
1428   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1429   {
1430     h2_waveform_twrAvg[i] = (TH2 *) cl->getHisto(*server, "h2_waveform_twrAvg");
1431     if (h2_waveform_twrAvg[i] && start[0] == -1)
1432     {
1433       start[0] = i;
1434     }
1435     if (start[0] > -1 && h2_waveform_twrAvg[i])
1436     {
1437       h2_waveform_twrAvg[i]->SetName(Form("h2_waveform_twrAvg_%d", i));
1438       h2_waveform_twrAvg[start[0]]->Add(h2_waveform_twrAvg[i], 1);
1439     }
1440     i++;
1441   }
1442 
1443   TH1 *h1_waveform_time[m_ServerSet.size()];
1444   start[1] = -1;
1445   i = 0;
1446   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1447   {
1448     h1_waveform_time[i] = cl->getHisto(*server, "h1_waveform_time");
1449     if (h1_waveform_time[i] && start[1] == -1)
1450     {
1451       start[1] = i;
1452     }
1453     if (start[1] > -1 && h1_waveform_time[i])
1454     {
1455       h1_waveform_time[i]->SetName(Form("h1_waveform_time_%d", i));
1456       h1_waveform_time[start[1]]->Add(h1_waveform_time[i], 1);
1457     }
1458     i++;
1459   }
1460 
1461   TH1 *h1_waveform_pedestal[m_ServerSet.size()];
1462   start[2] = -1;
1463   i = 0;
1464   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1465   {
1466     h1_waveform_pedestal[i] = cl->getHisto(*server, "h1_waveform_pedestal");
1467 
1468     if (h1_waveform_pedestal[i] && start[2] == -1)
1469     {
1470       start[2] = i;
1471     }
1472     if (start[2] > -1 && h1_waveform_pedestal[i])
1473     {
1474       h1_waveform_pedestal[i]->SetName(Form("h1_waveform_pedestal_%d", i));
1475       h1_waveform_pedestal[start[2]]->Add(h1_waveform_pedestal[i], 1);
1476     }
1477     i++;
1478   }
1479 
1480   if (!gROOT->FindObject("CemcMon3"))
1481   {
1482     MakeCanvas("CemcMon3");
1483   }
1484 
1485   TC[2]->SetEditable(true);
1486   TC[2]->Clear("D");
1487   Pad[4]->cd();
1488   if (start[0] < 0 || start[1] < 0 || start[2] < 0)
1489   {
1490     DrawDeadServer(transparent[2]);
1491     TC[2]->SetEditable(false);
1492     return -1;
1493   }
1494   std::cout << "start drawing" << std::endl;
1495   gStyle->SetTitleFontSize(0.03);
1496   TProfile *profiled = h2_waveform_twrAvg[start[0]]->ProfileX();
1497   float ymaxp = profiled->GetMaximum();
1498   ymaxp = ymaxp * 20 > pow(2, 14) ? pow(2, 14) : 20 * ymaxp;
1499   h2_waveform_twrAvg[start[0]]->GetYaxis()->SetRangeUser(0, ymaxp);
1500   /*
1501   TProfile *profile_y = h2_waveform_twrAvg[start[0]]->ProfileY();
1502   profile_y->Rebin(5);
1503 
1504   // Define the range
1505   double x_min = 100;
1506   double x_max = 7 * ymaxp;
1507   int n_points_in_range = 0;
1508   // Loop through the bins of the Profile Y histogram and count the bins within the range
1509   int n_bins = profile_y->GetNbinsX();
1510   for (int j = 1; j <= n_bins; ++i)
1511   {
1512     double bin_center = profile_y->GetBinCenter(j);
1513     if (profile_y->GetBinContent(j) == 0)
1514     {
1515       continue;
1516     }
1517     if (bin_center >= x_min && bin_center <= x_max)
1518     {
1519       n_points_in_range++;
1520     }
1521   }
1522 
1523   double *x_vals = new double[n_points_in_range];
1524   double *y_vals = new double[n_points_in_range];
1525 
1526   // Extract the Profile Y values and their corresponding Y positions within the range
1527   int point_index = 0;
1528   for (int j = 1; j <= n_bins; ++j)
1529   {
1530     double bin_center = profile_y->GetBinCenter(j);
1531     if (profile_y->GetBinContent(j) == 0)
1532     {
1533       continue;
1534     }
1535     if (bin_center >= x_min && bin_center <= x_max)
1536     {
1537       y_vals[point_index] = bin_center;
1538       x_vals[point_index] = profile_y->GetBinContent(i);
1539       point_index++;
1540     }
1541   }
1542   std::cout<<"start drawing 2"<<std::endl;
1543   */
1544   float tsize = 0.06;
1545   float tsize2 = 0.08;
1546 
1547   h2_waveform_twrAvg[start[0]]->GetXaxis()->SetNdivisions(16);
1548   h2_waveform_twrAvg[start[0]]->GetXaxis()->SetTitle("sample #");
1549   h2_waveform_twrAvg[start[0]]->GetYaxis()->SetTitle("Waveform ADC [ADC]");
1550   h2_waveform_twrAvg[start[0]]->GetXaxis()->SetLabelSize(tsize2);
1551   h2_waveform_twrAvg[start[0]]->GetYaxis()->SetLabelSize(tsize2 - .01);
1552   h2_waveform_twrAvg[start[0]]->GetXaxis()->SetTitleSize(tsize2);
1553   h2_waveform_twrAvg[start[0]]->GetYaxis()->SetTitleSize(tsize);
1554   h2_waveform_twrAvg[start[0]]->GetXaxis()->SetTitleOffset(1.);
1555   h2_waveform_twrAvg[start[0]]->GetYaxis()->SetTitleOffset(1.25);
1556   // float ymaxp = 5000;
1557   h2_waveform_twrAvg[start[0]]->GetYaxis()->SetRangeUser(0, ymaxp);
1558 
1559   gStyle->SetOptStat(0);
1560   gPad->SetBottomMargin(0.16);
1561   gPad->SetLeftMargin(0.16);
1562   gPad->SetRightMargin(0.05);
1563   gPad->SetLeftMargin(0.15);
1564   gPad->SetTicky();
1565   gPad->SetTickx();
1566   gStyle->SetPalette(kBird);
1567   h2_waveform_twrAvg[start[0]]->DrawCopy("colz");
1568   // over lay the profile draw only the marker
1569   /*
1570   TGraph* graph = new TGraph(n_points_in_range, x_vals, y_vals);
1571   graph->SetMarkerStyle(20);
1572   graph->SetMarkerSize(1);
1573   graph->SetMarkerColor(1);
1574   graph->Draw("P same");
1575   */
1576   TLine windowLow1(SampleLowBoundary, 0, SampleLowBoundary, ymaxp);
1577   windowLow1.SetLineWidth(3);
1578   windowLow1.DrawLine(SampleLowBoundary, 0, SampleLowBoundary, ymaxp);
1579 
1580   TLine windowHigh1(SampleHighBoundary, 0, SampleHighBoundary, ymaxp);
1581   windowHigh1.SetLineWidth(3);
1582   windowHigh1.DrawLine(SampleHighBoundary, 0, SampleHighBoundary, ymaxp);
1583 
1584   gStyle->SetPalette(57);
1585   gPad->SetLogz();
1586 
1587   TText PrintRun;
1588   PrintRun.SetTextFont(62);
1589   PrintRun.SetTextSize(0.03);
1590   PrintRun.SetNDC();          // set to normalized coordinates
1591   PrintRun.SetTextAlign(23);  // center/top alignment
1592   std::ostringstream runnostream;
1593   std::ostringstream runnostream2;
1594   std::string runstring;
1595   std::pair<time_t, int> evttime = cl->EventTime("CURRENT");
1596   // fill run number and event time into string
1597   runnostream << ThisName << ": Pulse fitting";
1598   runnostream2 << "Run " << cl->RunNumber() << ", Time: " << ctime(&evttime.first);
1599 
1600   transparent[2]->cd();
1601 
1602   runstring = runnostream.str();
1603   PrintRun.SetTextColor(evttime.second);
1604   PrintRun.DrawText(0.5, 0.99, runstring.c_str());
1605 
1606   runstring = runnostream2.str();
1607   PrintRun.DrawText(0.5, 0.966, runstring.c_str());
1608 
1609   Pad[5]->cd();
1610   gPad->SetTopMargin(0.06);
1611   gPad->SetBottomMargin(0.18);
1612   gPad->SetRightMargin(0.05);
1613   gPad->SetLeftMargin(0.15);
1614   gPad->SetTicky();
1615   gPad->SetTickx();
1616 
1617   gStyle->SetTitleFontSize(0.06);
1618 
1619   h1_waveform_time[start[1]]->SetStats(kFALSE);
1620   h1_waveform_time[start[1]]->Draw("hist");
1621   h1_waveform_time[start[1]]->GetXaxis()->SetNdivisions(16);
1622   h1_waveform_time[start[1]]->GetXaxis()->SetTitle("Sample #");
1623   h1_waveform_time[start[1]]->GetYaxis()->SetTitle("Fraction of Towers");
1624   h1_waveform_time[start[1]]->GetXaxis()->SetLabelSize(tsize2);
1625   h1_waveform_time[start[1]]->GetYaxis()->SetLabelSize(tsize2);
1626   h1_waveform_time[start[1]]->GetXaxis()->SetTitleSize(tsize2);
1627   h1_waveform_time[start[1]]->GetYaxis()->SetTitleSize(tsize2);
1628   h1_waveform_time[start[1]]->GetXaxis()->SetTitleOffset(1.0);
1629   h1_waveform_time[start[1]]->GetYaxis()->SetTitleOffset(.85);
1630   h1_waveform_time[start[1]]->SetFillColorAlpha(kBlue, 0.1);
1631   gPad->Update();
1632   if (h1_waveform_time[start[1]]->GetEntries())
1633   {
1634     h1_waveform_time[start[1]]->Scale(1. / h1_waveform_time[start[1]]->GetEntries());
1635   }
1636   h1_waveform_time[start[1]]->GetYaxis()->SetRangeUser(0, 1.);
1637 
1638   gPad->SetTopMargin(0.06);
1639   gPad->SetBottomMargin(0.18);
1640   gPad->SetRightMargin(0.05);
1641   gStyle->SetOptStat(0);
1642   gPad->SetLeftMargin(0.15);
1643   gPad->SetTicky();
1644   gPad->SetTickx();
1645   TLine windowLow2(SampleLowBoundary, 0, SampleLowBoundary, gPad->GetFrame()->GetY2());
1646   windowLow2.SetLineWidth(3);
1647   windowLow2.DrawLine(SampleLowBoundary, 0, SampleLowBoundary, gPad->GetFrame()->GetY2());
1648 
1649   TLine windowHigh2(SampleHighBoundary, 0, SampleHighBoundary, gPad->GetFrame()->GetY2());
1650   windowHigh2.SetLineWidth(3);
1651   windowHigh2.DrawLine(SampleHighBoundary, 0, SampleHighBoundary, gPad->GetFrame()->GetY2());
1652 
1653   TLine meantime(h1_waveform_time[start[1]]->GetMean(), 0, h1_waveform_time[start[1]]->GetMean(), gPad->GetFrame()->GetY2());
1654   meantime.SetLineWidth(3);
1655   meantime.SetLineColor(kRed);
1656   meantime.DrawLine(h1_waveform_time[start[1]]->GetMean(), 0, h1_waveform_time[start[1]]->GetMean(), gPad->GetFrame()->GetY2());
1657 
1658   Pad[6]->cd();
1659   gPad->SetTopMargin(0.06);
1660   gPad->SetBottomMargin(0.18);
1661   gPad->SetRightMargin(0.05);
1662   gPad->SetLeftMargin(0.15);
1663   gPad->SetTicky();
1664   gPad->SetTickx();
1665 
1666   gPad->SetLogy(kFALSE);
1667   gStyle->SetTitleFontSize(0.06);
1668   h1_waveform_pedestal[start[2]]->SetStats(false);
1669   h1_waveform_pedestal[start[2]]->Draw("hist");
1670   h1_waveform_pedestal[start[2]]->GetXaxis()->SetNdivisions(505, kTRUE);
1671   h1_waveform_pedestal[start[2]]->GetXaxis()->SetTitle("ADC Pedestal");
1672   h1_waveform_pedestal[start[2]]->GetYaxis()->SetTitle("Fraction of Towers");
1673   h1_waveform_pedestal[start[2]]->GetXaxis()->SetLabelSize(tsize2);
1674   h1_waveform_pedestal[start[2]]->GetYaxis()->SetLabelSize(tsize2);
1675   h1_waveform_pedestal[start[2]]->GetXaxis()->SetTitleSize(tsize2);
1676   h1_waveform_pedestal[start[2]]->GetYaxis()->SetTitleSize(tsize2);
1677   h1_waveform_pedestal[start[2]]->GetXaxis()->SetTitleOffset(1);
1678   h1_waveform_pedestal[start[2]]->GetYaxis()->SetTitleOffset(0.85);
1679   h1_waveform_pedestal[start[2]]->SetFillColorAlpha(kBlue, 0.1);
1680   gPad->Update();
1681 
1682   TLine windowLow3(1000, 0, 1000, gPad->GetFrame()->GetY2());
1683   windowLow3.SetLineWidth(3);
1684   windowLow3.DrawLine(1000, 0, 1000, gPad->GetFrame()->GetY2());
1685 
1686   TLine windowHigh3(2000, 0, 2000, gPad->GetFrame()->GetY2());
1687   windowHigh3.SetLineWidth(3);
1688   windowHigh3.DrawLine(2000, 0, 2000, gPad->GetFrame()->GetY2());
1689 
1690   gPad->SetLogy();
1691   gPad->Update();
1692 
1693   if (h1_waveform_pedestal[start[2]]->GetEntries())
1694   {
1695     h1_waveform_pedestal[start[2]]->Scale(1. / h1_waveform_pedestal[start[2]]->GetEntries());
1696   }
1697   // h1_waveform_pedestal -> GetXaxis() -> SetRangeUser(1000,2000);
1698   gStyle->SetOptStat(0);
1699 
1700   TC[2]->Update();
1701   // gStyle->SetOptStat(0);
1702 
1703   TC[2]->Show();
1704   // gStyle->SetOptStat(0);
1705 
1706   TC[2]->SetEditable(false);
1707   // gStyle->SetOptStat(0);
1708 
1709   if (save)
1710   {
1711     TC[2]->SaveAs("plots/waveform.pdf");
1712   }
1713   // gStyle->SetOptStat(0);
1714 
1715   return 0;
1716 }
1717 
1718 int CemcMonDraw::DrawFourth(const std::string & /* what */)
1719 {
1720   OnlMonClient *cl = OnlMonClient::instance();
1721 
1722   TH1 *h_waveform_sigDiff[m_ServerSet.size()];
1723   int start[3];
1724   start[0] = -1;
1725   int i = 0;
1726   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1727   {
1728     h_waveform_sigDiff[i] = cl->getHisto(*server, "h1_fitting_sigDiff");
1729     if (h_waveform_sigDiff[i] && start[0] == -1)
1730     {
1731       start[0] = i;
1732     }
1733     if (start[0] > -1 && h_waveform_sigDiff[i])
1734     {
1735       h_waveform_sigDiff[i]->SetName(Form("h_fitting_sigDiff_%d", i));
1736       h_waveform_sigDiff[start[0]]->Add(h_waveform_sigDiff[i], 1);
1737     }
1738     i++;
1739   }
1740 
1741   TH1 *h_waveform_pedDiff[m_ServerSet.size()];
1742   start[1] = -1;
1743   i = 0;
1744   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1745   {
1746     h_waveform_pedDiff[i] = cl->getHisto(*server, "h1_fitting_pedDiff");
1747     if (h_waveform_pedDiff[i] && start[1] == -1)
1748     {
1749       start[1] = i;
1750     }
1751     if (start[1] > -1 && h_waveform_pedDiff[i])
1752     {
1753       h_waveform_pedDiff[i]->SetName(Form("h_fitting_pedDiff_%d", i));
1754       h_waveform_pedDiff[start[1]]->Add(h_waveform_pedDiff[i], 1);
1755     }
1756     i++;
1757   }
1758 
1759   TH1 *h_waveform_timeDiff[m_ServerSet.size()];
1760   start[2] = -1;
1761   i = 0;
1762   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1763   {
1764     h_waveform_timeDiff[i] = cl->getHisto(*server, "h1_fitting_timeDiff");
1765     if (h_waveform_timeDiff[i] && start[2] == -1)
1766     {
1767       start[2] = i;
1768     }
1769     if (start[2] > -1 && h_waveform_timeDiff[i])
1770     {
1771       h_waveform_timeDiff[i]->SetName(Form("h_fitting_timeDiff_%d", i));
1772       h_waveform_timeDiff[start[2]]->Add(h_waveform_timeDiff[i], 1);
1773     }
1774     i++;
1775   }
1776 
1777   if (!gROOT->FindObject("CemcMon4"))
1778   {
1779     MakeCanvas("CemcMon4");
1780   }
1781 
1782   TC[3]->SetEditable(true);
1783   TC[3]->Clear("D");
1784   if (start[0] < 0 || start[1] < 0 || start[2] < 0)
1785   {
1786     DrawDeadServer(transparent[3]);
1787     TC[3]->SetEditable(false);
1788     return -1;
1789   }
1790 
1791   gStyle->SetTitleFontSize(0.03);
1792 
1793   TText PrintRun;
1794   PrintRun.SetTextFont(62);
1795   PrintRun.SetTextSize(0.03);
1796   PrintRun.SetNDC();          // set to normalized coordinates
1797   PrintRun.SetTextAlign(23);  // center/top alignment
1798   std::ostringstream runnostream;
1799   std::ostringstream runnostream2;
1800   std::string runstring;
1801   std::pair<time_t, int> evttime = cl->EventTime("CURRENT");
1802   // fill run number and event time into string
1803   runnostream << "Waveform Template vs. Fast Fitting";
1804   runnostream2 << "Run " << cl->RunNumber() << ", Time: " << ctime(&evttime.first);
1805 
1806   transparent[3]->cd();
1807 
1808   runstring = runnostream.str();
1809   PrintRun.SetTextColor(evttime.second);
1810   PrintRun.DrawText(0.5, 0.99, runstring.c_str());
1811 
1812   runstring = runnostream2.str();
1813   PrintRun.DrawText(0.5, 0.966, runstring.c_str());
1814 
1815   Pad[7]->cd();
1816 
1817   gStyle->SetTitleFontSize(0.06);
1818 
1819   float tsize2 = 0.08;
1820   h_waveform_sigDiff[start[0]]->GetXaxis()->SetNdivisions(16);
1821   h_waveform_sigDiff[start[0]]->GetXaxis()->SetTitle("Fast ADC/Template ADC");
1822   h_waveform_sigDiff[start[0]]->GetYaxis()->SetTitle("Fraction of Towers");
1823   h_waveform_sigDiff[start[0]]->GetXaxis()->SetLabelSize(tsize2 - .01);
1824   h_waveform_sigDiff[start[0]]->GetYaxis()->SetLabelSize(tsize2 - .01);
1825   h_waveform_sigDiff[start[0]]->GetXaxis()->SetTitleSize(tsize2 - .01);
1826   h_waveform_sigDiff[start[0]]->GetYaxis()->SetTitleSize(tsize2 - .01);
1827   h_waveform_sigDiff[start[0]]->GetXaxis()->SetTitleOffset(.9);
1828   h_waveform_sigDiff[start[0]]->GetYaxis()->SetTitleOffset(1.1);
1829   if (h_waveform_sigDiff[start[0]]->GetEntries())
1830   {
1831     h_waveform_sigDiff[start[0]]->Scale(1. / h_waveform_sigDiff[start[0]]->GetEntries());
1832   }
1833   gPad->SetTopMargin(0.06);
1834   gPad->SetBottomMargin(0.18);
1835   gPad->SetRightMargin(0.05);
1836   gPad->SetLeftMargin(0.15);
1837   gStyle->SetOptStat(0);
1838   gPad->SetTicky();
1839   gPad->SetTickx();
1840   h_waveform_sigDiff[start[0]]->DrawCopy("hist");
1841 
1842   Pad[8]->cd();
1843 
1844   gStyle->SetTitleFontSize(0.06);
1845 
1846   h_waveform_pedDiff[start[1]]->GetXaxis()->SetNdivisions(16);
1847   h_waveform_pedDiff[start[1]]->GetXaxis()->SetTitle("Fast Pedestal/Template Pedestal");
1848   h_waveform_pedDiff[start[1]]->GetYaxis()->SetTitle("Fraction of Towers");
1849   h_waveform_pedDiff[start[1]]->GetXaxis()->SetLabelSize(tsize2);
1850   h_waveform_pedDiff[start[1]]->GetYaxis()->SetLabelSize(tsize2);
1851   h_waveform_pedDiff[start[1]]->GetXaxis()->SetTitleSize(tsize2);
1852   h_waveform_pedDiff[start[1]]->GetYaxis()->SetTitleSize(tsize2);
1853   h_waveform_pedDiff[start[1]]->GetXaxis()->SetTitleOffset(0.9);
1854   h_waveform_pedDiff[start[1]]->GetYaxis()->SetTitleOffset(0.9);
1855   if (h_waveform_pedDiff[start[1]]->GetEntries())
1856   {
1857     h_waveform_pedDiff[start[1]]->Scale(1. / h_waveform_pedDiff[start[1]]->GetEntries());
1858   }
1859   gPad->SetTopMargin(0.06);
1860   gPad->SetBottomMargin(0.18);
1861   gPad->SetRightMargin(0.05);
1862   gPad->SetLeftMargin(0.15);
1863   gStyle->SetOptStat(0);
1864   gPad->SetTicky();
1865   gPad->SetTickx();
1866   h_waveform_pedDiff[start[1]]->DrawCopy("hist");
1867 
1868   Pad[9]->cd();
1869 
1870   h_waveform_timeDiff[start[2]]->GetXaxis()->SetNdivisions(16);
1871   h_waveform_timeDiff[start[2]]->GetXaxis()->SetTitle("Fast Peak Time - Template Peak Time");
1872   h_waveform_timeDiff[start[2]]->GetYaxis()->SetTitle("Fraction of Towers");
1873   h_waveform_timeDiff[start[2]]->GetXaxis()->SetLabelSize(tsize2);
1874   h_waveform_timeDiff[start[2]]->GetYaxis()->SetLabelSize(tsize2);
1875   h_waveform_timeDiff[start[2]]->GetXaxis()->SetTitleSize(tsize2);
1876   h_waveform_timeDiff[start[2]]->GetYaxis()->SetTitleSize(tsize2);
1877   h_waveform_timeDiff[start[2]]->GetXaxis()->SetTitleOffset(0.9);
1878   h_waveform_timeDiff[start[2]]->GetYaxis()->SetTitleOffset(0.9);
1879   if (h_waveform_timeDiff[start[2]]->GetEntries())
1880   {
1881     h_waveform_timeDiff[start[2]]->Scale(1. / h_waveform_timeDiff[start[2]]->GetEntries());
1882   }
1883   gPad->SetTopMargin(0.06);
1884   gPad->SetBottomMargin(0.18);
1885   gPad->SetRightMargin(0.05);
1886   gPad->SetLeftMargin(0.15);
1887   gPad->SetTicky();
1888   gPad->SetTickx();
1889   h_waveform_timeDiff[start[2]]->DrawCopy("hist");
1890 
1891   TC[3]->Update();
1892   TC[3]->Show();
1893   TC[3]->SetEditable(false);
1894   gStyle->SetOptStat(0);
1895   if (save)
1896   {
1897     TC[3]->SaveAs("plots/waveformExpert.pdf");
1898   }
1899   return 0;
1900 }
1901 
1902 int CemcMonDraw::DrawFifth(const std::string & /* what */)
1903 {
1904   OnlMonClient *cl = OnlMonClient::instance();
1905 
1906   TH2 *h_cemc_hits_trig[64][m_ServerSet.size()];
1907   int start_trig[64] = {-1};
1908   for (int i = 1; i < 64; i++)
1909   {
1910     start_trig[i] = -1;
1911   }
1912   int i = 0;
1913 
1914   for (int itrig = 0; itrig < 64; itrig++)
1915   {
1916     i = 0;
1917 
1918     for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1919     {
1920       h_cemc_hits_trig[itrig][i] = (TH2 *) cl->getHisto(*server, Form("h2_cemc_hits_trig_bit_%d", itrig));
1921       if (h_cemc_hits_trig[itrig][i] && start_trig[itrig] == -1)
1922       {
1923         start_trig[itrig] = i;
1924       }
1925       if (start_trig[itrig] > -1 && h_cemc_hits_trig[itrig][i])
1926       {
1927         h_cemc_hits_trig[itrig][i]->SetName(Form("h_cemc_hits_trig_bit_%d_%d", itrig, i));
1928         if (start_trig[itrig] != i)
1929         {
1930           h_cemc_hits_trig[itrig][start_trig[itrig]]->Add(h_cemc_hits_trig[itrig][i], 1);
1931         }
1932       }
1933       i++;
1934     }
1935   }
1936 
1937   TH1 *h_cemc_trig[m_ServerSet.size()];
1938   int start = -1;
1939   i = 0;
1940   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1941   {
1942     h_cemc_trig[i] = cl->getHisto(*server, "h1_cemc_trig");
1943     if (h_cemc_trig[i] && start == -1)
1944     {
1945       start = i;
1946     }
1947     if (start > -1 && h_cemc_trig[i])
1948     {
1949       h_cemc_trig[i]->SetName(Form("h_cemc_trig_%d", i));
1950       if (start != i)
1951       {
1952         h_cemc_trig[start]->Add(h_cemc_trig[i], 1);
1953       }
1954     }
1955     i++;
1956   }
1957 
1958   TProfile *h_evtRec;
1959   i = 0;
1960   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
1961   {
1962     h_evtRec = (TProfile *) cl->getHisto(*server, "h_evtRec");
1963     if (h_evtRec)
1964     {
1965       break;
1966     }
1967   }
1968 
1969   if (!gROOT->FindObject("CemcMon5"))
1970   {
1971     MakeCanvas("CemcMon5");
1972   }
1973 
1974   TC[4]->SetEditable(true);
1975   TC[4]->Clear("D");
1976   if (!h_cemc_trig[start])
1977   {
1978     DrawDeadServer(transparent[4]);
1979     TC[4]->SetEditable(false);
1980     return -1;
1981   }
1982 
1983   for (int itrig = 0; itrig < 64; itrig++)
1984   {
1985     if (!h_cemc_hits_trig[itrig][start_trig[itrig]])
1986     {
1987       DrawDeadServer(transparent[4]);
1988       TC[4]->SetEditable(false);
1989       return -1;
1990     }
1991   }
1992 
1993   // vector of pairs (Number of entries, Trigger bit)
1994   std::vector<std::pair<float, int>> n_entries;
1995 
1996   for (int itrig = 0; itrig < 64; itrig++)
1997   {
1998     n_entries.push_back(std::make_pair(h_cemc_hits_trig[itrig][start_trig[itrig]]->GetEntries(), itrig));
1999   }
2000 
2001   // Sort it in ascending order of entries
2002   std::sort(n_entries.begin(), n_entries.end());
2003   // Reverse it to get the vector in descending order
2004   std::reverse(n_entries.begin(), n_entries.end());
2005 
2006   // Get the 4 priority trigger bits to be displayed
2007   std::vector<int> priority_triggers;
2008 
2009   for (int itrig = 0; itrig < 64; itrig++)
2010   {
2011     // Priority to the bits between 24 and 31 (draw photon trig first)
2012     // if (n_entries[itrig].second >= 24 && n_entries[itrig].second <= 31)
2013     if (n_entries[itrig].second == TriggerEnum::BitCodes::PHOTON6_MBD_NS2 ||
2014         n_entries[itrig].second == TriggerEnum::BitCodes::PHOTON8_MBD_NS2 ||
2015         n_entries[itrig].second == TriggerEnum::BitCodes::PHOTON10_MBD_NS2 ||
2016         n_entries[itrig].second == TriggerEnum::BitCodes::PHOTON12_MBD_NS2 || n_entries[itrig].second == TriggerEnum::BitCodes::HCAL_SINGLES || n_entries[itrig].second == TriggerEnum::BitCodes::HCAL_NARROW_VERT || n_entries[itrig].second == TriggerEnum::BitCodes::HCAL_WIDE_VERT || n_entries[itrig].second == TriggerEnum::BitCodes::HCAL_NARROW_HORZ || n_entries[itrig].second == TriggerEnum::BitCodes::HCAL_WIDE_HORZ)
2017     {
2018       if (n_entries[itrig].first > 0. && priority_triggers.size() < 4)
2019       {
2020         priority_triggers.push_back(n_entries[itrig].second);
2021       }
2022     }
2023   }
2024 
2025   // If trigger bits from 24 to 31 do not have 4 with entries, plot the others
2026   if (priority_triggers.size() < 4)
2027   {
2028     for (int itrig = 0; itrig < 64; itrig++)
2029     {
2030       if (priority_triggers.size() < 4 && n_entries[itrig].second < TriggerEnum::BitCodes::PHOTON12_MBD_NS2)
2031       {
2032         priority_triggers.push_back(n_entries[itrig].second);
2033       }
2034     }
2035   }
2036 
2037   TText PrintRun;
2038   PrintRun.SetTextFont(62);
2039   PrintRun.SetTextSize(0.03);
2040   PrintRun.SetNDC();          // set to normalized coordinates
2041   PrintRun.SetTextAlign(23);  // center/top alignment
2042   std::ostringstream runnostream;
2043   std::string runstring;
2044   std::pair<time_t, int> evttime = cl->EventTime("CURRENT");
2045   // fill run number and event time into string
2046   runnostream << ThisName << ": Trigger Info, Run" << cl->RunNumber()
2047               << ", Time: " << ctime(&evttime.first);
2048   runstring = runnostream.str();
2049   transparent[4]->cd();
2050   PrintRun.SetTextColor(evttime.second);
2051   PrintRun.DrawText(0.5, 0.99, runstring.c_str());
2052 
2053   Pad[10]->cd();
2054   gStyle->SetTitleFontSize(0.06);
2055 
2056   float tsize = 0.08;
2057   h_cemc_hits_trig[priority_triggers[0]][start_trig[priority_triggers[0]]]->GetXaxis()->SetNdivisions(510, kTRUE);
2058   h_cemc_hits_trig[priority_triggers[0]][start_trig[priority_triggers[0]]]->GetXaxis()->SetTitle(Form("(trigger bit %d)  ieta", priority_triggers[0]));
2059   h_cemc_hits_trig[priority_triggers[0]][start_trig[priority_triggers[0]]]->GetYaxis()->SetTitle("iphi");
2060   h_cemc_hits_trig[priority_triggers[0]][start_trig[priority_triggers[0]]]->GetXaxis()->SetLabelSize(tsize);
2061   h_cemc_hits_trig[priority_triggers[0]][start_trig[priority_triggers[0]]]->GetYaxis()->SetLabelSize(tsize);
2062   h_cemc_hits_trig[priority_triggers[0]][start_trig[priority_triggers[0]]]->GetXaxis()->SetTitleSize(tsize);
2063   h_cemc_hits_trig[priority_triggers[0]][start_trig[priority_triggers[0]]]->GetYaxis()->SetTitleSize(tsize);
2064   h_cemc_hits_trig[priority_triggers[0]][start_trig[priority_triggers[0]]]->GetXaxis()->SetTitleOffset(1.2);
2065   h_cemc_hits_trig[priority_triggers[0]][start_trig[priority_triggers[0]]]->GetYaxis()->SetTitleOffset(0.75);
2066   gPad->SetLogz();
2067   gPad->SetTopMargin(0.06);
2068   gPad->SetBottomMargin(0.18);
2069   gPad->SetRightMargin(0.05);
2070   gPad->SetLeftMargin(0.15);
2071   gStyle->SetOptStat(0);
2072   gStyle->SetPalette(57);
2073   gPad->SetTicky();
2074   gPad->SetTickx();
2075   h_cemc_hits_trig[priority_triggers[0]][start_trig[priority_triggers[0]]]->Draw("colz");
2076 
2077   Pad[11]->cd();
2078   gStyle->SetTitleFontSize(0.06);
2079 
2080   h_cemc_hits_trig[priority_triggers[1]][start_trig[priority_triggers[1]]]->GetXaxis()->SetNdivisions(510, kTRUE);
2081   h_cemc_hits_trig[priority_triggers[1]][start_trig[priority_triggers[1]]]->GetXaxis()->SetTitle(Form("(trigger bit %d)  ieta", priority_triggers[1]));
2082   h_cemc_hits_trig[priority_triggers[1]][start_trig[priority_triggers[1]]]->GetYaxis()->SetTitle("iphi");
2083   h_cemc_hits_trig[priority_triggers[1]][start_trig[priority_triggers[1]]]->GetXaxis()->SetLabelSize(tsize);
2084   h_cemc_hits_trig[priority_triggers[1]][start_trig[priority_triggers[1]]]->GetYaxis()->SetLabelSize(tsize);
2085   h_cemc_hits_trig[priority_triggers[1]][start_trig[priority_triggers[1]]]->GetXaxis()->SetTitleSize(tsize);
2086   h_cemc_hits_trig[priority_triggers[1]][start_trig[priority_triggers[1]]]->GetYaxis()->SetTitleSize(tsize);
2087   h_cemc_hits_trig[priority_triggers[1]][start_trig[priority_triggers[1]]]->GetXaxis()->SetTitleOffset(1.2);
2088   h_cemc_hits_trig[priority_triggers[1]][start_trig[priority_triggers[1]]]->GetYaxis()->SetTitleOffset(0.75);
2089   gPad->SetLogz();
2090   gPad->SetTopMargin(0.06);
2091   gPad->SetBottomMargin(0.18);
2092   gPad->SetRightMargin(0.05);
2093   gPad->SetLeftMargin(0.15);
2094   gStyle->SetOptStat(0);
2095   gStyle->SetPalette(57);
2096   gPad->SetTicky();
2097   gPad->SetTickx();
2098   h_cemc_hits_trig[priority_triggers[1]][start_trig[priority_triggers[1]]]->Draw("colz");
2099 
2100   Pad[12]->cd();
2101   gStyle->SetTitleFontSize(0.06);
2102 
2103   h_cemc_hits_trig[priority_triggers[2]][start_trig[priority_triggers[2]]]->GetXaxis()->SetNdivisions(510, kTRUE);
2104   h_cemc_hits_trig[priority_triggers[2]][start_trig[priority_triggers[2]]]->GetXaxis()->SetTitle(Form("(trigger bit %d)  ieta", priority_triggers[2]));
2105   h_cemc_hits_trig[priority_triggers[2]][start_trig[priority_triggers[2]]]->GetYaxis()->SetTitle("iphi");
2106   h_cemc_hits_trig[priority_triggers[2]][start_trig[priority_triggers[2]]]->GetXaxis()->SetLabelSize(tsize);
2107   h_cemc_hits_trig[priority_triggers[2]][start_trig[priority_triggers[2]]]->GetYaxis()->SetLabelSize(tsize);
2108   h_cemc_hits_trig[priority_triggers[2]][start_trig[priority_triggers[2]]]->GetXaxis()->SetTitleSize(tsize);
2109   h_cemc_hits_trig[priority_triggers[2]][start_trig[priority_triggers[2]]]->GetYaxis()->SetTitleSize(tsize);
2110   h_cemc_hits_trig[priority_triggers[2]][start_trig[priority_triggers[2]]]->GetXaxis()->SetTitleOffset(1.2);
2111   h_cemc_hits_trig[priority_triggers[2]][start_trig[priority_triggers[2]]]->GetYaxis()->SetTitleOffset(0.75);
2112   gPad->SetLogz();
2113   gPad->SetTopMargin(0.06);
2114   gPad->SetBottomMargin(0.18);
2115   gPad->SetRightMargin(0.05);
2116   gPad->SetLeftMargin(0.15);
2117   gStyle->SetOptStat(0);
2118   gStyle->SetPalette(57);
2119   gPad->SetTicky();
2120   gPad->SetTickx();
2121   h_cemc_hits_trig[priority_triggers[2]][start_trig[priority_triggers[2]]]->Draw("colz");
2122 
2123   Pad[13]->cd();
2124   gStyle->SetTitleFontSize(0.06);
2125 
2126   h_cemc_hits_trig[priority_triggers[3]][start_trig[priority_triggers[3]]]->GetXaxis()->SetNdivisions(510, kTRUE);
2127   h_cemc_hits_trig[priority_triggers[3]][start_trig[priority_triggers[3]]]->GetXaxis()->SetTitle(Form("(trigger bit %d)  ieta", priority_triggers[3]));
2128   h_cemc_hits_trig[priority_triggers[3]][start_trig[priority_triggers[3]]]->GetYaxis()->SetTitle("iphi");
2129   h_cemc_hits_trig[priority_triggers[3]][start_trig[priority_triggers[3]]]->GetXaxis()->SetLabelSize(tsize);
2130   h_cemc_hits_trig[priority_triggers[3]][start_trig[priority_triggers[3]]]->GetYaxis()->SetLabelSize(tsize);
2131   h_cemc_hits_trig[priority_triggers[3]][start_trig[priority_triggers[3]]]->GetXaxis()->SetTitleSize(tsize);
2132   h_cemc_hits_trig[priority_triggers[3]][start_trig[priority_triggers[3]]]->GetYaxis()->SetTitleSize(tsize);
2133   h_cemc_hits_trig[priority_triggers[3]][start_trig[priority_triggers[3]]]->GetXaxis()->SetTitleOffset(1.2);
2134   h_cemc_hits_trig[priority_triggers[3]][start_trig[priority_triggers[3]]]->GetYaxis()->SetTitleOffset(0.75);
2135   gPad->SetLogz();
2136   gPad->SetTopMargin(0.06);
2137   gPad->SetBottomMargin(0.18);
2138   gPad->SetRightMargin(0.05);
2139   gPad->SetLeftMargin(0.15);
2140   gStyle->SetOptStat(0);
2141   gStyle->SetPalette(57);
2142   gPad->SetTicky();
2143   gPad->SetTickx();
2144   h_cemc_hits_trig[priority_triggers[3]][start_trig[priority_triggers[3]]]->Draw("colz");
2145 
2146   Pad[14]->cd();
2147   gStyle->SetTitleFontSize(0.06);
2148 
2149   float nEvtRec = 0.;
2150 
2151   if (h_evtRec)
2152   {
2153     nEvtRec = h_evtRec->GetBinContent(1);
2154   }
2155 
2156   h_cemc_trig[start]->SetTitle(Form("Receiving %0.3f of events from event reciever", nEvtRec));
2157   h_cemc_trig[start]->GetXaxis()->SetNdivisions(510, kTRUE);
2158   h_cemc_trig[start]->GetXaxis()->SetTitle("trigger index");
2159   h_cemc_trig[start]->GetYaxis()->SetTitle("events");
2160   h_cemc_trig[start]->GetXaxis()->SetLabelSize(tsize);
2161   h_cemc_trig[start]->GetYaxis()->SetLabelSize(tsize);
2162   h_cemc_trig[start]->GetXaxis()->SetTitleSize(tsize);
2163   h_cemc_trig[start]->GetYaxis()->SetTitleSize(tsize);
2164   h_cemc_trig[start]->GetXaxis()->SetTitleOffset(0.9);
2165   h_cemc_trig[start]->GetYaxis()->SetTitleOffset(0.85);
2166   gPad->SetTopMargin(0.06);
2167   gPad->SetBottomMargin(0.18);
2168   gPad->SetRightMargin(0.05);
2169   gPad->SetLeftMargin(0.15);
2170   gStyle->SetOptStat(0);
2171   gPad->SetTicky();
2172   gPad->SetTickx();
2173   h_cemc_trig[start]->Draw("histo");
2174 
2175   TC[4]->Update();
2176   TC[4]->Show();
2177   TC[4]->SetEditable(false);
2178 
2179   return 0;
2180 }
2181 int CemcMonDraw::FindGainMode(TPad *warningpad, TH2 *hhit)
2182 {
2183   float avgrms = 0;
2184   float totaltowers = 0;
2185 
2186   for (int ieta = 0; ieta < nTowersEta; ieta++)
2187   {
2188     for (int iphi = 0; iphi < nTowersPhi; iphi++)
2189     {
2190       if (hhit->GetBinContent(ieta + 1, iphi + 1) == 0)
2191       {
2192         continue;
2193       }
2194       double rms = hhit->GetBinContent(ieta + 1, iphi + 1);
2195       avgrms += rms;
2196       totaltowers += 1;
2197     }
2198   }
2199   avgrms /= totaltowers;
2200 
2201   bool hg = false;
2202   if (avgrms > 100)
2203   {
2204     hg = true;
2205   }
2206 
2207   warningpad->cd();
2208   std::string gainmode = "Gain mode: LOW";
2209   if (hg)
2210   {
2211     gainmode = "Gain mode: HIGH";
2212   }
2213 
2214   TText warn;
2215   warn.SetTextFont(62);
2216   warn.SetTextSize(0.3);
2217   warn.SetTextColor(kRed);
2218   warn.SetNDC();
2219   warn.SetTextAlign(23);
2220   warn.DrawText(0.5, 0.5, gainmode.c_str());
2221 
2222   warn.SetTextFont(62);
2223   warn.SetTextSize(0.06);
2224   warn.SetTextColor(1);
2225   warn.SetNDC();
2226   warn.SetTextAlign(23);
2227   warn.DrawText(0.5, 0.9, "Average RMS of pedestal:");
2228   warn.DrawText(0.5, 0.8, Form("%.2f", avgrms));
2229 
2230   warningpad->Update();
2231   return 0;
2232 }
2233 
2234 int CemcMonDraw::FindHotTower(TPad *warningpad, TH2 *hhit, bool usetemplate)
2235 {
2236   float nhott = 0;
2237   float ndeadt = 0;
2238   float ncoldt = 0;
2239   int displaylimit = 10;
2240   std::ostringstream hottowerlist;
2241   std::ostringstream deadtowerlist;
2242   std::ostringstream coldtowerlist;
2243   float hot_threshold = 2.0;
2244   float dead_threshold = 0.01;
2245   float cold_threshold = 0.5;
2246   if (!usetemplate)
2247   {
2248     float mean = 0;
2249     float rms = 0;
2250     int ntower = 0;
2251     for (int ieta = 0; ieta < nTowersEta; ieta++)
2252     {
2253       for (int iphi = 0; iphi < nTowersPhi; iphi++)
2254       {
2255         if ((ieta < 40 && ieta >= 32) && ((iphi >= 144) && (iphi < 152)))
2256         {
2257           continue;  // uninstrumented
2258         }
2259         else if ((ieta < 64 && ieta >= 56) && ((iphi >= 32) && (iphi < 40)))
2260         {
2261           continue;  // uninstrumented
2262         }
2263         if (hhit->GetBinContent(ieta + 1, iphi + 1) == 0)
2264         {
2265           continue;
2266         }
2267         double nhit = hhit->GetBinContent(ieta + 1, iphi + 1);
2268         mean += nhit;
2269         rms += nhit * nhit;
2270         ntower++;
2271       }
2272     }
2273     mean /= ntower;
2274     rms = sqrt(rms / ntower - mean * mean);
2275     hot_threshold = mean + 10 * rms;
2276     cold_threshold = mean - 3 * rms;
2277     dead_threshold = 0.01 * mean;
2278   }
2279 
2280   // float nTowerTotal = 24576. - 384.;  // to account for the non-functioning towers at the edge of the south
2281   for (int ieta = 0; ieta < nTowersEta; ieta++)
2282   {
2283     for (int iphi = 0; iphi < nTowersPhi; iphi++)
2284     {
2285       if ((ieta < 40 && ieta >= 32) && ((iphi >= 144) && (iphi < 152)))
2286       {
2287         continue;  // uninstrumented
2288       }
2289       else if ((ieta < 64 && ieta >= 56) && ((iphi >= 32) && (iphi < 40)))
2290       {
2291         continue;  // uninstrumented
2292       }
2293       if (hhit->GetBinContent(ieta + 1, iphi + 1) == 0)
2294       {
2295         continue;
2296       }
2297       double nhit = hhit->GetBinContent(ieta + 1, iphi + 1);
2298 
2299       if (nhit > hot_threshold)
2300       {
2301         if (nhott <= displaylimit)
2302         {
2303           hottowerlist << " (" << ieta << "," << iphi << ")";
2304         }
2305         nhott++;
2306       }
2307 
2308       if (nhit < dead_threshold)
2309       {
2310         if (ndeadt <= displaylimit)
2311         {
2312           deadtowerlist << " (" << ieta << "," << iphi << ")";
2313         }
2314         ndeadt++;
2315       }
2316       else if (nhit < cold_threshold)
2317       {
2318         if (ncoldt <= displaylimit)
2319         {
2320           coldtowerlist << " (" << ieta << "," << iphi << ")";
2321         }
2322         ncoldt++;
2323       }
2324     }
2325   }
2326 
2327   if (nhott > displaylimit)
2328   {
2329     hottowerlist << "... " << nhott << " total";
2330   }
2331   if (ndeadt > displaylimit)
2332   {
2333     deadtowerlist << "... " << ndeadt << " total";
2334   }
2335   if (ncoldt > displaylimit)
2336   {
2337     coldtowerlist << "... " << ncoldt << " total";
2338   }
2339   if (nhott == 0)
2340   {
2341     hottowerlist << " None";
2342   }
2343   if (ndeadt == 0)
2344   {
2345     deadtowerlist << " None";
2346   }
2347   if (ncoldt == 0)
2348   {
2349     coldtowerlist << " None";
2350   }
2351   // draw warning here
2352   warningpad->cd();
2353   // TPaveText *dead = new TPaveText(0.01, 0.7, 0.33, 1);
2354   // dead->SetFillColor(kGray + 2);
2355   // dead->SetTextColor(kWhite);
2356   // dead->AddText(Form("Cold towers: %.3g%%", 100 * ndeadt / nTowerTotal));
2357   // TPaveText *good = new TPaveText(0.33, 0.7, 0.66, 1);
2358   // good->SetFillColor(kGreen + 1);
2359   // good->AddText(Form("Good towers: %.3g%%", 100 * (nTowerTotal - ndeadt - nhott) / nTowerTotal));
2360   // TPaveText *hot = new TPaveText(0.66, 0.7, 1, 1);
2361   // hot->SetFillColor(kRed - 9);
2362   // hot->AddText(Form("Hot towers: %.3g%%", 100 * nhott / nTowerTotal));
2363   // TPaveText *warn = new TPaveText(0.01, 0.1, 1, 0.7);
2364   // warn->SetTextSize(0.1);
2365   // warn->AddText("Helpful Numbers: 1 Box = Interface Board (IB)");
2366   // warn->AddText("3 Boxes (horiz) = 1 Packet; 6 Boxes (horiz) = 1 Sector");
2367   // warn->AddText("For now, watch for entire IB's or sectors going dead");
2368   // dead->Draw();
2369   // good->Draw();
2370   // hot->Draw();
2371   // warn->Draw();
2372   TText warn;
2373   warn.SetTextFont(62);
2374   warn.SetTextSize(0.09);
2375   warn.SetTextColor(2);
2376   warn.SetNDC();
2377   warn.SetTextAlign(23);
2378   warn.DrawText(0.5, 0.9, "Hot towers (ieta,iphi):");
2379   warn.DrawText(0.5, 0.8, hottowerlist.str().c_str());
2380 
2381   warn.SetTextColor(1);
2382   warn.SetTextAlign(23);
2383   warn.DrawText(0.5, 0.3, "Dead towers (ieta,iphi):");
2384   warn.DrawText(0.5, 0.2, deadtowerlist.str().c_str());
2385 
2386   warn.SetTextColor(4);
2387   warn.SetTextAlign(23);
2388   warn.DrawText(0.5, 0.6, "Cold towers (ieta,iphi):");
2389   warn.DrawText(0.5, 0.5, coldtowerlist.str().c_str());
2390 
2391   warningpad->Update();
2392   return 0;
2393 }
2394 
2395 int CemcMonDraw::SavePlot(const std::string &what, const std::string &type)
2396 {
2397   OnlMonClient *cl = OnlMonClient::instance();
2398   int iret = Draw(what);
2399   if (iret)  // on error no png files please
2400   {
2401     return iret;
2402   }
2403   int icnt = 0;
2404   for (TCanvas *canvas : TC)
2405   {
2406     if (canvas == nullptr)
2407     {
2408       continue;
2409     }
2410     icnt++;
2411     std::string filename = ThisName + "_" + std::to_string(icnt) + "_" +
2412                            std::to_string(cl->RunNumber()) + "." + type;
2413     cl->CanvasToPng(canvas, filename);
2414   }
2415   return 0;
2416 }
2417 
2418 int CemcMonDraw::MakeHtml(const std::string &what)
2419 {
2420   int iret = Draw(what);
2421   if (iret)  // on error no html output please
2422   {
2423     return iret;
2424   }
2425 
2426   OnlMonClient *cl = OnlMonClient::instance();
2427 
2428   int icnt = 0;
2429   for (TCanvas *canvas : TC)
2430   {
2431     if (canvas == nullptr)
2432     {
2433       continue;
2434     }
2435     icnt++;
2436     // Register the canvas png file to the menu and produces the png file.
2437     std::string pngfile = cl->htmlRegisterPage(*this, canvas->GetTitle(), std::to_string(icnt), "png");
2438     cl->CanvasToPng(canvas, pngfile);
2439   }
2440   // Now register also EXPERTS html pages, under the EXPERTS subfolder.
2441 
2442   // std::string logfile = cl->htmlRegisterPage(*this, "EXPERTS/Log", "log", "html");
2443   // std::ofstream out(logfile.c_str());
2444   // out << "<HTML><HEAD><TITLE>Log file for run " << cl->RunNumber()
2445   //     << "</TITLE></HEAD>" << std::endl;
2446   // out << "<P>Some log file output would go here." << std::endl;
2447   // out.close();
2448 
2449   // std::string status = cl->htmlRegisterPage(*this, "EXPERTS/Status", "status", "html");
2450   // std::ofstream out2(status.c_str());
2451   // out2 << "<HTML><HEAD><TITLE>Status file for run " << cl->RunNumber()
2452   //      << "</TITLE></HEAD>" << std::endl;
2453   // out2 << "<P>Some status output would go here." << std::endl;
2454   // out2.close();
2455   // cl->SaveLogFile(*this);
2456   return 0;
2457 }
2458 
2459 std::vector<int> CemcMonDraw::getBadPackets(TH1 *hist, int what, float cutoff)
2460 {
2461   float params[3] = {1., 5981., 192.};
2462   float params2[3] = {1., 3991., 128.};
2463   // float cutoff = 0.75;
2464   std::vector<int> badpacks = {0};
2465   std::vector<int> uninPackets = {6022, 6026, 6030, 6034, 6038, 6042};
2466   // 6022, 6026, 6030, 6034, 6038, 6042
2467   for (int i = 1; i < hist->GetNbinsX(); i++)
2468   {
2469     if (std::count(uninPackets.begin(), uninPackets.end(), i + 6000))
2470     {
2471       if ((hist->GetBinContent(i) < params2[what] * cutoff || hist->GetBinContent(i) > params2[what]))
2472       {
2473         badpacks.push_back(i + 6000);
2474       }
2475     }
2476     else
2477     {
2478       if ((hist->GetBinContent(i) < params[what] * cutoff || hist->GetBinContent(i) > params[what]))
2479       {
2480         badpacks.push_back(i + 6000);
2481       }
2482     }
2483   }
2484 
2485   // if((hist -> GetBinContent(i) < params[what]*cutoff ||  hist -> GetBinContent(i) > params[what]) &&  !(((6000 + i - 2.)/4.) == floor(((6000 + i - 2)/4))) ) badpacks.push_back(i+6000);
2486 
2487   // else if((hist -> GetBinContent(i) < params2[what]*cutoff || hist -> GetBinContent(i) > params2[what]) &&  (((6000 + i - 2.)/4.) == floor(((6000 + i - 2)/4))) ) badpacks.push_back(i+6000);
2488 
2489   return badpacks;
2490 }
2491 
2492 int CemcMonDraw::DrawServerStats()
2493 {
2494   OnlMonClient *cl = OnlMonClient::instance();
2495   if (!gROOT->FindObject("CemcMonServerStats"))
2496   {
2497     MakeCanvas("CemcMonServerStats");
2498   }
2499   TC[5]->Clear("D");
2500   TC[5]->SetEditable(true);
2501   transparent[5]->cd();
2502   TText PrintRun;
2503   PrintRun.SetTextFont(62);
2504   PrintRun.SetNDC();          // set to normalized coordinates
2505   PrintRun.SetTextAlign(23);  // center/top alignment
2506   PrintRun.SetTextSize(0.04);
2507   PrintRun.SetTextColor(1);
2508   PrintRun.DrawText(0.5, 0.99, "Server Statistics");
2509 
2510   PrintRun.SetTextSize(0.02);
2511   double vdist = 0.04;
2512   double vpos = 0.92;
2513   for (const auto &server : m_ServerSet)
2514   {
2515     std::ostringstream txt;
2516     auto servermapiter = cl->GetServerMap(server);
2517     if (servermapiter == cl->GetServerMapEnd())
2518     {
2519       txt << "Server " << server
2520           << " is dead ";
2521       PrintRun.SetTextColor(kRed);
2522     }
2523     else
2524     {
2525       int gl1counts = std::get<4>(servermapiter->second);
2526       txt << "Server " << server
2527           << ", run number " << std::get<1>(servermapiter->second)
2528           << ", event count: " << std::get<2>(servermapiter->second);
2529       if (gl1counts >= 0)
2530       {
2531         txt << ", gl1 count: " << std::get<4>(servermapiter->second);
2532       }
2533       txt << ", current time " << ctime(&(std::get<3>(servermapiter->second)));
2534       if (std::get<0>(servermapiter->second))
2535       {
2536         PrintRun.SetTextColor(kGray + 2);
2537       }
2538       else
2539       {
2540         PrintRun.SetTextColor(kRed);
2541       }
2542     }
2543     PrintRun.DrawText(0.5, vpos, txt.str().c_str());
2544     vpos -= vdist;
2545   }
2546   TC[5]->Update();
2547   TC[5]->Show();
2548   TC[5]->SetEditable(false);
2549 
2550   return 0;
2551 }
2552 
2553 int CemcMonDraw::DrawBadChi2(const std::string & /* what */)
2554 {
2555   OnlMonClient *cl = OnlMonClient::instance();
2556   if (!gROOT->FindObject("CemcBadChi2"))
2557   {
2558     MakeCanvas("CemcBadChi2");
2559   }
2560   TC[8]->Clear("D");
2561   TC[8]->SetEditable(true);
2562   transparent[8]->cd();
2563 
2564   TText PrintRun;
2565   PrintRun.SetTextFont(62);
2566   PrintRun.SetTextSize(0.03);
2567   PrintRun.SetNDC();          // set to normalized coordinates
2568   PrintRun.SetTextAlign(23);  // center/top alignment
2569   std::ostringstream runnostream;
2570   std::ostringstream runnostream2;
2571   std::string runstring;
2572   std::pair<time_t, int> evttime = cl->EventTime("CURRENT");
2573   // fill run number and event time into string
2574   runnostream << "Tower with bad chi2";
2575   runnostream2 << "Run " << cl->RunNumber() << ", Time: " << ctime(&evttime.first);
2576 
2577   runstring = runnostream.str();
2578   PrintRun.SetTextColor(evttime.second);
2579   PrintRun.DrawText(0.5, 0.99, runstring.c_str());
2580 
2581   runstring = runnostream2.str();
2582   PrintRun.DrawText(0.5, 0.966, runstring.c_str());
2583 
2584   TProfile2D *p2_bad_chi2 = nullptr;
2585   TProfile2D *proftmp;
2586   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
2587   {
2588     proftmp = (TProfile2D *) cl->getHisto(*server, "p2_bad_chi2");
2589     if (proftmp)
2590     {
2591       if (p2_bad_chi2)
2592       {
2593         p2_bad_chi2->Add(proftmp);
2594       }
2595       else
2596       {
2597         p2_bad_chi2 = proftmp;
2598       }
2599     }
2600   }
2601   Pad[21]->cd();
2602   std::vector<std::pair<int, int>> badchi2;
2603   // loop over TProfile to find bad chi2
2604   for (int i = 1; i <= p2_bad_chi2->GetNbinsX(); i++)
2605   {
2606     for (int j = 1; j <= p2_bad_chi2->GetNbinsY(); j++)
2607     {
2608       float bad_chi2_prob = p2_bad_chi2->GetBinContent(i, j);
2609       if (bad_chi2_prob > 0.5)
2610       {
2611         // do stuff here find the sector and IB and display the text
2612         badchi2.push_back(std::make_pair(i - 1, j - 1));
2613       }
2614     }
2615   }
2616   // text display
2617   double vdist = 0.04;  // Vertical distance between lines
2618   double vpos = 0.92;   // Starting vertical position
2619   int displayedTowers = 0;
2620   TText printChi2;
2621   printChi2.SetTextFont(62);
2622   printChi2.SetNDC();          // Set to normalized coordinates
2623   printChi2.SetTextAlign(23);  // Center/top alignment
2624   printChi2.SetTextSize(0.04);
2625   printChi2.SetTextColor(1);
2626   for (const auto &[x, y] : badchi2)
2627   {
2628     bool ifknownbad = false;
2629     for (const auto &[i, j] : hotChannels)
2630     {
2631       if (i == x && j == y)
2632       {
2633         ifknownbad = true;
2634         break;
2635       }
2636     }
2637     float badChi2Rate = p2_bad_chi2->GetBinContent(x + 1, y + 1);  // Adjusting for ROOT's 1-based indexing
2638     std::ostringstream txt;
2639     txt << "Tower(" << x << "," << y << "): bad chi2 rate=" << badChi2Rate;
2640     // If the tower is known to be bad, draw it in red
2641     if (ifknownbad)
2642     {
2643       printChi2.SetTextColor(kRed);
2644     }
2645     else
2646     {
2647       printChi2.SetTextColor(kBlack);
2648     }
2649 
2650     printChi2.DrawText(0.5, vpos, txt.str().c_str());
2651 
2652     vpos -= vdist;  // Move to the next line
2653     displayedTowers++;
2654     if (vpos < 0.1) break;  // Prevent drawing outside the canvas
2655   }
2656 
2657   int remainingTowers = badchi2.size() - displayedTowers;
2658   if (remainingTowers > 0)
2659   {
2660     // Use TText to draw a message indicating how many more towers there are
2661     std::ostringstream moreTxt;
2662     moreTxt << "And " << remainingTowers << " more...";
2663     printChi2.DrawText(0.5, 0.05, moreTxt.str().c_str());  // Draw at the bottom
2664   }
2665 
2666   TC[8]->Update();
2667   TC[8]->Show();
2668   TC[8]->SetEditable(false);
2669 
2670   return 0;
2671 }
2672 
2673 int CemcMonDraw::DrawSeventh(const std::string &what)
2674 {
2675   OnlMonClient *cl = OnlMonClient::instance();
2676   // watch the absolute insanity as we merge all these
2677   // histograms from across seven different machines
2678 
2679   TProfile2D *p2_zsFrac_etaphiCombined = nullptr;
2680   TProfile2D *proftmp;
2681   for (auto server = ServerBegin(); server != ServerEnd(); ++server)
2682   {
2683     if (what == "SEVENTH")
2684     {
2685       proftmp = (TProfile2D *) cl->getHisto(*server, "p2_zsFrac_etaphi");
2686     }
2687     else
2688     {
2689       proftmp = (TProfile2D *) cl->getHisto(*server, "p2_zsFrac_etaphi_all");
2690     }
2691     if (proftmp)
2692     {
2693       if (p2_zsFrac_etaphiCombined)
2694       {
2695         p2_zsFrac_etaphiCombined->Add(proftmp);
2696       }
2697       else
2698       {
2699         p2_zsFrac_etaphiCombined = proftmp;
2700       }
2701     }
2702   }
2703 
2704   if (!gROOT->FindObject("CemcMon8"))
2705   {
2706     MakeCanvas("CemcMon8");
2707   }
2708   TC[7]->SetEditable(true);
2709   TC[7]->Clear("D");
2710   if (what == "SEVENTH")
2711   {
2712     TC[7]->SetTitle("Channel unsuppressed event fraction");
2713   }
2714   else
2715   {
2716     TC[7]->SetTitle("Channel unsuppressed event fraction (all triggers)");
2717   }
2718   if (!p2_zsFrac_etaphiCombined)
2719   {
2720     DrawDeadServer(transparent[7]);
2721     TC[7]->SetEditable(false);
2722     return -1;
2723   }
2724 
2725   Pad[19]->cd();
2726 
2727   double sum = 0;
2728   int count = 0;
2729 
2730   // reset
2731   h1_zs->Reset();
2732   h1_zs_low->Reset();
2733   h1_zs_high->Reset();
2734 
2735   for (int i = 1; i <= p2_zsFrac_etaphiCombined->GetNbinsX(); i++)
2736   {
2737     for (int j = 1; j <= p2_zsFrac_etaphiCombined->GetNbinsY(); j++)
2738     {
2739       float rate = p2_zsFrac_etaphiCombined->GetBinContent(i + 1, j + 1);
2740       if (rate <= 0.05)
2741       {
2742         h1_zs_low->Fill(rate);
2743       }
2744       else if (rate > 0.8)
2745       {
2746         h1_zs_high->Fill(rate);
2747       }
2748 
2749       h1_zs->Fill(rate);
2750       
2751       sum += rate;
2752       count++;
2753     }
2754   }
2755 
2756   double maxx = (sum / count) * 5 > 1.1 ? 1.1 : (sum / count) * 5;
2757   h1_zs->GetXaxis()->SetRangeUser(0, maxx);
2758   //max y is the max among h1_zs, h1_zs_low, h1_zs_high
2759   //double maxy = std::max({h1_zs->GetMaximum(), h1_zs_low->GetMaximum(), h1_zs_high->GetMaximum()});
2760   //h1_zs->SetMaximum(maxy * 1.2);
2761 
2762   double averagezs = sum / count * 100;
2763 
2764   gPad->SetTopMargin(0.02);
2765   gPad->SetBottomMargin(0.12);
2766   gPad->SetLeftMargin(0.12);
2767   gPad->SetRightMargin(0.12);
2768   gStyle->SetTitleFontSize(0.06);
2769   gStyle->SetPalette(57);
2770   // gStyle->SetPalette(255, ZSPalette);
2771   gStyle->SetNumberContours(255);
2772   p2_zsFrac_etaphiCombined->GetXaxis()->SetTitle("eta index");
2773   p2_zsFrac_etaphiCombined->GetYaxis()->SetTitle("phi index");
2774   p2_zsFrac_etaphiCombined->SetTitle(Form("Average unsuppressed rate: %.3f%%", averagezs));
2775   p2_zsFrac_etaphiCombined->GetXaxis()->SetLabelSize(0.05);
2776   p2_zsFrac_etaphiCombined->GetYaxis()->SetLabelSize(0.05);
2777   p2_zsFrac_etaphiCombined->GetXaxis()->SetTitleSize(0.05);
2778   p2_zsFrac_etaphiCombined->GetYaxis()->SetTitleSize(0.05);
2779   p2_zsFrac_etaphiCombined->GetXaxis()->SetTitleOffset(1.0);
2780   p2_zsFrac_etaphiCombined->GetYaxis()->SetTitleOffset(1.0);
2781   p2_zsFrac_etaphiCombined->SetMinimum(0);
2782   p2_zsFrac_etaphiCombined->SetMaximum(1);
2783   p2_zsFrac_etaphiCombined->SetStats(kFALSE);
2784   p2_zsFrac_etaphiCombined->DrawCopy("colz");
2785 
2786   Pad[20]->cd();
2787   gStyle->SetTitleFontSize(0.06);
2788   float tsize = 0.08;
2789   h1_zs->Draw();
2790   h1_zs->GetXaxis()->SetTitle("unsuppressed fraction");
2791   h1_zs->GetYaxis()->SetTitle("towers");
2792   h1_zs->GetXaxis()->SetLabelSize(tsize);
2793   h1_zs->GetYaxis()->SetLabelSize(tsize);
2794   h1_zs->GetXaxis()->SetTitleSize(tsize);
2795   h1_zs->GetYaxis()->SetTitleSize(tsize);
2796   h1_zs->GetXaxis()->SetTitleOffset(0.9);
2797   h1_zs->GetYaxis()->SetTitleOffset(0.85);
2798   h1_zs->GetXaxis()->SetNdivisions(510, kTRUE);
2799   h1_zs->SetFillColorAlpha(kBlue, 0.1);
2800   h1_zs_low->SetFillColorAlpha(kRed, 0.1);
2801   h1_zs_high->SetFillColorAlpha(kYellow, 0.1);
2802   h1_zs_low->Draw("same");
2803   h1_zs_high->Draw("same");
2804   gPad->SetBottomMargin(0.16);
2805   gPad->SetLeftMargin(0.15);
2806   gPad->SetRightMargin(0.15);
2807   gPad->SetTopMargin(0.1);
2808   gStyle->SetOptStat(0);
2809   gPad->SetTicky();
2810   gPad->SetTickx();
2811 
2812   TText PrintRun;
2813   PrintRun.SetTextFont(62);
2814   PrintRun.SetTextSize(0.03);
2815   PrintRun.SetNDC();          // set to normalized coordinates
2816   PrintRun.SetTextAlign(23);  // center/top alignment
2817   std::ostringstream runnostream;
2818   std::string runstring;
2819   std::pair<time_t, int> evttime = cl->EventTime("CURRENT");
2820   // fill run number and event time into string
2821   runnostream << ThisName << ": Unsuppressed event fraction, Run" << cl->RunNumber()
2822               << ", Time: " << ctime(&evttime.first);
2823   runstring = runnostream.str();
2824   transparent[7]->cd();
2825   PrintRun.SetTextColor(evttime.second);
2826   PrintRun.DrawText(0.5, 0.99, runstring.c_str());
2827 
2828   TC[7]->Update();
2829   TC[7]->Show();
2830   TC[7]->SetEditable(false);
2831   if (save) TC[7]->SaveAs("plots/UnsuppressedEventFraction.pdf");
2832   return 0;
2833 }
2834 
2835 /***********************
2836 int CemcMonDraw::DrawSixth(const std::string &  ){
2837 
2838   TH2D* h2_maxima;
2839   TH2D* h2_timeofMax;
2840   TH2D* h2_pedestal;
2841   TH2D* h2_saturating;
2842 
2843   if(!gROOT->FindObject("h2_maxima")){
2844     h2_maxima = new TH2D("h2_maxima","ADC counts at peak position",96,0,96,256,0,256);
2845     h2_maxima->SetStats(kFALSE);
2846     h2_maxima->GetXaxis()->SetNdivisions(12,kFALSE);
2847     h2_maxima->GetYaxis()->SetNdivisions(32,kFALSE);
2848     h2_maxima->GetXaxis()->SetTitle("eta index");
2849     h2_maxima->GetYaxis()->SetTitle("phi index");
2850   }
2851   else {
2852     h2_maxima=(TH2D*)gROOT->FindObject("h2_maxima");
2853     h2_maxima->Reset();
2854   }
2855   if(!gROOT->FindObject("h2_timeOfMax")){
2856     h2_timeofMax=new TH2D("h2_timeOfMax","Waveform peak position",96,0,96,256,0,256);
2857     h2_timeofMax->SetStats(kFALSE);
2858     h2_timeofMax->GetXaxis()->SetNdivisions(12,kFALSE);
2859     h2_timeofMax->GetYaxis()->SetNdivisions(32,kFALSE);
2860     h2_timeofMax->GetXaxis()->SetTitle("eta index");
2861     h2_timeofMax->GetYaxis()->SetTitle("phi index");
2862   }
2863   else{
2864     h2_timeofMax=(TH2D*)gROOT->FindObject("h2_timeOfMax");
2865     h2_timeofMax->Reset();
2866   }
2867   if(!gROOT->FindObject("h2_pedestal")){
2868     h2_pedestal=new TH2D("h2_pedestal","ADC counts for pedestal",96,0,96,256,0,256);
2869     h2_pedestal->SetStats(kFALSE);
2870     h2_pedestal->GetXaxis()->SetNdivisions(12,kFALSE);
2871     h2_pedestal->GetYaxis()->SetNdivisions(32,kFALSE);
2872     h2_pedestal->GetXaxis()->SetTitle("eta index");
2873     h2_pedestal->GetYaxis()->SetTitle("phi index");
2874   }
2875   else{
2876     h2_pedestal=(TH2D*)gROOT->FindObject("h2_pedestal");
2877     h2_pedestal->Reset();
2878   }
2879   if(!gROOT->FindObject("h2_saturating")){
2880     h2_saturating=new TH2D("h2_saturating","Tower with saturated signals",96,0,96,256,0,256);
2881     h2_saturating->SetStats(kFALSE);
2882     h2_saturating->GetXaxis()->SetNdivisions(12,kFALSE);
2883     h2_saturating->GetYaxis()->SetNdivisions(32,kFALSE);
2884     h2_saturating->GetXaxis()->SetTitle("eta index");
2885     h2_saturating->GetYaxis()->SetTitle("phi index");
2886   }
2887   else{
2888     h2_saturating=(TH2D*)gROOT->FindObject("h2_saturating");
2889     h2_saturating->Reset();
2890   }
2891 
2892   if (!gROOT->FindObject("CemcMon7"))
2893     {
2894       MakeCanvas("CemcMon7");
2895     }
2896   OnlMonClient *cl = OnlMonClient::instance();
2897   for(int i=0; i<256; i++){
2898     for(int j=0; j<96; j++){
2899       double mean=0;
2900       int Nmean=0;
2901       bool unsat=true;
2902       int nSaturated=0;
2903       bool init=false;
2904       TProfile* ptmp=nullptr;
2905       for (auto server = ServerBegin(); server != ServerEnd(); ++server){
2906         //for(int iseb=0;iseb<nSEBs; iseb++){
2907         //ptmp=(TProfile*)cl->getHisto(Form("CEMCMON_%d",iseb),Form("h2_waveform_phi%d_eta%d",i,j));
2908         ptmp=(TProfile*)cl->getHisto(*server,Form("h2_waveform_phi%d_eta%d",i,j));
2909         if(ptmp){
2910           if(!init){
2911             if(!gROOT->FindObject(Form("Combined%s",ptmp->GetName()))){
2912               AllProfiles[i][j] = new TProfile(*(TProfile*)ptmp);//Is it really necessary? Most likely not
2913               AllProfiles[i][j]->SetName(Form("Combined%s",ptmp->GetName()));
2914               init=true;
2915             }
2916             else{
2917               AllProfiles[i][j]->Reset();
2918               AllProfiles[i][j]->Add(ptmp);
2919               init=true;
2920             }
2921           }
2922           else{
2923             AllProfiles[i][j]->Add(ptmp);
2924           }
2925         }
2926         else{
2927           std::cout<<"Could not retrieve "<<Form("h2_waveform_phi%d_eta%d",i,j)<<" from "<<*server<<std::endl;
2928         }
2929       }
2930       if(AllProfiles[i][j]){
2931         h2_maxima->Fill(j,i,AllProfiles[i][j]->GetMaximum());
2932         h2_timeofMax->Fill(j,i,AllProfiles[i][j]->GetXaxis()->GetBinCenter(AllProfiles[i][j]->GetMaximumBin()));
2933         if(AllProfiles[i][j]->GetBinError(AllProfiles[i][j]->GetMaximumBin())==0 && AllProfiles[i][j]->GetBinContent(AllProfiles[i][j]->GetMaximumBin())>0){
2934           unsat=false;
2935           nSaturated++;
2936         }
2937         for(int ibin=0; ibin<AllProfiles[i][j]->GetNbinsX(); ibin++){
2938           if(ibin+1==AllProfiles[i][j]->GetMaximumBin())continue;
2939           if(AllProfiles[i][j]->GetBinContent(ibin+1)/AllProfiles[i][j]->GetMaximum()>0.95&&AllProfiles[i][j]->GetBinContent(ibin+1)>10000){
2940             unsat=false;
2941             nSaturated++;
2942           }
2943         }
2944         for(int Rmean=1; Rmean<7; Rmean++){
2945           if(AllProfiles[i][j]->GetBinContent(Rmean)){
2946             mean+=AllProfiles[i][j]->GetBinContent(Rmean);
2947             Nmean++;
2948           }
2949         }
2950         h2_pedestal->Fill(j,i,Nmean>0?mean/Nmean:0);//AllProfiles[i][j]->GetBinContent(2));
2951         if(!unsat)h2_saturating->Fill(j,i,nSaturated);
2952       }
2953     }
2954   }
2955   TLine *line_sector[32];
2956   for(int i_line=0;i_line<32;i_line++)
2957     {
2958       line_sector[i_line] = new TLine(0,(i_line+1)*8,96,(i_line+1)*8);
2959       line_sector[i_line]->SetLineColor(1);
2960       line_sector[i_line]->SetLineWidth(1);
2961       line_sector[i_line]->SetLineStyle(1);
2962     }
2963 
2964 
2965   const int numVertDiv = 12;
2966   int dEI = 96/numVertDiv;
2967   TLine *l_board[numVertDiv-1];
2968   for(int il=1; il<numVertDiv; il++){
2969     l_board[il-1] = new TLine(dEI*il,0,dEI*il,256);
2970     l_board[il-1]->SetLineColor(1);
2971     l_board[il-1]->SetLineWidth(1);
2972     l_board[il-1]->SetLineStyle(1);
2973     if(il==6) l_board[il-1]->SetLineWidth(2);
2974   }
2975 
2976   TC[6]->SetEditable(1);
2977   TC[6]->Clear("D");
2978   Pad[15]->cd();
2979   gStyle->SetPalette(kBird);
2980   h2_maxima->DrawCopy("colz");
2981   for(int i_line=0;i_line<32;i_line++) line_sector[i_line]->Draw();
2982   for(int il=0; il<numVertDiv-1; il++) l_board[il]->Draw();
2983   Pad[16]->cd();
2984   gStyle->SetPalette(kBird);
2985   h2_timeofMax->DrawCopy("colz");
2986   for(int i_line=0;i_line<32;i_line++) line_sector[i_line]->Draw();
2987   for(int il=0; il<numVertDiv-1; il++) l_board[il]->Draw();
2988   Pad[17]->cd();
2989   gStyle->SetPalette(kBird);
2990   h2_pedestal->DrawCopy("colz");
2991   for(int i_line=0;i_line<32;i_line++) line_sector[i_line]->Draw();
2992   for(int il=0; il<numVertDiv-1; il++) l_board[il]->Draw();
2993   Pad[18]->cd();
2994   gStyle->SetPalette(kBird);
2995   h2_saturating->DrawCopy("colz");
2996   for(int i_line=0;i_line<32;i_line++) line_sector[i_line]->Draw();
2997   for(int il=0; il<numVertDiv-1; il++) l_board[il]->Draw();
2998 
2999   TText PrintRun;
3000   PrintRun.SetTextFont(62);
3001   PrintRun.SetTextSize(0.04);
3002   PrintRun.SetNDC();          // set to normalized coordinates
3003   PrintRun.SetTextAlign(23);  // center/top alignment
3004   std::ostringstream runnostream;
3005   std::string runstring;
3006   std::pair<time_t,int> evttime = cl->EventTime("CURRENT");
3007   runnostream << "Run " << cl->RunNumber() << ", Time: " << ctime(&evttime.first);
3008   runstring = runnostream.str();
3009   transparent[6]->cd();
3010   PrintRun.SetTextColor(evttime.second);
3011   PrintRun.DrawText(0.5, 0.99, runstring.c_str());
3012 
3013   TC[6]->Update();
3014   TC[6]->Show();
3015   TC[6]->SetEditable(0);
3016   TC[6]->Connect("ProcessedEvent(int, int, int, TObject*)","CemcMonDraw",this,"HandleEvent(int, int, int, TObject*)");
3017   if(save)TC[6] -> SaveAs("plots/Test.pdf");
3018   return 0;
3019 }
3020 
3021 void CemcMonDraw::HandleEvent(int event, int x, int y, TObject* sel){
3022   if(event==1){
3023     TCanvas* canvas=nullptr;
3024     TPad *mypad=nullptr;
3025     if(strcmp(sel->GetName(),"transparent0")==0){
3026       //canvas=TC[0];
3027       //mypad=Pad[0];
3028       return;//we do not want draw from there
3029     }
3030     else if(strcmp(sel->GetName(),"transparent6")==0){
3031       canvas=TC[6];
3032       if(canvas->AbsPixeltoX(x)<0.5){
3033         if(canvas->AbsPixeltoY(y)<0.475){
3034           mypad=Pad[17];
3035         }
3036         else{
3037           mypad=Pad[15];
3038         }
3039       }
3040       else{
3041         if(canvas->AbsPixeltoY(y)<0.475){
3042           mypad=Pad[18];
3043         }
3044         else{
3045           mypad=Pad[16];
3046         }
3047       }
3048 
3049     }
3050     else return;//dummy to avoid the unused warning
3051 
3052     double xeta=mypad->PadtoX(mypad->AbsPixeltoX(x));
3053     double yphi=mypad->PadtoY(mypad->AbsPixeltoY(y));
3054     int ieta=(int) xeta;
3055     int iphi=(int) yphi;
3056     if(ieta<0||ieta>96) return;
3057     if(iphi<0||iphi>256) return;
3058     int iphi_begin=(iphi/8)*8;
3059     int ieta_begin=(ieta/8)*8;
3060     if(!gROOT->FindObject("CemcPopup")) MakeCanvas("CemcPopup");
3061     PopUpCanvas->SetEditable(true);
3062     PopUpCanvas->Clear("D");
3063     for(int i=0; i<8; i++){
3064       for(int j=0; j<8; j++){
3065         if(!gROOT->FindObject(Form("Combinedh2_waveform_phi%d_eta%d",j+iphi_begin,i+ieta_begin))){
3066           return ;
3067           PopUpCanvas->Update();
3068           PopUpCanvas->Show();
3069           PopUpCanvas->SetEditable(false);
3070         }
3071         else{
3072           summedProfile[i][j]=(TProfile*)gROOT->FindObject(Form("Combinedh2_waveform_phi%d_eta%d",j+iphi_begin,i+ieta_begin));
3073         }
3074         if(PopUpPad[i][j]){
3075           PopUpPad[i][j]->cd();
3076         }
3077         else{
3078           std::cout<<"no valid popuppad"<<std::endl;
3079         }
3080         if(summedProfile[i][j]){
3081           summedProfile[i][j]->SetMaximum(pow(2,14));
3082           summedProfile[i][j]->DrawCopy();
3083           summedProfile[i][j]->SetMaximum(-1111);
3084           PopUpPad[i][j]->Update();
3085           PopUpPad[i][j]->Paint();
3086         }
3087         else{
3088           DrawDeadServer(PopUpTransparent);
3089           PopUpCanvas->SetEditable(false);
3090           return;
3091         }
3092       }
3093     }
3094     PopUpCanvas->Update();
3095     PopUpCanvas->Show();
3096     PopUpCanvas->SetEditable(false);
3097   }
3098 }
3099 
3100 ***********************/