Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-04-04 08:16:08

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