Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "BbcMonDraw.h"
0002 
0003 #include <onlmon/OnlMonClient.h>
0004 #include <onlmon/triggerEnum.h>
0005 
0006 #include <TArc.h>
0007 #include <TArrow.h>
0008 #include <TAxis.h>
0009 #include <TCanvas.h>
0010 #include <TDatime.h>
0011 #include <TF1.h>
0012 #include <TFrame.h>
0013 #include <TGraph.h>
0014 #include <TGraphErrors.h>
0015 #include <TH1.h>
0016 #include <TH2.h>
0017 #include <TLatex.h>
0018 #include <TLine.h>
0019 #include <TPad.h>
0020 #include <TPaveText.h>
0021 #include <TROOT.h>
0022 #include <TSpectrum.h>
0023 #include <TStyle.h>
0024 #include <TSystem.h>
0025 #include <TText.h>
0026 #include <THStack.h>
0027 #include <TLegend.h>
0028 
0029 #include <algorithm>
0030 #include <chrono>
0031 #include <cstring>
0032 #include <ctime>
0033 #include <fstream>
0034 #include <iomanip>
0035 #include <iostream>
0036 #include <sstream>
0037 #include <vector>
0038 
0039 #define DEBUG
0040 #ifdef DEBUG
0041 #define PRINT_DEBUG(x) std::cout << (x) << std::endl
0042 #else
0043 #define PRINT_DEBUG(x) {};
0044 #endif
0045 
0046 #ifdef DEBUGNEW
0047 #define ifdelete(x)             \
0048   if (x != nullptr)             \
0049   {                     \
0050     std::cout << "Delete " << #x << std::endl;  \
0051     delete x;                   \
0052     x = nullptr;                \
0053   }
0054 #define ifnew(t, x)                 \
0055   {                         \
0056     if (x != nullptr)                   \
0057     {                           \
0058       std::cout << "Delete " << #x << std::endl;    \
0059       delete x;                     \
0060     }                           \
0061     std::cout << "New " << #x << std::endl;     \
0062     x = new t;                      \
0063   }
0064 #else
0065 #define ifdelete(x)             \
0066   if (x != nullptr)             \
0067   {                     \
0068     delete x;                   \
0069     x = nullptr;                \
0070   }
0071 #define ifnew(t, x)             \
0072   {                     \
0073     if (x != nullptr)               \
0074     {                       \
0075       delete x;                 \
0076     }                       \
0077     x = new t;                  \
0078   }
0079 #endif
0080 
0081 // x position of trigger, scale factor and vtx mean
0082 // static float xpos[3] = {0.30, 0.50, 0.75};
0083 static float xpos[4] = {0.20, 0.35, 0.55, 0.75};
0084 
0085 BbcMonDraw::BbcMonDraw(const std::string &name)
0086   : OnlMonDraw(name)
0087 {
0088   PRINT_DEBUG("In BbcMonDraw::BbcMonDraw()");
0089   std::cout << "Done with BbcMonDraw::BbcMonDraw" << std::endl;
0090   return;
0091 }
0092 
0093 BbcMonDraw::~BbcMonDraw()
0094 {
0095   PRINT_DEBUG("In BbcMonDraw::~BbcMonDraw()");
0096 
0097   ifdelete(bbcStyle);
0098 
0099   // ------------------------------------------------------
0100   // Canvas and Histogram
0101 
0102   ifdelete(PaveTop);
0103   ifdelete(TextTop);
0104 
0105   for (int i = 0; i < BbcMonDefs::MAX_WARNING; i++)
0106   {
0107     ifdelete(PaveWarning[i]);
0108     ifdelete(ArcWarning[i]);
0109   }
0110 
0111   // ------------------------------------------------------
0112   // Graph for visualization
0113 
0114   // for the 1st Page
0115 
0116   for (int side = 0; side < nSIDE; side++)
0117   {
0118     ifdelete(TdcOver[side]);
0119     ifdelete(LineTdcOver[side][0]);
0120     ifdelete(LineTdcOver[side][1]);
0121     ifdelete(BoxTdcOver[side]);
0122 
0123     for (int trig = 0; trig < nTRIGGER; trig++)
0124     {
0125       ifdelete(nHit[trig][side]);
0126       ifdelete(LinenHit[trig][side][0]);
0127       ifdelete(LinenHit[trig][side][1]);
0128       ifdelete(BoxnHit[trig][side]);
0129     }
0130   }
0131   ifdelete(TextnHitStatus);
0132 
0133   // for 2nd Page
0134 
0135   for (int side = 0; side < nSIDE; side++)
0136   {
0137     ifdelete(HitTime[side]);
0138     ifdelete(LineHitTime[side][0]);
0139     ifdelete(LineHitTime[side][1]);
0140     ifdelete(ArrowHitTime[side]);
0141     ifdelete(TextHitTime[side]);
0142     ifdelete(FitHitTime[side]);
0143   }
0144 
0145   ifdelete(AvrHitTime);
0146   ifdelete(LineAvrHitTime[0]);
0147   ifdelete(LineAvrHitTime[1]);
0148   ifdelete(ArrowAvrHitTime);
0149   ifdelete(TextAvrHitTime);
0150   ifdelete(FitAvrHitTime);
0151 
0152   ifdelete(NorthHitTime);
0153   ifdelete(LineNorthHitTime[0]);
0154   ifdelete(LineNorthHitTime[1]);
0155   ifdelete(ArrowNorthHitTime);
0156   ifdelete(TextNorthHitTime);
0157   ifdelete(FitNorthHitTime);
0158   ifdelete(SouthHitTime);
0159   ifdelete(LineSouthHitTime[0]);
0160   ifdelete(LineSouthHitTime[1]);
0161   ifdelete(ArrowSouthHitTime);
0162   ifdelete(TextSouthHitTime);
0163   ifdelete(FitSouthHitTime);
0164 
0165   ifdelete(NorthChargeSum);
0166   ifdelete(SouthChargeSum);
0167   ifdelete(TextSouthChargeSum);
0168   ifdelete(TextNorthChargeSum);
0169 
0170   ifdelete(TextBbcSummaryHitTime[0]);
0171   ifdelete(TextBbcSummaryHitTime[1]);
0172   ifdelete(TextBbcSummaryGlobalOffset[0]);
0173   ifdelete(TextBbcSummaryGlobalOffset[1]);
0174   ifdelete(TextBbcSummaryZvertex);
0175   ifdelete(TextBbcSummaryTrigRate);
0176 
0177   ifdelete(Zvtx);
0178   ifdelete(FitZvtx);
0179   ifdelete(LineZvtx[0]);
0180   ifdelete(LineZvtx[1]);
0181   ifdelete(ArrowZvtx);
0182   ifdelete(TextZvtx);
0183   ifdelete(TextZvtxNorth);
0184   ifdelete(TextZvtxSouth);
0185   ifdelete(TextZvtxStatus[0]);
0186   ifdelete(TextZvtxStatus[1]);
0187   ifdelete(TextZvtxStatus[2]);
0188   ifdelete(TextZvtxStatus[3]);
0189 
0190   ifdelete(RunVtx);
0191   ifdelete(RunVtxErr);
0192   ifdelete(RunVtxTime);
0193 
0194   ifdelete(ArmHit);
0195   ifdelete(TextArmHit);
0196   ifdelete(ArcArmHit);
0197 
0198   // for 3rd Page
0199   ifdelete(South_Nhit);
0200   ifdelete(North_Nhit);
0201   for (int iarm=0; iarm<2; iarm++)
0202   {
0203     ifdelete(Nhit_emcal[iarm]);
0204     ifdelete(Nhit_hcal[iarm]);
0205     ifdelete(Nhit_emcalmbd[iarm]);
0206     ifdelete(Nhit_hcalmbd[iarm]);
0207   }
0208 
0209   ifdelete(Zvtx);
0210   ifdelete(Zvtx_alltrigger);
0211   ifdelete(Zvtx_ns);
0212   ifdelete(Zvtx_10);
0213   ifdelete(Zvtx_30);
0214   ifdelete(Zvtx_60);
0215   ifdelete(Zvtx_ns_chk);
0216   ifdelete(Zvtx_10_chk);
0217   ifdelete(Zvtx_30_chk);
0218   ifdelete(Zvtx_60_chk);
0219   ifdelete(Zvtx_zdcns);
0220   ifdelete(Zvtx_emcal);
0221   ifdelete(Zvtx_hcal);
0222   ifdelete(Zvtx_emcalmbd);
0223   ifdelete(Zvtx_hcalmbd);
0224 
0225   // for 4th Page
0226   ifdelete(TzeroZvtx);
0227   ifdelete(TextZVertexNotice);
0228 
0229   ifdelete(Adc);
0230   ifdelete(MultiView1F);
0231   ifdelete(MultiView2F);
0232   ifdelete(PaveWarnings);
0233 
0234   ifdelete(LineTzeroZvtx[0]);
0235   ifdelete(LineTzeroZvtx[1]);
0236   ifdelete(LineTzeroZvtx[2]);
0237   ifdelete(LineTzeroZvtx[3]);
0238   ifdelete(TextTzeroZvtx);
0239 
0240   // for (int i = 0; i < 4; i++)//need a look
0241   for (int i = 0; i < 5; i++)
0242   {
0243     ifdelete(TextZVertex[i]);
0244     ifdelete(TextZVertex_scale[i]);
0245     ifdelete(TextZVertex_mean[i]);
0246   }
0247   ifdelete(FitZvtx);
0248   ifdelete(TextZvtxStatus[0]);
0249   ifdelete(TextZvtxStatus[1]);
0250   ifdelete(TextZvtxStatus[2]);
0251   ifdelete(TextZvtxStatus[3]);
0252 
0253   // segfaults since root does not set these pointers to zero
0254   // when canvas is deleted
0255   // for (auto &icv : TC)
0256   // {
0257   //   ifdelete(icv);
0258   // }
0259 
0260   ifdelete(Prescale_hist);
0261   ifdelete(tspec);
0262   ifdelete(gRunVtx);
0263   ifdelete(gRunAvgVtx);
0264 
0265   return;
0266 }
0267 
0268 int BbcMonDraw::UpdateSendFlag(const int flag)
0269 {
0270   sendflag = flag;
0271   std::ofstream sendflagfile( sendflagfname );
0272   if ( sendflagfile.is_open() )
0273   {
0274     sendflagfile << sendflag << std::endl;
0275   }
0276   else
0277   {
0278     std::cout << "UpdateSendFlag, unable to open file " << sendflagfname << std::endl;
0279     return 0;
0280   }
0281   sendflagfile.close();
0282   return 1;
0283 }
0284 
0285 int BbcMonDraw::GetSendFlag()
0286 {
0287   std::ifstream sendflagfile( sendflagfname );
0288   if ( sendflagfile.is_open() )
0289   {
0290     sendflagfile >> sendflag;
0291   }
0292   else
0293   {
0294     std::cout << "GetSendFlag, unable to open file " << sendflagfname << std::endl;
0295     sendflag = 0;
0296   }
0297   sendflagfile.close();
0298 
0299   return sendflag;
0300 }
0301 
0302 int BbcMonDraw::UpdateZResetFlag(const int flag)
0303 {
0304   zresetflag = flag;
0305   std::ofstream zresetflagfile( zresetflagfname );
0306   if ( zresetflagfile.is_open() )
0307   {
0308     zresetflagfile << zresetflag << std::endl;
0309   }
0310   else
0311   {
0312     std::cout << "UpdatezresetFlag, unable to open file " << zresetflagfname << std::endl;
0313     return 0;
0314   }
0315   zresetflagfile.close();
0316   return 1;
0317 }
0318 
0319 int BbcMonDraw::GetZResetFlag()
0320 {
0321   std::ifstream zresetflagfile( zresetflagfname );
0322   if ( zresetflagfile.is_open() )
0323   {
0324     zresetflagfile >> zresetflag;
0325   }
0326   else
0327   {
0328     std::cout << "GetZResetFlag, unable to open file " << zresetflagfname << std::endl;
0329     zresetflag = 0;
0330   }
0331   zresetflagfile.close();
0332 
0333   return zresetflag;
0334 }
0335 
0336 int BbcMonDraw::UpdateGL1BadFlag(const int flag)
0337 {
0338   gl1badflag = flag;
0339   std::ofstream gl1badflagfile( gl1badflagfname );
0340   if ( gl1badflagfile.is_open() )
0341   {
0342     gl1badflagfile << gl1badflag << std::endl;
0343   }
0344   else
0345   {
0346     std::cout << "unable to open file " << gl1badflagfname << std::endl;
0347     return 0;
0348   }
0349   gl1badflagfile.close();
0350   return 1;
0351 }
0352 
0353 int BbcMonDraw::GetGL1BadFlag()
0354 {
0355   std::ifstream gl1badflagfile( gl1badflagfname );
0356   if ( gl1badflagfile.is_open() )
0357   {
0358     gl1badflagfile >> gl1badflag;
0359   }
0360   else
0361   {
0362     std::cout << "unable to open file " << gl1badflagfname << std::endl;
0363     gl1badflag = 0;
0364   }
0365   gl1badflagfile.close();
0366 
0367   return gl1badflag;
0368 }
0369 
0370 
0371 int BbcMonDraw::Init()
0372 {
0373   PRINT_DEBUG("In BbcMonDraw::Init()");
0374   TStyle *oldStyle = gStyle;
0375   bbcStyle = new TStyle("bbcStyle", "MBD Online Monitor Style");
0376   bbcStyle->SetOptStat(0);
0377   bbcStyle->SetTitleH(0.075);
0378   bbcStyle->SetTitleW(0.98);
0379   bbcStyle->SetPalette(1);
0380   bbcStyle->SetFrameBorderMode(0);
0381   bbcStyle->SetPadBorderMode(0);
0382   bbcStyle->SetCanvasBorderMode(0);
0383   oldStyle->cd();
0384 
0385   /*
0386     for ( auto server : m_ServerSet )
0387     {
0388     std::cout << "servers " << server << std::endl;
0389     }
0390   */
0391   
0392   // prep the vtx to MCR info
0393   sendflagfname = "/home/phnxrc/operations/mbd/mbd2mcr.seb18";
0394   std::cout << "sendflagfname " << sendflagfname << std::endl;
0395   GetSendFlag();
0396 
0397   // prep z-reset
0398   zresetflagfname = "/home/phnxrc/operations/mbd/mbdzreset.seb18";
0399 
0400   // ------------------------------------------------------
0401   // Canvas and Histogram
0402 
0403   memset(TC, 0, sizeof(TC));
0404   memset(transparent, 0, sizeof(transparent));
0405   memset(PadTop, 0, sizeof(PadTop));
0406   memset(PaveWarning, 0, sizeof(PaveWarning));
0407   memset(ArcWarning, 0, sizeof(ArcWarning));
0408   memset(PadWarning, 0, sizeof(PadWarning));
0409   memset(nPadWarning, 0, sizeof(nPadWarning));
0410   memset(PadAutoUpdate, 0, sizeof(PadAutoUpdate));
0411   // ------------------------------------------------------
0412   // Graph for visualization
0413 
0414   // for the 1st Page
0415   memset(TdcOver, 0, sizeof(TdcOver));
0416   memset(PadTdcOver, 0, sizeof(PadTdcOver));
0417   memset(LineTdcOver, 0, sizeof(LineTdcOver));
0418   memset(BoxTdcOver, 0, sizeof(BoxTdcOver));
0419   memset(FrameTdcOver, 0, sizeof(FrameTdcOver));
0420   memset(PadnHit, 0, sizeof(PadnHit));
0421   memset(nHit, 0, sizeof(nHit));
0422   memset(LinenHit, 0, sizeof(LinenHit));
0423   memset(BoxnHit, 0, sizeof(BoxnHit));
0424   memset(FramenHit, 0, sizeof(FramenHit));
0425 
0426   // for 2nd Page
0427 
0428   memset(HitTime, 0, sizeof(HitTime));
0429   memset(PadHitTime, 0, sizeof(PadHitTime));
0430   memset(LineHitTime, 0, sizeof(LineHitTime));
0431   memset(ArrowHitTime, 0, sizeof(ArrowHitTime));
0432   memset(TextHitTime, 0, sizeof(TextHitTime));
0433   memset(FitHitTime, 0, sizeof(FitHitTime));
0434   memset(LineAvrHitTime, 0, sizeof(LineAvrHitTime));
0435 
0436   memset(LineNorthHitTime, 0, sizeof(LineNorthHitTime));
0437   memset(LineSouthHitTime, 0, sizeof(LineSouthHitTime));
0438 
0439   memset(TextBbcSummaryHitTime, 0, sizeof(TextBbcSummaryHitTime));
0440   memset(TextBbcSummaryGlobalOffset, 0, sizeof(TextBbcSummaryGlobalOffset));
0441 
0442   memset(LineZvtx, 0, sizeof(LineZvtx));
0443 
0444   memset(LineTzeroZvtx, 0, sizeof(LineTzeroZvtx));
0445 
0446   memset(TextZVertex, 0, sizeof(TextZVertex));
0447   memset(TextZVertex_scale, 0, sizeof(TextZVertex_scale));
0448   memset(TextZVertex_mean, 0, sizeof(TextZVertex_mean));
0449 
0450   // memset(Pad, 0, sizeof(Pad));
0451 
0452   FitZvtx = new TF1("FitZvtx", "gaus", -60, 60);
0453   TextZvtxStatus[0] = new TLatex;
0454   TextZvtxStatus[1] = new TLatex;
0455   TextZvtxStatus[2] = new TLatex;
0456   TextZvtxStatus[3] = new TLatex;
0457 
0458   tspec = new TSpectrum(5);  // 5 peaks is enough - we have 4
0459 
0460   gRunVtx = new TGraphErrors();
0461   gRunVtx->SetName("gRunVtx");
0462   gRunVtx->SetTitle("Zvertex vs. Time");
0463   gRunVtx->GetHistogram()->SetXTitle("time [s]");
0464   gRunVtx->GetHistogram()->SetYTitle("Z_{MBD} [cm]");
0465   gRunVtx->GetHistogram()->GetXaxis()->SetTitleSize(0.08);
0466   gRunVtx->GetHistogram()->GetXaxis()->SetTitleOffset(0.5);
0467   gRunVtx->GetHistogram()->GetXaxis()->SetLabelSize(0.1);
0468   gRunVtx->GetHistogram()->GetXaxis()->SetTickSize(0.1);
0469   gRunVtx->GetHistogram()->GetYaxis()->SetTitleSize(0.08);
0470   gRunVtx->GetHistogram()->GetYaxis()->SetTitleOffset(0.35);
0471   gRunVtx->GetHistogram()->GetYaxis()->SetLabelSize(0.1);
0472   //gRunVtx->GetHistogram()->GetYaxis()->SetTickSize(0.05);
0473 
0474   gRunAvgVtx = new TGraph();
0475   gRunAvgVtx->SetName("gRunAvgVtx");
0476   gRunAvgVtx->SetLineWidth(2);
0477 
0478   // Create Empty Histogram for Running Vtx Plot when plot is blank
0479   EmptyHist = new TH1F("EmptyHist","",1,0,1);
0480   EmptyHist->SetTitle("Zvertex vs. Time");
0481   EmptyHist->SetXTitle("time [s]");
0482   EmptyHist->SetYTitle("Z_{MBD} [cm]");
0483   EmptyHist->GetXaxis()->SetTitleSize(0.08);
0484   EmptyHist->GetXaxis()->SetTitleOffset(0.5);
0485   EmptyHist->GetXaxis()->SetLabelSize(0.1);
0486   EmptyHist->GetXaxis()->SetTickSize(0.1);
0487   EmptyHist->GetYaxis()->SetTitleSize(0.08);
0488   EmptyHist->GetYaxis()->SetTitleOffset(0.35);
0489   EmptyHist->GetYaxis()->SetLabelSize(0.1);
0490 
0491   return 0;
0492 }
0493 
0494 int BbcMonDraw::ClearWarning()
0495 {
0496   for (int i = 0; i < nWarning; i++)
0497   {
0498     ifdelete(PaveWarning[i]);
0499     ifdelete(ArcWarning[i]);
0500   }
0501   nWarning = 0;
0502 
0503   if (PaveWarnings)
0504   {
0505     PaveWarnings->Clear();
0506   }
0507   // if( PadWarnings )
0508   // PaveWarnings->Draw();
0509   return 0;
0510 }
0511 
0512 int BbcMonDraw::Warning(TPad *pad, const float x, const float y, const int r, const std::string &msg)
0513 {
0514   static int brink = 1;
0515   float x1, x2, y1, y2;
0516 
0517   x1 = pad->GetX1() * 0.97 + pad->GetX2() * 0.03;
0518   x2 = pad->GetX1() * 0.03 + pad->GetX2() * 0.97;
0519   // pave rises on top if error point is lower
0520   if (y > pad->GetY1() * 0.5 + pad->GetY2() * 0.5)
0521   {
0522     y1 = pad->GetY1() + (pad->GetY2() - pad->GetY1()) * 0.10;
0523     y2 = pad->GetY1() + (pad->GetY2() - pad->GetY1()) * 0.30;
0524   }
0525   else
0526   {
0527     y1 = pad->GetY1() + (pad->GetY2() - pad->GetY1()) * 0.70;
0528     y2 = pad->GetY1() + (pad->GetY2() - pad->GetY1()) * 0.90;
0529   }
0530 
0531   int newWarning = nWarning;
0532   for (int i = 0; i < nWarning; i++)
0533   {
0534     if (PadWarning[i] == pad)
0535     {
0536       newWarning = i;
0537     }
0538   }
0539 
0540   ifnew(TArc(x, y, r), ArcWarning[newWarning]);
0541   ArcWarning[newWarning]->SetLineWidth(r);
0542   ArcWarning[newWarning]->SetLineColor(2);
0543 
0544   ifnew(TPaveText(x1, y1, x2, y2), PaveWarning[newWarning]);
0545   PaveWarning[newWarning]->AddText(msg.c_str());
0546   ArcWarning[newWarning]->SetLineColor(2 + 3 * brink);
0547   PaveWarning[newWarning]->SetFillColor(5 + 5 * brink);
0548   PaveWarning[newWarning]->SetTextColor(2);
0549   PaveWarning[newWarning]->SetLineColor(2);
0550   PaveWarning[newWarning]->SetLineWidth(2);
0551 
0552   if (newWarning == nWarning)
0553   {
0554     PadWarning[newWarning] = pad;
0555     nPadWarning[newWarning] = 1;
0556     nWarning++;
0557   }
0558   else
0559   {
0560     std::ostringstream wmsg;
0561     wmsg << "... and other " << nPadWarning[newWarning] << " warnings";
0562     nPadWarning[newWarning]++;
0563     PaveWarning[newWarning]->AddText(wmsg.str().c_str());
0564     wmsg.str("");
0565   }
0566   // PaveWarning[newWarning]->AddText( "Call MBD Expert. If this run is a PHYSICS run " );
0567   PaveWarning[newWarning]->AddText("Do Quick-feed MBD");
0568   PaveWarning[newWarning]->AddText("If it is NOT fixed by feed at PHYSICS run, call MBD experts");
0569   // PaveWarning[newWarning]->AddText( "If it is NOT fixed by quick feed at PHYSICS run, conform to the MBD standing orders" );
0570 
0571   if (PaveWarnings)
0572   {
0573     PaveWarnings->AddText(msg.c_str());
0574   }
0575 
0576   if (PadWarnings && PaveWarnings)
0577   {
0578     PaveWarnings->Draw();
0579   }
0580   // ArcWarning[newWarning]->Draw();
0581   PaveWarning[newWarning]->Draw();
0582 
0583   // brink = 1 - brink;
0584 
0585   if (nWarning == BbcMonDefs::MAX_WARNING - 2)
0586   {
0587     std::string bmsg = "Too Many Warnings";
0588     Warning(pad, x, y, 0, bmsg);
0589     bmsg.erase();
0590   }
0591 
0592   return 0;
0593 }
0594 
0595 int BbcMonDraw::MakeCanvas(const std::string &name)
0596 {
0597   PRINT_DEBUG("In BbcMonDraw::MakeCanvas()");
0598   OnlMonClient *cl = OnlMonClient::instance();
0599   int xsize = cl->GetDisplaySizeX();
0600   int ysize = cl->GetDisplaySizeY();
0601 
0602   if (name == "BbcMon1")
0603   {
0604     std::cout << "Creating Canvas BbcMon1..." << std::endl;
0605 
0606     TC[0] = new TCanvas("BbcMon1", "MBD Z-Vertex View for Shift crew", -1, 0, xsize / 2, ysize);
0607 
0608     // root is pathetic, whenever a new TCanvas is created root piles up
0609     // 6kb worth of X11 events which need to be cleared with
0610     // gSystem->ProcessEvents(), otherwise your process will grow and
0611     // grow and grow but will not show a defninitely lost memory leak
0612     gSystem->ProcessEvents();
0613 
0614     TC[0]->cd();
0615     PadTop[0] = new TPad("PadTop0", "PadTop0", 0.00, 0.90, 1.00, 1.00, 0, 0, 0);
0616     PadZVertex = new TPad("PadZVertex", "PadZVertex", 0.00, 0.70, 1.00, 0.90, 0, 0, 0);
0617     PadZVertexSummary = new TPad("PadZVertexSummary", "PadZVertexSummary", 0.00, 0.55, 1.00, 0.70, 0, 0, 0);
0618     PadRunZVertex = new TPad("PadRunZVertex", "PadRunZVertex", 0.00, 0.40, 1.00, 0.55, 0, 0, 0);
0619     PadSouthHitMap = new TPad("PadSouthHitMap", "PadSouthHitMap", 0.00, 0.00, 0.495, 0.40, 0, 0, 0);
0620     PadNorthHitMap = new TPad("PadNorthHitMap", "PadNorthHitMap", 0.505, 0.00, 1.0, 0.40, 0, 0, 0);
0621     // PadTzeroZVertex = new TPad("PadTzeroZVertex", "PadTzeroZVertex", 0.00, 0.00, 1.00, 0.40, 0, 0, 0);
0622 
0623     PadTop[0]->Draw();
0624     // PadZVertex->SetLogy();
0625     PadZVertex->Draw();
0626     PadRunZVertex->Draw();
0627     // PadTzeroZVertex->Draw();
0628     PadSouthHitMap->Draw();
0629     PadNorthHitMap->Draw();
0630     PadZVertexSummary->Draw();
0631 
0632     PadZVertexSummary->cd();
0633 
0634     // ifnew( TText, TextZVertexNotice );
0635     // TextZVertexNotice->SetTextSize(0.08);
0636     // TextZVertexNotice->SetText(0.05, 0.75, "< Z vertex deviation may NOT be due to MBD, don't page expert easily! >");
0637 
0638     // for (int i = 0; i < 4; i++)
0639     for (int i = 0; i < 1; i++)  //  pp
0640     {
0641       ifnew(TText, TextZVertex[i]);
0642       TextZVertex[i]->SetTextColor(BbcMonDefs::BBC_COLOR_ZVTX[i]);
0643       // TextZVertex[i]->SetTextSize(0.15);
0644       TextZVertex[i]->SetTextSize(0.08);
0645 
0646       ifnew(TText, TextZVertex_scale[i]);
0647       ifnew(TText, TextZVertex_mean[i]);
0648       TextZVertex_scale[i]->SetTextColor(BbcMonDefs::BBC_COLOR_ZVTX[i]);
0649       TextZVertex_mean[i]->SetTextColor(BbcMonDefs::BBC_COLOR_ZVTX[i]);
0650       TextZVertex_scale[i]->SetTextSize(0.08);
0651       TextZVertex_mean[i]->SetTextSize(0.08);
0652     }
0653     /*chiu
0654       TextZVertex[0]->SetText(xpos[0], 0.65, "Zbbc [BBLL1]");       // RUN11 pp
0655     */
0656     TC[0]->cd();
0657     transparent[0] = new TPad("transparent0", "this does not show", 0, 0, 1, 1, 0, 0);
0658     transparent[0]->SetFillStyle(4000);
0659     transparent[0]->Draw();
0660 
0661     /*
0662       ifnew(TLine(BbcMonDefs::BBC_MIN_REGULAR_ZVERTEX_MEAN, -6,
0663       BbcMonDefs::BBC_MIN_REGULAR_ZVERTEX_MEAN, 16),
0664       LineTzeroZvtx[0]);
0665       ifnew(TLine(BbcMonDefs::BBC_MAX_REGULAR_ZVERTEX_MEAN, -6,
0666       BbcMonDefs::BBC_MAX_REGULAR_ZVERTEX_MEAN, 16),
0667       LineTzeroZvtx[1]);
0668       ifnew(TLine(-200, BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN - 5,
0669       200, BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN - 5),
0670       LineTzeroZvtx[2]);
0671       ifnew(TLine(-200, BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN - 5,
0672       200, BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN - 5),
0673       LineTzeroZvtx[3]);
0674       ifnew(TText, TextTzeroZvtx);
0675     */
0676   }
0677 
0678   else if (name == "BbcMon2")
0679   {
0680     std::cout << "Creating Canvas BbcMon2..." << std::endl;
0681 
0682     TC[1] = new TCanvas("BbcMon2", "Beam status view for Shift crew", -xsize / 2, 0, xsize / 2, ysize);
0683     gSystem->ProcessEvents();
0684     TC[1]->cd();
0685     PadTop[1] = new TPad("PadTop1", "PadTop1", 0.00, 0.90, 1.00, 1.00, 0, 0, 0);
0686     PadTimeWave = new TPad("PadTimeWave", "PadTimeWave", 0.00, 0.30, 1.00, 0.90, 0, 0, 0);
0687     PadSouthHitTime = new TPad("PadHitTimeSouth1", "PadHitTimeSouth1", 0.00, 0.00, 0.50, 0.30, 0, 0, 0);
0688     PadNorthHitTime = new TPad("PadHitTimeNorth1", "PadHitTimeNorth1", 0.50, 0.00, 1.00, 0.30, 0, 0, 0);
0689 
0690     // PadArmHit = new TPad("PadArmHit", "PadArmHit", 0.50, 0.00, 1.00, 0.40, 0, 0, 0);
0691     // PadBbcSummary = new TPad("PadBbcSummary", "PadBbcSummary", 0.00, 0.40, 1.00, 0.55, 0, 0, 0);
0692     // PadAvrHitTime = new TPad("PadAvrHitTime", "PadAvrHitTime", 0.00, 0.00, 0.50, 0.40, 0, 0, 0);
0693 
0694     // PadArmHit->SetLogz();
0695 
0696     PadTop[1]->Draw();
0697     if (PadTimeWave)
0698     {
0699       PadTimeWave->Draw();
0700     }
0701     if (PadNorthHitTime)
0702     {
0703       PadNorthHitTime->Draw();
0704     }
0705     if (PadSouthHitTime)
0706     {
0707       PadSouthHitTime->Draw();
0708     }
0709     if (PadArmHit)
0710     {
0711       PadArmHit->Draw();
0712     }
0713     if (PadBbcSummary)
0714     {
0715       PadBbcSummary->Draw();
0716     }
0717     if (PadAvrHitTime)
0718     {
0719       PadAvrHitTime->Draw();
0720     }
0721 
0722     ifnew(TText, TextBbcSummaryHitTime[0]);
0723     ifnew(TText, TextBbcSummaryHitTime[1]);
0724     ifnew(TText, TextBbcSummaryZvertex);
0725     ifnew(TText, TextBbcSummaryGlobalOffset[0]);
0726     ifnew(TText, TextBbcSummaryGlobalOffset[1]);
0727     // TextBbcSummaryHitTime->SetTextSize(0.2);
0728     TextBbcSummaryHitTime[0]->SetTextSize(0.2);
0729     TextBbcSummaryHitTime[1]->SetTextSize(0.2);
0730     TextBbcSummaryZvertex->SetTextSize(0.2);
0731     TextBbcSummaryGlobalOffset[0]->SetTextSize(0.2);
0732     TextBbcSummaryGlobalOffset[1]->SetTextSize(0.2);
0733 
0734     ifnew(TArc(10.5, 10.5, 4, 0, 360), ArcArmHit);
0735     ArcArmHit->SetFillStyle(4000);
0736     // ifnew( TArc(10.5, 10.5, 4), ArcArmHit );
0737     ifnew(TText, TextArmHit);
0738     TextArmHit->SetTextSize(0.08);
0739     TextArmHit->SetText(10, 4, "Good region");
0740 
0741     // PadSouthHitTime = new TPad("PadSouthHitTime", "PadSouthHitTime", 0.00, 0.55, 0.50, 0.90, 0);
0742     // PadNorthHitTime = new TPad("PadNorthHitTime", "PadNorthHitTime", 0.50, 0.55, 1.00, 0.90, 0);
0743 
0744     if (PadAvrHitTime)
0745     {
0746       ifnew(TLine(BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 0,
0747                   BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 10000),
0748             LineAvrHitTime[0]);
0749       ifnew(TLine(BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 0,
0750                   BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000),
0751             LineAvrHitTime[1]);
0752 
0753       ifnew(TArrow(BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 10000,
0754                    BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000),
0755             ArrowAvrHitTime);
0756       ArrowAvrHitTime->SetOption("<>");
0757 
0758       ifnew(TText, TextAvrHitTime);
0759       TextAvrHitTime->SetTextSize(0.08);
0760       TextAvrHitTime->SetText(BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000, " good mean");
0761 
0762       LineAvrHitTime[0]->SetLineColor(9);
0763       LineAvrHitTime[1]->SetLineColor(9);
0764       ArrowAvrHitTime->SetLineColor(9);
0765       TextAvrHitTime->SetTextColor(9);
0766     }
0767 
0768     if (PadSouthHitTime)
0769     {
0770       ifnew(TLine(BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 0,
0771                   BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 10000),
0772             LineSouthHitTime[0]);
0773       ifnew(TLine(BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 0,
0774                   BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000),
0775             LineSouthHitTime[1]);
0776 
0777       ifnew(TArrow(BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 10000,
0778                    BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000),
0779             ArrowSouthHitTime);
0780       ArrowSouthHitTime->SetOption("<>");
0781 
0782       ifnew(TText, TextSouthHitTime);
0783       TextSouthHitTime->SetTextSize(0.08);
0784       TextSouthHitTime->SetText(BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000, " good mean");
0785 
0786       LineSouthHitTime[0]->SetLineColor(9);
0787       LineSouthHitTime[1]->SetLineColor(9);
0788       ArrowSouthHitTime->SetLineColor(9);
0789       TextSouthHitTime->SetTextColor(9);
0790     }
0791 
0792     if (PadNorthHitTime)
0793     {
0794       ifnew(TLine(BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 0,
0795                   BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 10000),
0796             LineNorthHitTime[0]);
0797       ifnew(TLine(BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 0,
0798                   BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000),
0799             LineNorthHitTime[1]);
0800 
0801       ifnew(TArrow(BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 10000,
0802                    BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000),
0803             ArrowNorthHitTime);
0804       ArrowNorthHitTime->SetOption("<>");
0805 
0806       ifnew(TText, TextNorthHitTime);
0807       TextNorthHitTime->SetTextSize(0.08);
0808       TextNorthHitTime->SetText(BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000, " good mean");
0809 
0810       LineNorthHitTime[0]->SetLineColor(9);
0811       LineNorthHitTime[1]->SetLineColor(9);
0812       ArrowNorthHitTime->SetLineColor(9);
0813       TextNorthHitTime->SetTextColor(9);
0814     }
0815 
0816     transparent[1] = new TPad("transparent1", "this does not show", 0, 0, 1, 1, 0, 0);
0817     transparent[1]->SetFillStyle(4000);
0818     transparent[1]->Draw();
0819   }
0820 
0821   //
0822   // 3rd Page
0823   else if (name == "BbcMon3")
0824   {
0825     std::cout << "Creating Canvas BbcMon3..." << std::endl;
0826 
0827     // ifnew( TText, TextBbcSummaryTrigRate );
0828     ifnew(TLatex, TextBbcSummaryTrigRate);
0829     // TextBbcSummaryTrigRate->SetTextSize(0.109);
0830     TextBbcSummaryTrigRate->SetTextSize(0.08);
0831     // TextBbcSummaryTrigRate->SetTextSize(0.075);
0832 
0833     TC[2] = new TCanvas("BbcMon3", "Trigger view for Shift crew", -1, 0, xsize / 2, ysize);
0834     gSystem->ProcessEvents();
0835     TC[2]->cd();
0836 
0837     PadTop[2] = new TPad("PadTop2", "PadTop3", 0.00, 0.90, 1.00, 1.00, 0, 0, 0);
0838     PadTop[2]->Draw();
0839 
0840     PadZvtx  = new TPad("PadZvtx", "PadZvtx", 0.00, 0.60, 0.50, 0.90, 0, 0, 0);
0841     PadNhits = new TPad("PadNhits", "PadNhits", 0.50, 0.60, 1.00, 0.90, 0, 0, 0);
0842     PadZvtx->SetLeftMargin(0.17);
0843     PadNhits->SetLeftMargin(0.17);
0844     PadZvtx->Draw();
0845     PadNhits->Draw();
0846 
0847     /*
0848       PadHitTime[0] = new TPad("PadHitTimeSouth1", "PadHitTimeSouth1", 0.00, 0.60, 0.50, 0.90, 0, 0, 0);
0849       PadHitTime[1] = new TPad("PadHitTimeNorth1", "PadHitTimeNorth1", 0.50, 0.60, 1.00, 0.90, 0, 0, 0);
0850       PadHitTime[0]->SetLeftMargin(0.17);
0851       PadHitTime[1]->SetLeftMargin(0.17);
0852       PadHitTime[0]->Draw();
0853       PadHitTime[1]->Draw();
0854 
0855       for (int side = 0; side < nSIDE; side++)
0856       {
0857       ifnew(TLine(BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 0, BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 10000), LineHitTime[side][0]);
0858       ifnew(TLine(BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 0, BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000), LineHitTime[side][1]);
0859 
0860       ifnew(TArrow(BbcMonDefs::BBC_MIN_REGULAR_TDC1_MEAN, 10000,
0861       BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000),
0862       ArrowHitTime[side]);
0863       ArrowHitTime[side]->SetOption("<>");
0864 
0865       ifnew(TText, TextHitTime[side]);
0866       TextHitTime[side]->SetTextSize(0.08);
0867       TextHitTime[side]->SetText(BbcMonDefs::BBC_MAX_REGULAR_TDC1_MEAN, 10000, " good mean");
0868 
0869       LineHitTime[side][0]->SetLineColor(9);
0870       LineHitTime[side][1]->SetLineColor(9);
0871       ArrowHitTime[side]->SetLineColor(9);
0872       TextHitTime[side]->SetTextColor(9);
0873       }
0874     */
0875 
0876     PadZvtxEMCAL  = new TPad("PadZvtxEMCAL", "PadZvtxEMCAL", 0.00, 0.30, 0.50, 0.60, 0, 0, 0);
0877     PadNhitsEMCAL = new TPad("PadNhitsEMCAL", "PadNhitsEMCAL", 0.50, 0.30, 1.00, 0.60, 0, 0, 0);
0878     PadZvtxEMCAL->SetLeftMargin(0.17);
0879     PadNhitsEMCAL->SetLeftMargin(0.17);
0880     PadZvtxEMCAL->Draw();
0881     PadNhitsEMCAL->Draw();
0882 
0883     PadZvtxHCAL  = new TPad("PadZvtxHCAL", "PadZvtxHCAL", 0.00, 0.00, 0.50, 0.30, 0, 0, 0);
0884     PadNhitsHCAL = new TPad("PadNhitsHCAL", "PadNhitsHCAL", 0.50, 0.00, 1.00, 0.30, 0, 0, 0);
0885     PadZvtxHCAL->SetLeftMargin(0.17);
0886     PadNhitsHCAL->SetLeftMargin(0.17);
0887     PadZvtxHCAL->Draw();
0888     PadNhitsHCAL->Draw();
0889 
0890     /*
0891     // for Zvtx
0892     PadZvtx = new TPad("PadZvtx", "PadZvtx", 0.00, 0.30, 0.50, 0.60, 0, 0, 0);
0893     if (PadZvtx)
0894     {
0895     PadZvtx->SetLeftMargin(0.17);
0896     PadZvtx->Draw();
0897     ifnew(TLine(BbcMonDefs::BBC_MIN_REGULAR_ZVERTEX_MEAN, 0,
0898     BbcMonDefs::BBC_MIN_REGULAR_ZVERTEX_MEAN, 10000),
0899     LineZvtx[0]);
0900     ifnew(TLine(BbcMonDefs::BBC_MAX_REGULAR_ZVERTEX_MEAN, 0,
0901     BbcMonDefs::BBC_MAX_REGULAR_ZVERTEX_MEAN, 10000),
0902     LineZvtx[1]);
0903 
0904     ifnew(TArrow(BbcMonDefs::BBC_MIN_REGULAR_ZVERTEX_MEAN, 10000,
0905     BbcMonDefs::BBC_MAX_REGULAR_ZVERTEX_MEAN, 10000),
0906     ArrowZvtx);
0907     ArrowZvtx->SetOption("<>");
0908 
0909     ifnew(TText, TextZvtx);
0910     TextZvtx->SetTextSize(0.08);
0911     TextZvtx->SetText(BbcMonDefs::BBC_MAX_REGULAR_ZVERTEX_MEAN, 10000, " good mean");
0912     ifnew(TText, TextZvtxNorth);
0913     TextZvtxNorth->SetTextSize(0.05);
0914     TextZvtxNorth->SetText(130, 0, "North");
0915     ifnew(TText, TextZvtxSouth);
0916     TextZvtxSouth->SetTextSize(0.05);
0917     TextZvtxSouth->SetText(-160, 0, "South");
0918 
0919     LineZvtx[0]->SetLineColor(9);
0920     LineZvtx[1]->SetLineColor(9);
0921     ArrowZvtx->SetLineColor(9);
0922     TextZvtx->SetTextColor(9);
0923     }
0924     */
0925 
0926     /*
0927       PadChargeSum = new TPad("PadChargeSum", "PadCHargeSum", 0.50, 0.30, 1.00, 0.60, 0, 0, 0);
0928       PadChargeSum->SetLogy();
0929       PadChargeSum->Draw();
0930 
0931       ifnew(TText, TextNorthChargeSum);
0932       // SetNDC(): this will use normalized coordinates (0-1/0-1) for drawing no matter what the histogram uses
0933       TextNorthChargeSum->SetNDC();
0934       TextNorthChargeSum->SetTextSize(0.08);
0935       TextNorthChargeSum->SetText(0.3, 0.8, "--North");  // for p+p
0936       TextNorthChargeSum->SetTextColor(4);
0937 
0938       ifnew(TText, TextSouthChargeSum);
0939       // SetNDC(): this will use normalized coordinates (0-1/0-1) for drawing no matter what the histogram uses
0940       TextSouthChargeSum->SetNDC();
0941       TextSouthChargeSum->SetTextSize(0.08);
0942       TextSouthChargeSum->SetText(0.6, 0.8, "--South");  // for p+p
0943       TextSouthChargeSum->SetTextColor(2);
0944     */
0945 
0946     /*
0947       PadAdc = new TPad("PadAdc", "PadAdc", 0.00, 0.00, 1.00, 0.30, 0, 0, 0);
0948       PadAdc->SetLogz();
0949       PadAdc->Draw();
0950     */
0951 
0952     transparent[2] = new TPad("transparent2", "this does not show", 0, 0, 1, 1);
0953     transparent[2]->SetFillStyle(4000);
0954     transparent[2]->Draw();
0955 
0956     // PadHitTime[1][0]->SetLogy();
0957     // PadHitTime[1][1]->SetLogy();
0958 
0959     /*
0960       ifnew( TPad("PadButton" , "PadButton" , 0.00, 0.70, 1.00, 0.90, 0), PadButton);
0961       ifnew( TPad("PadMultiView" , "PadMultiView" , 0.10, 0.20, 1.00, 0.70, 0), PadMultiView);
0962       ifnew( TPad("PadWarnings" , "PadWarnings" , 0.00, 0.00, 1.00, 0.20, 0), PadWarnings);
0963       PadMultiView->Draw();
0964       PadWarnings->Draw();
0965       PadButton->Draw();
0966       ifnew( TPaveText(0.05, 0.05, 0.95, 0.95) , PaveWarnings );
0967     */
0968 
0969     // TC[3]->SetEditable(0);
0970   }
0971 
0972   else if (name == "BbcMon4")
0973   {
0974     std::cout << "Creating Canvas BbcMon4..." << std::endl;
0975 
0976     TC[3] = new TCanvas("BbcMon4", "MBD status view for Expert", -xsize / 2, 0, xsize / 2, ysize);
0977     gSystem->ProcessEvents();
0978     TC[3]->cd();
0979 
0980     PadTop[3] = new TPad("PadTop0", "PadTop0", 0.00, 0.90, 1.00, 1.00, 0, 0, 0);
0981     PadTdcOver[0] = new TPad("PadTdcOverSouth0", "PadTdcOverSouth0", 0.00, 0.30, 0.50, 0.60, 0, 0, 0);
0982     PadTdcOver[1] = new TPad("PadTdcOverNorth1", "PadTdcOverNorth1", 0.50, 0.60, 1.00, 0.90, 0, 0, 0);
0983     PadnHit[1] = new TPad("PadnHitNorth", "PadnHitNorth", 0.50, 0.03, 1.00, 0.30, 0, 0, 0);
0984     PadnHit[0] = new TPad("PadnHitSouth", "PadnHitSouth", 0.00, 0.03, 0.50, 0.30, 0);
0985     PadnHitStatus = new TPad("PadnHitStatus", "PadnHitStatus", 0.00, 0.00, 1.00, 0.03, 0, 0, 0);
0986 
0987     PadTop[3]->Draw();
0988     for (int side = 0; side < nSIDE; side++)
0989     {
0990       PadTdcOver[side]->Draw();
0991       PadnHit[side]->Draw();
0992       PadnHitStatus->Draw();
0993 
0994       ifnew(TBox(0.5, BbcMonDefs::BBC_nHIT_MB_MIN[side], nPMT_1SIDE_BBC + .5, BbcMonDefs::BBC_nHIT_MB_MAX[side]), BoxnHit[0][side]);
0995       BoxnHit[0][side]->SetFillColor(5);
0996       BoxnHit[0][side]->SetLineColor(3);
0997 
0998       ifnew(TBox(0.5, BbcMonDefs::BBC_nHIT_LASER_MIN[side], nPMT_1SIDE_BBC + .5, BbcMonDefs::BBC_nHIT_LASER_MAX[side]), BoxnHit[1][side]);
0999       BoxnHit[1][side]->SetFillColor(7);
1000       BoxnHit[1][side]->SetLineColor(4);
1001     }
1002 
1003     for (auto &side : BoxTdcOver)
1004     {
1005       ifnew(TBox(0.5, BbcMonDefs::BBC_TDC_OVERFLOW_REGULAR_MIN, nPMT_1SIDE_BBC + .5, BbcMonDefs::BBC_TDC_OVERFLOW_REGULAR_MAX), side);
1006       side->SetFillColor(5);
1007       side->SetLineColor(3);
1008     }
1009     ifnew(TText, TextnHitStatus);
1010     TextnHitStatus->SetTextSize(0.7);
1011     TextnHitStatus->SetText(0.05, 0.5, "Red Square : Collision Event  / Blue Triangle : Laser Event");
1012     transparent[3] = new TPad("transparent3", "this does not show", 0, 0, 1, 1, 0, 0);
1013     transparent[3]->SetFillStyle(4000);
1014     transparent[3]->Draw();
1015   }
1016   else if (name == "BbcVertexSend")
1017   {
1018     TC[4] = new TCanvas(name.c_str(), "Bbc Vertex Sender", -1, 0, 2 * xsize / 3, ysize * 0.9);
1019     gSystem->ProcessEvents();
1020     TC[4]->cd();
1021     // this one is used to plot the run number on the canvas
1022     transparent[4] = new TPad("transparent4", "this does not show", 0, 0, 1, 1);
1023     transparent[4]->SetFillColor(kGray);
1024     transparent[4]->Draw();
1025     TC[4]->SetEditable(false);
1026   }
1027   else if (name == "BbcMonServerStats")
1028   {
1029     TC[5] = new TCanvas(name.c_str(), "BbcMon Server Stats", -1, 0, 2 * xsize / 3, ysize * 0.9);
1030     gSystem->ProcessEvents();
1031     TC[5]->cd();
1032     // this one is used to plot the run number on the canvas
1033     transparent[5] = new TPad("transparent4", "this does not show", 0, 0, 1, 1);
1034     transparent[5]->SetFillColor(kGray);
1035     transparent[5]->Draw();
1036     TC[5]->SetEditable(false);
1037   }
1038 
1039   else if (name == "BbcMon5")
1040   {
1041     std::cout << "Creating Canvas BbcMon5..." << std::endl;
1042 
1043     TC[6] = new TCanvas("BbcMon5", "MBD Z-Vertex View for Shift crew", -1, 0, xsize / 2, ysize);
1044 
1045     // root is pathetic, whenever a new TCanvas is created root piles up
1046     // 6kb worth of X11 events which need to be cleared with
1047     // gSystem->ProcessEvents(), otherwise your process will grow and
1048     // grow and grow but will not show a definitely lost memory leak
1049     gSystem->ProcessEvents();
1050 
1051     TC[6]->cd();
1052     PadTop[4] = new TPad("PadTop0", "PadTop0", 0.00, 0.90, 1.00, 1.00, 0, 0, 0);
1053     PadZVertex = new TPad("PadZVertex", "PadZVertex", 0.00, 0.70, 1.00, 0.90, 0, 0, 0);
1054     PadZVertexSummary = new TPad("PadZVertexSummary", "PadZVertexSummary", 0.00, 0.55, 1.00, 0.70, 0, 0, 0);
1055     PadRunZVertex = new TPad("PadRunZVertex", "PadRunZVertex", 0.00, 0.40, 1.00, 0.55, 0, 0, 0);
1056     PadSouthHitMap = new TPad("PadSouthHitMap", "PadSouthHitMap", 0.00, 0.00, 0.495, 0.40, 0, 0, 0);
1057     PadNorthHitMap = new TPad("PadNorthHitMap", "PadNorthHitMap", 0.505, 0.00, 1.0, 0.40, 0, 0, 0);
1058     // PadTzeroZVertex = new TPad("PadTzeroZVertex", "PadTzeroZVertex", 0.00, 0.00, 1.00, 0.40, 0, 0, 0);
1059 
1060     PadTop[4]->Draw();
1061     // PadZVertex->SetLogy();
1062     PadZVertex->Draw();
1063     PadRunZVertex->Draw();
1064     // PadTzeroZVertex->Draw();
1065     PadSouthHitMap->Draw();
1066     PadNorthHitMap->Draw();
1067     PadZVertexSummary->Draw();
1068 
1069     PadZVertexSummary->cd();
1070     // for (int i = 0; i < 4; i++)
1071     for (int i = 0; i < 1; i++)  //  pp
1072     {
1073       ifnew(TText, TextZVertex[i]);
1074       TextZVertex[i]->SetTextColor(BbcMonDefs::BBC_COLOR_ZVTX[i]);
1075       // TextZVertex[i]->SetTextSize(0.15);
1076       TextZVertex[i]->SetTextSize(0.08);
1077 
1078       ifnew(TText, TextZVertex_scale[i]);
1079       ifnew(TText, TextZVertex_mean[i]);
1080       TextZVertex_scale[i]->SetTextColor(BbcMonDefs::BBC_COLOR_ZVTX[i]);
1081       TextZVertex_mean[i]->SetTextColor(BbcMonDefs::BBC_COLOR_ZVTX[i]);
1082       TextZVertex_scale[i]->SetTextSize(0.08);
1083       TextZVertex_mean[i]->SetTextSize(0.08);
1084     }
1085     /*chiu
1086       TextZVertex[0]->SetText(xpos[0], 0.65, "Zbbc [BBLL1]");       // RUN11 pp
1087     */
1088     TC[6]->cd();
1089     transparent[6] = new TPad("transparent6", "this does not show", 0, 0, 1, 1, 0, 0);
1090     transparent[6]->SetFillStyle(4000);
1091     transparent[6]->Draw();
1092   }
1093   else if (name == "BbcMon6")
1094   {
1095     int canvasindex = 7;
1096     
1097     // xpos negative: do not draw menu bar
1098     TC[canvasindex] = new TCanvas(name.c_str(), "Bbc Packet Information", -1, ysize, xsize , ysize);
1099     TC[canvasindex]->Draw();
1100     gSystem->ProcessEvents();
1101     Pad[2] = new TPad("bbcpad1", "packet event check", 0.0, 0.0, 0.78, 0.95, 0);
1102     Pad[2]->SetLeftMargin(0.07);
1103     Pad[2]->SetRightMargin(0.05);
1104     Pad[2]->Draw();
1105     Pad[3] = new TPad("bbcpad2", "packet event check legend", 0.75, 0.3, 0.95, 0.95, 0);
1106     //Pad[2]->SetLeftMargin(0.05);
1107     Pad[3]->SetRightMargin(0);
1108     Pad[3]->Draw();
1109     //this one is used to plot the run number on the canvas
1110     transparent[canvasindex] = new TPad("transparent1", "this does not show", 0, 0, 1., 1);
1111     transparent[canvasindex]->SetFillStyle(4000);
1112     transparent[canvasindex]->Draw();
1113 
1114     // packet warnings
1115     packetWarning = new TPad("warning1", "packet warnings", 0.75, 0.1, 1, 0.3);
1116     packetWarning->SetRightMargin(0);
1117     packetWarning->SetFillStyle(4000);
1118     packetWarning->Draw();
1119   }
1120   else if (name == "BbcAutoUpdate")
1121   {
1122     
1123     TC[8] = new TCanvas(name.c_str(), "Bbc Autoupdating", -1, 0, 2 * xsize / 3, ysize * 0.9);
1124     gSystem->ProcessEvents();
1125     TC[8]->cd();
1126     // this one is used to plot the run number on the canvas
1127     transparent[8] = new TPad("transparent8", "this does not show", 0, 0, 1, 1);
1128     transparent[8]->SetFillColor(kGray);
1129     transparent[8]->Draw();
1130     TC[8]->SetEditable(false);
1131 
1132     // create 4x2 grid of pads
1133     for (int i = 0; i < nCANVAS; i++)
1134     {
1135       std::string padname = "PadAutoUpdate" + std::to_string(i);
1136       PadAutoUpdate[i] = new TPad(padname.c_str(), padname.c_str(), (i % 4) * 0.25, (i / 4) * 0.5, (i % 4 + 1) * 0.25, (i / 4 + 1) * 0.5, 0, 0, 0);
1137       PadAutoUpdate[i]->SetFillColor(kWhite);
1138       PadAutoUpdate[i]->Draw();
1139     }
1140   }
1141   else
1142   {
1143     std::cout << "Unknown canvas name: " << name << std::endl;
1144     return -1;
1145   }
1146   //
1147 
1148   /*
1149     if ( ( strlen(name) < 1 || strcmp(name, "BBCMon1") == 0 ) ||
1150     ( strlen(name) < 1 || strcmp(name, "BBCMon2") == 0 ) ||
1151     ( strlen(name) < 1 || strcmp(name, "BBCMon3") == 0 ) ||
1152     ( strlen(name) < 1 || strcmp(name, "BBCMon4") == 0 ) )
1153   */
1154   {
1155     ifnew(TPaveText(0.05, 0.65, 0.70, 0.95), PaveTop);
1156     PaveTop->AddText("MBD ONLINE MONITOR");
1157     ifnew(TText, TextTop);
1158     TextTop->SetTextSize(0.25);
1159   }
1160 
1161   std::cout << "Initialize completed" << std::endl;
1162 
1163   return 0;
1164 }
1165 
1166 int BbcMonDraw::Draw(const std::string &what)
1167 {
1168   PRINT_DEBUG("In BbcMonDraw::Draw()");
1169   std::cout << what << std::endl;
1170 
1171   TStyle *oldStyle = gStyle;
1172   bbcStyle->cd();
1173   int canvasindex = -1;
1174   if (!gROOT->FindObject("BbcMon1"))
1175   {
1176     PRINT_DEBUG("In BbcMonDraw::Draw(), BbcMon1");
1177     TC[0] = nullptr;
1178     if (what == "ALL" || what == "FIRST" || what == "BbcMon1" || what == "BbcMonitor")
1179     {
1180       canvasindex = 0;
1181       MakeCanvas("BbcMon1");
1182     }
1183   }
1184   if (!gROOT->FindObject("BbcMon2"))
1185   {
1186     PRINT_DEBUG("In BbcMonDraw::Draw(), BbcMon2");
1187     TC[1] = nullptr;
1188     if (what == "ALL" || what == "SECOND" || what == "BbcMon2" || what == "VertexMonitor")
1189     {
1190       canvasindex = 1;
1191       MakeCanvas("BbcMon2");
1192     }
1193   }
1194   if (!gROOT->FindObject("BbcMon3"))
1195   {
1196     TC[2] = nullptr;
1197     if (what == "ALL" || what == "THIRD" ||  what == "BbcMon3" || what == "TriggerMonitor")
1198     {
1199       canvasindex = 2;
1200       MakeCanvas("BbcMon3");
1201     }
1202   }
1203   // 4th canvas
1204   if (!gROOT->FindObject("BbcMon4"))
1205   {
1206     TC[3] = nullptr;
1207     if (what == "BbcMon4" || what == "VertexMonitor")
1208     {
1209       canvasindex = 3;
1210       MakeCanvas("BbcMon4");
1211     }
1212   }
1213   if (!gROOT->FindObject("BbcMon5"))
1214   {
1215     TC[6] = nullptr;
1216     if (what == "BbcMon5" || what == "VertexMonitor")
1217     {
1218       canvasindex = 6;
1219       MakeCanvas("BbcMon5");
1220     }
1221   }
1222   if (!gROOT->FindObject("BbcMon6"))
1223   {
1224     TC[7] = nullptr;
1225     if (what == "BbcMon6" || what == "ALL")
1226     {
1227       canvasindex = 7;
1228       MakeCanvas("BbcMon6");
1229     }
1230   }
1231   
1232 
1233   //
1234   if ( what == "MBD2MCR" )
1235   {
1236     if (!gROOT->FindObject("BbcVertexSend"))
1237     {
1238       MakeCanvas("BbcVertexSend");
1239     }
1240     TC[4]->Clear("D");
1241     TC[4]->SetEditable(true);
1242     transparent[4]->cd();
1243 
1244     TText PrintRun;
1245     PrintRun.SetTextFont(62);
1246     PrintRun.SetNDC();          // set to normalized coordinates
1247     PrintRun.SetTextAlign(23);  // center/top alignment
1248     PrintRun.SetTextSize(0.04);
1249     PrintRun.SetTextColor(1);
1250 
1251     GetSendFlag();
1252     if ( sendflag==0 )
1253     {
1254       UpdateSendFlag( 1 );
1255       PrintRun.DrawText(0.5, 0.5, "MBD: NOW Sending vertex to MCR");
1256       std::cout << "MBD: NOW Sending vertex to MCR" << std::endl;
1257     }
1258     else if ( sendflag==1 )
1259     {
1260       UpdateSendFlag( 0 );
1261       PrintRun.DrawText(0.5, 0.5, "MBD: STOP sending vertex to MCR");
1262       std::cout << "MBD: STOP sending vertex to MCR" << std::endl;
1263     }
1264     else
1265     {
1266       UpdateSendFlag( 0 );
1267       PrintRun.DrawText(0.5, 0.5, "MBD: something wrong with sendflag, setting to 0");
1268       std::cout << "MBD: something wrong with sendflag, setting to 0" << std::endl;
1269     }
1270     TC[4]->Update();
1271     TC[4]->Show();
1272     TC[4]->SetEditable(false);
1273     return 0;
1274   }
1275 
1276   //
1277   if ( what == "MBDZRESET" )
1278   {
1279     UpdateZResetFlag( 1 );
1280     return 0;
1281   }
1282 
1283   if ( what == "BADGL1" )
1284   {
1285     GetGL1BadFlag();
1286     if ( gl1badflag==0 )
1287     {
1288       UpdateGL1BadFlag( 1 );
1289     }
1290     else if ( gl1badflag==1 )
1291     {
1292       UpdateGL1BadFlag( 0 );
1293     }
1294     else
1295     {
1296       UpdateGL1BadFlag( 0 );
1297     }
1298     return 0;
1299   }
1300 
1301   if ( what == "BbcMonServerStats" )
1302   {
1303     OnlMonClient *cl = OnlMonClient::instance();
1304     if (!gROOT->FindObject("BbcMonServerStats"))
1305     {
1306       MakeCanvas("BbcMonServerStats");
1307     }
1308     TC[5]->Clear("D");
1309     TC[5]->SetEditable(true);
1310     transparent[5]->cd();
1311 
1312     TText PrintRun;
1313     PrintRun.SetTextFont(62);
1314     PrintRun.SetNDC();          // set to normalized coordinates
1315     PrintRun.SetTextAlign(23);  // center/top alignment
1316     PrintRun.SetTextSize(0.04);
1317     PrintRun.SetTextColor(1);
1318     PrintRun.DrawText(0.5, 0.99, "Server Statistics");
1319     PrintRun.SetTextSize(0.02);
1320     double vdist = 0.05;
1321     double vpos = 0.9;
1322     for (const auto &server : m_ServerSet)
1323     {
1324       std::ostringstream txt;
1325       auto servermapiter = cl->GetServerMap(server);
1326       if (servermapiter == cl->GetServerMapEnd())
1327       {
1328         txt << "Server " << server
1329             << " is dead ";
1330         PrintRun.SetTextColor(kRed);
1331       }
1332       else
1333       {
1334         int gl1counts = std::get<4>(servermapiter->second);
1335         txt << "Server " << server
1336             << ", run number " << std::get<1>(servermapiter->second)
1337             << ", event count: " << std::get<2>(servermapiter->second);
1338         if (gl1counts >= 0)
1339     {
1340       txt << ", gl1 count: " << std::get<4>(servermapiter->second);
1341     }
1342         txt << ", current time " << ctime(&(std::get<3>(servermapiter->second)));
1343         if (std::get<0>(servermapiter->second))
1344         {
1345           PrintRun.SetTextColor(kGray + 2);
1346         }
1347         else
1348         {
1349           PrintRun.SetTextColor(kRed);
1350         }
1351       }
1352       PrintRun.DrawText(0.5, vpos, txt.str().c_str());
1353       vpos -= vdist;
1354     }
1355     TC[5]->Update();
1356     TC[5]->Show();
1357     TC[5]->SetEditable(false);
1358     return 0;
1359   }
1360   
1361   if (what == "BbcAutoUpdate" )
1362   {
1363     if (!gROOT->FindObject("BbcAutoUpdate"))
1364     {
1365       canvasindex = 8;
1366       MakeCanvas("BbcAutoUpdate");
1367     }
1368   }
1369 
1370   ClearWarning();
1371 
1372   std::ostringstream otext;
1373   std::string text;
1374   std::string textok;
1375   // std::ostringstream textok;
1376 
1377   // ---------------------------------------------------------------------------------
1378   // Get Histograms from BbcMon server
1379 
1380   OnlMonClient *cl = OnlMonClient::instance();
1381 
1382   PRINT_DEBUG("Start Getting Histogram");
1383 
1384   TH1 *bbc_trigs = cl->getHisto("BBCMON_0", "bbc_trigs");
1385   //ifdelete(Trigs);
1386   if ( bbc_trigs )
1387   {
1388     Trigs = static_cast<TH1 *>(bbc_trigs->Clone());
1389   }
1390 
1391   std::ostringstream name;
1392 
1393   TH1 *bbc_south_nhit = cl->getHisto("BBCMON_0", "bbc_south_nhit");
1394   if (! bbc_south_nhit)
1395   {
1396     if (canvasindex >= 0)
1397     {
1398       DrawDeadServer(transparent[canvasindex]);
1399     }
1400     return -1;
1401   }
1402   ifdelete(South_Nhit);
1403   South_Nhit = static_cast<TH1 *>(bbc_south_nhit->Clone());
1404 
1405   TH1 *bbc_north_nhit = cl->getHisto("BBCMON_0", "bbc_north_nhit");
1406   ifdelete(North_Nhit);
1407   North_Nhit = static_cast<TH1 *>(bbc_north_nhit->Clone());
1408 
1409   for (int iarm=0; iarm<2; iarm++)
1410   {
1411     TString name2 = "bbc_nhit_emcal"; name2 += iarm;
1412     TH1 *bbc_nhit_emcal = cl->getHisto("BBCMON_0", name2.Data());
1413     ifdelete(Nhit_emcal[iarm]);
1414     Nhit_emcal[iarm] = static_cast<TH1 *>(bbc_nhit_emcal->Clone());
1415 
1416     name2 = "bbc_nhit_hcal"; name2 += iarm;
1417     TH1 *bbc_nhit_hcal = cl->getHisto("BBCMON_0", name2.Data());
1418     ifdelete(Nhit_hcal[iarm]);
1419     Nhit_hcal[iarm] = static_cast<TH1 *>(bbc_nhit_hcal->Clone());
1420 
1421     name2 = "bbc_nhit_emcalmbd"; name2 += iarm;
1422     TH1 *bbc_nhit_emcalmbd = cl->getHisto("BBCMON_0", name2.Data());
1423     ifdelete(Nhit_emcalmbd[iarm]);
1424     Nhit_emcalmbd[iarm] = static_cast<TH1 *>(bbc_nhit_emcalmbd->Clone());
1425 
1426     name2 = "bbc_nhit_hcalmbd"; name2 += iarm;
1427     TH1 *bbc_nhit_hcalmbd = cl->getHisto("BBCMON_0", name2.Data());
1428     ifdelete(Nhit_hcalmbd[iarm]);
1429     Nhit_hcalmbd[iarm] = static_cast<TH1 *>(bbc_nhit_hcalmbd->Clone());
1430   }
1431 
1432   bbc_nevent_counter = cl->getHisto("BBCMON_0", "bbc_nevent_counter");
1433 
1434   TH2 *bbc_tdc = static_cast<TH2 *>(cl->getHisto("BBCMON_0", "bbc_tdc"));
1435   //std::cout << "BBCTDC2 " << (uint64_t)bbc_tdc << std::endl;
1436 
1437   /*
1438     name << "bbc_tdc_overflow" ;
1439     bbc_tdc_overflow =  static_cast<TH2 *> (cl->getHisto("BBCMON_0",name.str().c_str()));
1440     name.str("");
1441 
1442     for ( int i = 0 ; i < nPMT_BBC ; i++ )
1443     {
1444     name << "bbc_tdc_overflow_" << setw(3) << setfill('0') << i ;
1445     bbc_tdc_overflow_each[i] = cl->getHisto("BBCMON_0",name.str().c_str());
1446     name.str("");
1447     }
1448   */
1449 
1450   TH2 *bbc_adc = static_cast<TH2 *>(cl->getHisto("BBCMON_0", "bbc_adc"));
1451   ifdelete(Adc);
1452   for (int i = 0; i < nCANVAS; i++)
1453   {
1454     if (TC[i] && i != 4 && i != 5)
1455     {
1456       transparent[i]->Clear();  // clear dead server msg if it was printed before
1457     }
1458   }
1459 
1460   if (bbc_adc)
1461   {
1462     Adc = static_cast<TH2 *>(bbc_adc->Clone());
1463   }
1464   else
1465   {
1466     // here I assume server is dead and there must be a message saying so
1467     std::cout << "SERVER IS DEAD, no bbc_adc" << std::endl;
1468     for (auto &i : TC)
1469     {
1470       if (i)
1471       {
1472         i->cd();
1473         i->Clear("D");
1474         //        DrawDeadServer(transparent[i]);
1475         i->Update();
1476       }
1477     }
1478     return -1;
1479   }
1480 
1481   TH2 *bbc_tdc_armhittime = static_cast<TH2 *>(cl->getHisto("BBCMON_0", "bbc_tdc_armhittime"));
1482   ifdelete(ArmHit);
1483   ArmHit = static_cast<TH2 *>(bbc_tdc_armhittime->Clone());
1484 
1485   TH1 *bbc_zvertex = cl->getHisto("BBCMON_0", "bbc_zvertex");
1486   ifdelete(Zvtx);
1487   Zvtx = static_cast<TH1 *>(bbc_zvertex->Clone());
1488 
1489   TH1 *bbc_zvertex_alltrigger = cl->getHisto("BBCMON_0", "bbc_zvertex_alltrigger");
1490   ifdelete(Zvtx_alltrigger);
1491   Zvtx_alltrigger = static_cast<TH1 *>(bbc_zvertex_alltrigger->Clone());
1492 
1493   TH1 *bbc_zvertex_ns = cl->getHisto("BBCMON_0", "bbc_zvertex_ns");
1494   ifdelete(Zvtx_ns);
1495   Zvtx_ns = static_cast<TH1 *>(bbc_zvertex_ns->Clone());
1496   Zvtx_ns->Rebin(2);
1497 
1498   TH1 *bbc_zvertex_10 = cl->getHisto("BBCMON_0", "bbc_zvertex_10");
1499   ifdelete(Zvtx_10);
1500   Zvtx_10 = static_cast<TH1 *>(bbc_zvertex_10->Clone());
1501 
1502   TH1 *bbc_zvertex_30 = cl->getHisto("BBCMON_0", "bbc_zvertex_30");
1503   ifdelete(Zvtx_30);
1504   Zvtx_30 = static_cast<TH1 *>(bbc_zvertex_30->Clone());
1505 
1506   TH1 *bbc_zvertex_60 = cl->getHisto("BBCMON_0", "bbc_zvertex_60");
1507   ifdelete(Zvtx_60);
1508   Zvtx_60 = static_cast<TH1 *>(bbc_zvertex_60->Clone());
1509 
1510   TH1 *bbc_zvertex_ns_chk = cl->getHisto("BBCMON_0", "bbc_zvertex_ns_chk");
1511   ifdelete(Zvtx_ns_chk);
1512   Zvtx_ns_chk = static_cast<TH1 *>(bbc_zvertex_ns_chk->Clone());
1513 
1514   TH1 *bbc_zvertex_10_chk = cl->getHisto("BBCMON_0", "bbc_zvertex_10_chk");
1515   ifdelete(Zvtx_10_chk);
1516   Zvtx_10_chk = static_cast<TH1 *>(bbc_zvertex_10_chk->Clone());
1517 
1518   TH1 *bbc_zvertex_30_chk = cl->getHisto("BBCMON_0", "bbc_zvertex_30_chk");
1519   ifdelete(Zvtx_30_chk);
1520   Zvtx_30_chk = static_cast<TH1 *>(bbc_zvertex_30_chk->Clone());
1521 
1522   TH1 *bbc_zvertex_60_chk = cl->getHisto("BBCMON_0", "bbc_zvertex_60_chk");
1523   ifdelete(Zvtx_60_chk);
1524   Zvtx_60_chk = static_cast<TH1 *>(bbc_zvertex_60_chk->Clone());
1525 
1526   TH1 *bbc_zvertex_zdcns = cl->getHisto("BBCMON_0", "bbc_zvertex_zdcns");
1527   ifdelete(Zvtx_zdcns);
1528   Zvtx_zdcns = static_cast<TH1 *>(bbc_zvertex_zdcns->Clone());
1529 
1530   TH1 *bbc_zvertex_emcal = cl->getHisto("BBCMON_0", "bbc_zvertex_emcal");
1531   ifdelete(Zvtx_emcal);
1532   Zvtx_emcal = static_cast<TH1 *>(bbc_zvertex_emcal->Clone());
1533 
1534   TH1 *bbc_zvertex_hcal = cl->getHisto("BBCMON_0", "bbc_zvertex_hcal");
1535   ifdelete(Zvtx_hcal);
1536   Zvtx_hcal = static_cast<TH1 *>(bbc_zvertex_hcal->Clone());
1537 
1538   TH1 *bbc_zvertex_emcalmbd = cl->getHisto("BBCMON_0", "bbc_zvertex_emcalmbd");
1539   ifdelete(Zvtx_emcalmbd);
1540   Zvtx_emcalmbd = static_cast<TH1 *>(bbc_zvertex_emcalmbd->Clone());
1541 
1542   TH1 *bbc_zvertex_hcalmbd = cl->getHisto("BBCMON_0", "bbc_zvertex_hcalmbd");
1543   ifdelete(Zvtx_hcalmbd);
1544   Zvtx_hcalmbd = static_cast<TH1 *>(bbc_zvertex_hcalmbd->Clone());
1545 
1546   TH2 *bbc_tzero_zvtx = static_cast<TH2 *>(cl->getHisto("BBCMON_0", "bbc_tzero_zvtx"));
1547   ifdelete(TzeroZvtx);
1548   TzeroZvtx = static_cast<TH2 *>(bbc_tzero_zvtx->Clone());
1549 
1550   TH1 *bbc_avr_hittime = cl->getHisto("BBCMON_0", "bbc_avr_hittime");
1551   ifdelete(AvrHitTime);
1552   AvrHitTime = static_cast<TH1 *>(bbc_avr_hittime->Clone());
1553 
1554   TH1 *bbc_north_hittime = cl->getHisto("BBCMON_0", "bbc_north_hittime");
1555   ifdelete(NorthHitTime);
1556   NorthHitTime = static_cast<TH1 *>(bbc_north_hittime->Clone());
1557 
1558   TH1 *bbc_south_hittime = cl->getHisto("BBCMON_0", "bbc_south_hittime");
1559   ifdelete(SouthHitTime);
1560   SouthHitTime = static_cast<TH1 *>(bbc_south_hittime->Clone());
1561 
1562   TH1 *bbc_south_chargesum = cl->getHisto("BBCMON_0", "bbc_south_chargesum");
1563   ifdelete(SouthChargeSum);
1564   SouthChargeSum = static_cast<TH1 *>(bbc_south_chargesum->Clone());
1565 
1566   TH1 *bbc_north_chargesum = cl->getHisto("BBCMON_0", "bbc_north_chargesum");
1567   ifdelete(NorthChargeSum);
1568   NorthChargeSum = static_cast<TH1 *>(bbc_north_chargesum->Clone());
1569 
1570   TH1 *bbc_prescale_hist = cl->getHisto("BBCMON_0", "bbc_prescale_hist");
1571   ifdelete(Prescale_hist);
1572   Prescale_hist = static_cast<TH1 *>(bbc_prescale_hist->Clone());
1573 
1574   TH1 *bbc_runvtx = cl->getHisto("BBCMON_0", "bbc_runvtx");
1575   ifdelete(RunVtx);
1576   RunVtx = static_cast<TH1 *>(bbc_runvtx->Clone());
1577 
1578   TH1 *bbc_runvtxerr = cl->getHisto("BBCMON_0", "bbc_runvtxerr");
1579   ifdelete(RunVtxErr);
1580   RunVtxErr = static_cast<TH1 *>(bbc_runvtxerr->Clone());
1581 
1582   TH1 *bbc_runvtxtime = cl->getHisto("BBCMON_0", "bbc_runvtxtime");
1583   ifdelete(RunVtxTime);
1584   RunVtxTime = static_cast<TH1 *>(bbc_runvtxtime->Clone());
1585 
1586   TH2 *bbc_time_wave = static_cast<TH2 *>(cl->getHisto("BBCMON_0", "bbc_time_wave"));
1587   ifdelete(TimeWave);
1588   TimeWave = static_cast<TH2 *>(bbc_time_wave->Clone());
1589 
1590   TH2 *bbc_charge_wave = static_cast<TH2 *>(cl->getHisto("BBCMON_0", "bbc_charge_wave"));
1591   ifdelete(ChargeWave);
1592   ChargeWave = static_cast<TH2 *>(bbc_charge_wave->Clone());
1593 
1594   TH2 *bbc_south_hitmap = static_cast<TH2 *>(cl->getHisto("BBCMON_0", "bbc_south_hitmap"));
1595   ifdelete(SouthHitMap);
1596   SouthHitMap = static_cast<TH2 *>(bbc_south_hitmap->Clone());
1597 
1598   TH2 *bbc_north_hitmap = static_cast<TH2 *>(cl->getHisto("BBCMON_0", "bbc_north_hitmap"));
1599   ifdelete(NorthHitMap);
1600   NorthHitMap = static_cast<TH2 *>(bbc_north_hitmap->Clone());
1601 
1602 
1603   TH1 *bbc_zvertex_auto[TriggerEnum::NUM_MBD_TRIGGERS] = {nullptr};
1604   for (int i = 0; i < static_cast<int>(TriggerEnum::NUM_MBD_TRIGGERS); i++)
1605   {
1606     // std::string name = Form("bbc_zvertex_autoupdate_%i", i);
1607     bbc_zvertex_auto[i] = static_cast<TH1 *>(cl->getHisto("BBCMON_0", Form("bbc_zvertex_autoupdate_%i", i)));
1608     ifdelete(ZvrtxAuto[i]);
1609     ZvrtxAuto[i] = static_cast<TH1F *>(bbc_zvertex_auto[i]->Clone());
1610   }
1611   PRINT_DEBUG("Start Creating graphs");
1612 
1613   // Create HitTime projection ------------------------------------------
1614 
1615   for (int side = 0; side < nSIDE; side++)
1616   {
1617     ifdelete(HitTime[side]);
1618 
1619     name << "FitHitTime" << BbcMonDefs::SIDE_Str[side];
1620     if ( bbc_tdc!=nullptr )
1621     {
1622       HitTime[side] = bbc_tdc->ProjectionY(name.str().c_str(), side * 64, side * 64 + 63);
1623       name.str("");
1624     }
1625 
1626     name << BbcMonDefs::SIDE_Str[side] << " MBD TDC Distribution";
1627     // name << BbcMonDefs::SIDE_Str[side] << " MBD TDC" << tdc << " Distribution(Trigger:BBLL1)" ; // Run14 AuAu 15GeV 2014.02.23
1628     HitTime[side]->SetTitle(name.str().c_str());
1629     ifnew(TF1(name.str().c_str(), "gaus"), FitHitTime[side]);
1630     name.str("");
1631     FitHitTime[side]->SetLineWidth(1);  // 0.05 was used fro run3
1632     FitHitTime[side]->SetLineColor(4);
1633   }
1634 
1635   // Create ZVertex Fit
1636 
1637   ifnew(TF1("FitZvtx", "gaus"), FitZvtx);
1638   ifnew(TF1("FitAvrHitTime", "gaus"), FitAvrHitTime);
1639   ifnew(TF1("FitNorthHitTime", "gaus"), FitNorthHitTime);
1640   ifnew(TF1("FitSouthHitTime", "gaus"), FitSouthHitTime);
1641   FitZvtx->SetLineWidth(3);
1642   FitZvtx->SetLineColor(1);
1643 
1644   // ------------------------------------------------------------------------------
1645   // making nHit TGraph
1646 
1647   double pmt[nPMT_1SIDE_BBC] = {0.};
1648   double zero[nPMT_1SIDE_BBC] = {0.};
1649   double nhitPmt[nTRIGGER][nSIDE][nPMT_1SIDE_BBC];
1650   double nhit_total = bbc_nevent_counter->GetBinContent(2);
1651   double nhit[nTRIGGER];
1652   nhit[0] = bbc_nevent_counter->GetBinContent(2);
1653   nhit[1] = bbc_nevent_counter->GetBinContent(3);
1654 
1655   for (int i = 0; i < nPMT_1SIDE_BBC; i++)
1656   {
1657     pmt[i] = i + 1;
1658     // zero[i] = 0;
1659   }
1660 
1661   PRINT_DEBUG("Creating nHit Graph");
1662 
1663   for (int side = 0; side < nSIDE; side++)
1664   {
1665     for (int trig = 0; trig < nTRIGGER; trig++)
1666     {
1667       for (int i = 0; i < nPMT_1SIDE_BBC; i++)
1668       {
1669         if (nhit[trig] != 0)
1670         {
1671           // RUN12: to ignore laser rate for ch8, fiber is broken.
1672           if (side == 0 && trig == 1 && i == 7)
1673           {
1674             nhitPmt[trig][side][i] = 999;
1675           }
1676           else
1677           {
1678             // nhitPmt[trig][side][i] = bbc_nhit[trig]->GetBinContent(i + side * nPMT_1SIDE_BBC + 1) / nhit[trig];
1679             nhitPmt[trig][side][i] = 999;
1680           }
1681         }
1682         else
1683         {
1684           nhitPmt[trig][side][i] = 0;
1685         }
1686       }
1687       ifnew(TGraph(nPMT_1SIDE_BBC, pmt, nhitPmt[trig][side]), nHit[trig][side]);
1688       // nHit[trig][side]->GetXaxis()->SetTitleSize(  0.05);
1689       // nHit[trig][side]->GetYaxis()->SetTitleSize(  0.05);
1690       // nHit[trig][side]->GetXaxis()->SetTitleOffset(0.70);
1691       // nHit[trig][side]->GetYaxis()->SetTitleOffset(1.75);
1692     }
1693   }
1694   PRINT_DEBUG("Creating OverFlow Grapth");
1695 
1696   // Create TDC Overflow Graph
1697 
1698   double tdcOverMean[nSIDE][nPMT_1SIDE_BBC];
1699   double tdcOverErrY[nSIDE][nPMT_1SIDE_BBC];
1700   for (int iside = 0; iside < nSIDE; iside++)
1701   {
1702     for (int i = 0; i < nPMT_1SIDE_BBC; i++)
1703     {
1704       tdcOverMean[iside][i] = 0.0;
1705       tdcOverErrY[iside][i] = 0.0;
1706     }
1707   }
1708 
1709   for (int side = 0; side < nSIDE; side++)
1710   {
1711     for (int i = 0; i < nPMT_1SIDE_BBC; i++)
1712     {
1713       /*
1714     tdcOverMean[0][side][i] = bbc_tdc_overflow_each[0][i + side * nPMT_1SIDE_BBC]->GetMean() /
1715         bbccalib->getOverflow0()->getCalibPar(i)->getDeviation();
1716     tdcOverErrY[0][side][i] = bbc_tdc_overflow_each[0][i + side * nPMT_1SIDE_BBC]->GetRMS() /
1717         bbccalib->getOverflow0()->getCalibPar(i)->getDeviation();
1718 
1719     tdcOverMean[1][side][i] = bbc_tdc_overflow_each[1][i + side * nPMT_1SIDE_BBC]->GetMean() /
1720         bbccalib->getOverflow1()->getCalibPar(i)->getDeviation();
1721     tdcOverErrY[1][side][i] = bbc_tdc_overflow_each[1][i + side * nPMT_1SIDE_BBC]->GetRMS() /
1722         bbccalib->getOverflow1()->getCalibPar(i)->getDeviation();
1723       */
1724 
1725       ifnew(TGraphErrors(nPMT_1SIDE_BBC, pmt, tdcOverMean[side], zero,
1726                          tdcOverErrY[side]),
1727             TdcOver[side]);
1728     }
1729   }
1730 
1731   // Redraw each Pad
1732 
1733   // ------------------------------------------------------------------------
1734   // Draw 1st Page
1735   // ------------------------------------------------------------------------
1736   std::ostringstream msg;
1737   PRINT_DEBUG("Drawing Graphs on Canvas");
1738 
1739   // Make TopPave
1740   std::pair<time_t,int> evttime = cl->EventTime("CURRENT");
1741 
1742   otext.str("");
1743   otext << "Run #" << cl->RunNumber();
1744   otext << " Events: " << nhit_total;
1745   otext << " Date:" << ctime(&evttime.first);
1746   text = otext.str();
1747   //ifnew(TText, TextTop);
1748   TextTop->SetTextColor(evttime.second);
1749   TextTop->SetText(0.01, 0.25, text.c_str());
1750 
1751   if (TC[0])
1752   {
1753     TC[0]->cd();
1754 
1755     PadTop[0]->cd();
1756     PaveTop->Draw();
1757     TextTop->Draw();
1758 
1759     PadZVertexSummary->cd();
1760 
1761     Zvtx->SetLineColor(4);
1762     Zvtx->SetFillColor(7);
1763 
1764     Zvtx_ns->SetTitle("MBD Wide Zvertex");
1765     Zvtx_ns->SetLineColor(4);
1766     Zvtx_ns->SetFillColor(7);
1767     Zvtx_ns->SetMinimum(0); // start plots at zero
1768 
1769     double nevt = Trigs->GetBinContent(11); // trig 10 is MBDNS>=1, +1 for bin
1770     double prescale = Prescale_hist->GetBinContent(11);
1771     std::cout << "TRIG 10 " << nevt << "\t" << prescale << std::endl;
1772     /*
1773       if ( prescale!= -1.0 )
1774       {
1775       Zvtx_ns->Scale( prescale+1 );
1776       }
1777     */
1778 
1779     Zvtx_10->SetLineColor(2);
1780     Zvtx_10->SetFillColor(2);
1781     nevt = Trigs->GetBinContent(13); // trig 10 is MBDNS, |vtx|<10
1782     prescale = Prescale_hist->GetBinContent(13);
1783     std::cout << "TRIG 12 " << nevt << "\t" << prescale << std::endl;
1784     if ( prescale!= -1.0 )
1785     {
1786       Zvtx_10->Scale( prescale+1 );
1787     }
1788 
1789     // Get Maximum at the inside of BBC which is 130cm from center;
1790     float maxEntries = 10;
1791 
1792     for (int i = 0; i < Zvtx->GetNbinsX(); i++)
1793     {
1794       if (fabs(Zvtx->GetBinCenter(i)) < 130)
1795       {
1796         if (maxEntries < Zvtx->GetBinContent(i + 1))
1797         {
1798           maxEntries = Zvtx->GetBinContent(i + 1);
1799         }
1800       }
1801     }
1802 
1803     // Fit No-Vertex Distribution
1804     FitZvtx->SetRange(-75, 75);
1805     FitZvtx->SetParameters(100, 0, 16);
1806     FitZvtx->SetLineColor(1);
1807 
1808     // Zvtx->Fit("FitZvtx", "LRQ");
1809     if ( Zvtx_ns->GetEntries() > 0 )
1810     {
1811       Zvtx_ns->Fit("FitZvtx", "LRNQ");
1812     }
1813 
1814     // here we get the relative scaling right to put all on the same plot
1815     // the binning might be different, so we first find the bins corresponding to
1816     // +- 20cm and then get the Integral corrected for the binwidth
1817     // this is then used to get the histograms on the same scale
1818     /*
1819       int lowbin = Zvtx_zdc->GetXaxis()->FindBin(-20.);
1820       int hibin = Zvtx_zdc->GetXaxis()->FindBin(20.);
1821       double zdc_count = Zvtx_zdc->Integral(lowbin, hibin, "width");
1822     */
1823 
1824     /*
1825       int lowbin = Zvtx->GetXaxis()->FindBin(-20.);
1826       int hibin = Zvtx->GetXaxis()->FindBin(20.);
1827       double bbc_count = Zvtx->Integral(lowbin, hibin, "width");
1828     */
1829 
1830     // std::cout << "the ratio of integral (-30cm < ZVertex < 30cm) between BBLL1 without vtx cut and ZDC : " << bbc_count_novtx / zdc_count << std::endl ;
1831     // std::cout << "the ratio of integral (-30cm < ZVertex < 30cm) between BBLL1 without vtx cut  and BBLL1 with BBCZ < |30cm|  : " << bbc_count_novtx/bbc_count << std::endl ;
1832 
1833     // Get the Trigger Name that is used
1834     GetMinBiasTrigName();
1835 
1836     // Draw ZVertex triggerd variable trigger
1837     Zvtx->SetMaximum(maxEntries * 1.05);
1838     TString title = "MBD ZVertex (TRIG = "; title += TrigName; title += ")";
1839     Zvtx->SetTitle( title );
1840     // PadZVertex->DrawFrame(-160,0,160,maxEntries*1.05,"Bbc ZVertex (south<-->north)");
1841     // std::cout << "maxEntries " << maxEntries << std::endl;
1842     // Zvtx->Draw("hist");
1843     // Zvtx->Scale(bbc_count_novtx/bbc_count);
1844 
1845     // just in case the bbcll1 with vtx cut histo is empty
1846     // draw the novertex cut (happens during setup)
1847     if (Zvtx_ns->GetEntries() > 0)
1848     {
1849       Zvtx_ns->Draw("hist");
1850       //Zvtx->Draw("histsame");
1851       FitZvtx->Draw("same");
1852     }
1853 
1854     // trigger rate between BBCLL1 and Zvertex within +- BBC_ZVERTEX_CUT_FOR_TRIG_RATE
1855     // bbll1, zdc, bbll1_novtx
1856 
1857     float trig_rate[3], trig_rate_err[3];
1858     double nevent[3];
1859     memset(trig_rate, 0, sizeof(trig_rate));
1860     memset(trig_rate_err, 0, sizeof(trig_rate_err));
1861     memset(nevent, 0, sizeof(nevent));
1862 
1863     /*
1864       CalculateTriggerRates(trig_rate[0], trig_rate_err[0], nevent[0],
1865       trig_rate[1], trig_rate_err[1], nevent[1],
1866       trig_rate[2], trig_rate_err[2], nevent[2]);
1867     */
1868 
1869     /* chiu
1870        float sigma_zdc = 0.0;
1871        // float sigma_zdc_err = 0.0;
1872        float effic_bbc = 0.0;
1873        // float effic_bbc_err = 0.0;
1874        float beamInZdc = 0.0;
1875        // float beamInZdc_err = 0.0;
1876        float beamInBbc = 0.0;
1877        // float beamInBbc_err = 0.0;
1878        */
1879 
1880     /*
1881       BeamMonitoring( sigma_zdc, sigma_zdc_err, effic_bbc, effic_bbc_err,
1882       beamInZdc, beamInZdc_err, beamInBbc, beamInBbc_err);
1883     */
1884 
1885     /*chiu
1886       std::ostringstream trig_rate_text;
1887       trig_rate_text << " #sigma_{ZDC} = " << std::fixed << std::setprecision(3) << sigma_zdc
1888       << " #epsilon_{BBC} = " << effic_bbc
1889       << " {}^{beam in acceptance}_{       ZDC      } = " << beamInZdc
1890       << " {}^{beam in acceptance}_{       BBC      } = " << beamInBbc;
1891       TextBbcSummaryTrigRate->SetText(0.02, 0.05, trig_rate_text.str().c_str());
1892       trig_rate_text.str("");
1893     */
1894 
1895     // float xpos[3] = {0.1, 0.34, 0.58};
1896     //     float xpos[3] = {0.30, 0.50, 0.75};
1897     // TextZVertexNotice->Draw();
1898 
1899     TH1 *Zvtx_array[4];  // with narrow
1900     // TH1 *Zvtx_array[3];
1901     Zvtx_array[0] = Zvtx;
1902     // Zvtx_array[1] = Zvtx;
1903     // Zvtx_array[2] = Zvtx;
1904     //  Zvtx_array[1] = Zvtx_zdc;
1905 
1906     // Show status of ZVertex
1907     int i = 0;
1908 
1909     // TextZVertex[i]->Draw();
1910 
1911     // scale factor ---------------------------------------------------------------------
1912     //  std::cout << "  " << i << " " << Prescale_hist->GetBinContent(i + 1) << std::endl;
1913     otext.str("");
1914     // otext << "( "<< Prescale_hist->GetBinContent(i+1)<<" )";
1915     // otext << "( "<< Prescale_hist->GetBinContent(i+1)<<" )"<<" ";
1916     // otext << nevent[i] ;
1917     // otext.precision(8);
1918     otext << " ( " << Prescale_hist->GetBinContent(i + 1) << " ) "
1919           << " ";
1920     // otext << nevent[i]/Prescale_hist->GetBinContent(i+1) ;
1921     otext << Zvtx_array[i]->GetEntries();
1922 
1923     text = otext.str();
1924     TextZVertex_scale[i]->SetText(xpos[i], 0.50, text.c_str());
1925     // TextZVertex_scale[i]->Draw();
1926 
1927     // mean and RMS ---------------------------------------------------------------------
1928     otext.str("");
1929     otext << ((float) int(Zvtx_array[i]->GetMean() * 10)) / 10.0 << "cm ( "
1930           << ((float) int(Zvtx_array[i]->GetRMS() * 10)) / 10.0 << " cm) ";
1931     text = otext.str();
1932 
1933     TextZVertex_mean[i]->SetText(xpos[i], 0.25, text.c_str());
1934     // TextZVertex_mean[i]->Draw();
1935     //  otext.precision(6);
1936 
1937     // TextZVertex[i]->Draw();
1938 
1939     /*
1940       TextZVertex_scale[i]->SetText(0.00, 0.50, "(Scale Fac.) #Evt.");
1941       TextZVertex_scale[i]->Draw();
1942       // TextZVertex_mean[i]->SetText(0.05, 0.25, "Vertex Mean (RMS)");
1943       TextZVertex_mean[i]->SetText(0.00, 0.25, "Vertex Mean (RMS)");
1944       TextZVertex_mean[i]->Draw();
1945     */
1946 
1947     // Draw Status
1948     otext.str("");
1949     otext << "Z_{MBDNS}^{Fit}= " << ((float) int(FitZvtx->GetParameter(1) * 10)) / 10.0 
1950           << " #pm " << ((float)int(FitZvtx->GetParError(1)*10))/10.0
1951           << " cm";
1952 
1953     text = otext.str();
1954     // TextZvtxStatus[0]->SetText(0.0, 0.85, text.c_str());
1955     Double_t ymax = Zvtx_ns->GetMaximum();
1956     TextZvtxStatus[0]->SetText(-230., ymax * 0.8, text.c_str());
1957     TextZvtxStatus[0]->SetTextSize(0.10);
1958     TextZvtxStatus[0]->Draw();
1959 
1960     otext.str("");
1961     otext << "#sigma = " << int(FitZvtx->GetParameter(2))
1962           << " #pm " << ((float)int(FitZvtx->GetParError(2)*10))/10.0
1963           << " cm";
1964     text = otext.str();
1965     TextZvtxStatus[1]->SetText(100., ymax * 0.8, text.c_str());
1966     TextZvtxStatus[1]->SetTextSize(0.10);
1967     TextZvtxStatus[1]->Draw();
1968 
1969     // chiu TextBbcSummaryTrigRate->Draw();
1970 
1971     PadZVertex->cd();
1972 
1973     TLine zpline;
1974     TLine zmline;
1975     float cutz = bbc_nevent_counter->GetBinContent(7) + 1.0;
1976     //std::cout << "CUTZ " << cutz << std::endl;
1977 
1978     if (Zvtx->GetEntries() > 0)
1979     {
1980       Zvtx->SetLineColor(2);
1981       Zvtx->SetFillColor(2);
1982       Zvtx->Draw("hist");
1983       Zvtx->GetXaxis()->SetRangeUser(-60, 60);
1984       Zvtx->Draw("hist");
1985       //Zvtx_10->Draw("histsame");
1986       PadZVertex->Update();
1987 
1988       //std::cout << "CUTZ " << cutz << " " << gPad->GetFrame()->GetY1() << " " << gPad->GetFrame()->GetY2() << std::endl;
1989 
1990       if (cutz>1. && cutz<60.)
1991       {
1992         zpline.SetLineStyle(7);
1993         zpline.SetLineColor(kBlack);
1994         zpline.SetLineWidth(2);
1995         zpline.DrawLine(cutz,gPad->GetFrame()->GetY1(),cutz,gPad->GetFrame()->GetY2());
1996         zmline.SetLineStyle(7);
1997         zmline.SetLineColor(kBlack);
1998         zmline.SetLineWidth(2);
1999         zmline.DrawLine(-cutz,gPad->GetFrame()->GetY1(),-cutz,gPad->GetFrame()->GetY2());
2000       }
2001     }
2002 
2003     // Status of sending vertex
2004     GetSendFlag();
2005     if ( sendflag==1 )
2006     {
2007       text = "Sending Vertex to MCR";
2008     }
2009     else
2010     {
2011       text = "NOT Sending Vertex to MCR";
2012     }
2013 
2014     //TextZvtxStatus[2]->SetText(-55., Zvtx_ns->GetMaximum()*0.95, text.c_str());
2015     TextZvtxStatus[2]->SetTextSize(0.05);
2016     TextZvtxStatus[2]->DrawLatexNDC(0.15,0.8,text.c_str());
2017 
2018     if ( bbc_nevent_counter->GetBinContent(8)>0 )
2019     {
2020       text = "GL1 mis-aligned";
2021       TextZvtxStatus[3]->SetTextSize(0.05);
2022       TextZvtxStatus[3]->DrawLatexNDC(0.15,0.7,text.c_str());
2023     }
2024     /*
2025     // replaced with hitmap
2026     PadTzeroZVertex->cd();
2027     TzeroZvtx->Draw("colz");
2028 
2029     // insert line
2030     LineTzeroZvtx[0]->Draw();
2031     LineTzeroZvtx[1]->Draw();
2032     LineTzeroZvtx[2]->Draw();
2033     LineTzeroZvtx[3]->Draw();
2034 
2035     // insert text
2036     TextTzeroZvtx->SetText(10, 4, "Good region");
2037     TextTzeroZvtx->Draw();
2038     */
2039 
2040     PadRunZVertex->cd();
2041 
2042     TLine aline;
2043     gRunVtx->Set(0);
2044     gRunAvgVtx->Set(0);
2045 
2046     int n = RunVtx->GetEntries();
2047     //std::cout << "XXXX " << n << std::endl;
2048     if ( n>0 )
2049     {
2050       for (int ibin=1; ibin<=n; ibin++)
2051       {
2052     Double_t zvtxmean = RunVtx->GetBinContent(ibin);
2053     Double_t zvtxmeanerr = RunVtxErr->GetBinContent(ibin);
2054     Double_t zvtxtime = RunVtxTime->GetBinContent(ibin);
2055     // drop outliers
2056     if ( zvtxmeanerr>5 || zvtxmeanerr<1e-2 )
2057     {
2058       continue;
2059     }
2060     int npts = gRunVtx->GetN();
2061     gRunVtx->SetPoint(npts,zvtxtime,zvtxmean);
2062     gRunVtx->SetPointError(npts,0,zvtxmeanerr);
2063       }
2064 
2065       int nvtx = gRunVtx->GetN();
2066       Double_t *xtime = gRunVtx->GetX();
2067       Double_t *yzvtx = gRunVtx->GetY();
2068 
2069       // calculate moving average (navg data points)
2070       for (int ix=0; ix<nvtx; ix++)
2071       {
2072     int imin = ix-4;
2073     if (imin<0)
2074     {
2075       imin=0;
2076     }
2077     int imax = ix+4;
2078     if (imax>nvtx-1)
2079     {
2080       imax=nvtx-1;
2081     }
2082     double nsum = 0.;
2083     double sum = 0.;
2084     for (int isum=imin; isum<=imax; isum++)
2085     {
2086       sum += yzvtx[isum];
2087       nsum += 1.0;
2088     }
2089 
2090     double runavg = sum/nsum;
2091 
2092     gRunAvgVtx->SetPoint(ix,xtime[ix],runavg);
2093       }
2094 
2095       gRunVtx->SetMarkerStyle(20);
2096       gRunVtx->SetMarkerColor(4);
2097       gRunVtx->SetLineColor(4);
2098 
2099       //std::cout << "DRAWING GRUNVTX" << std::endl;
2100       if ( nvtx>0 )
2101       {
2102     gRunVtx->Draw("ap");
2103     gRunAvgVtx->Draw("c");
2104         PadRunZVertex->SetGridy();
2105     PadRunZVertex->Update();
2106     aline.SetLineStyle(7);
2107     aline.SetLineColor(kRed);
2108     aline.SetLineWidth(2);
2109     aline.DrawLine(gPad->GetFrame()->GetX1(),0,gPad->GetFrame()->GetX2(),0);
2110       }
2111       else
2112       {
2113     // Draw an empty histogram
2114     EmptyHist->Draw();
2115       }
2116     }
2117     else
2118     {
2119       // Draw an empty histogram
2120       EmptyHist->Draw();
2121     }
2122 
2123     //NorthHitMap->Draw("colz");
2124 
2125     double nevents = bbc_nevent_counter->GetBinContent(2);
2126     PadSouthHitMap->cd();
2127     SouthHitMap->Scale(1.0 / nevents);
2128     SouthHitMap->Draw("colz");
2129 
2130     PadNorthHitMap->cd();
2131     NorthHitMap->Scale(1.0 / nevents);
2132     NorthHitMap->Draw("colz");
2133   }
2134   //copy for all triggers plot
2135   if (TC[6])
2136   {
2137     TC[6]->cd();
2138 
2139     PadTop[4]->cd();
2140     PaveTop->Draw();
2141     TextTop->Draw();
2142 
2143     PadZVertexSummary->cd();
2144 
2145     Zvtx_alltrigger->SetLineColor(4);
2146     Zvtx_alltrigger->SetFillColor(7);
2147 
2148     Zvtx_alltrigger->SetTitle("MBD zvertex");
2149     Zvtx_alltrigger->SetLineColor(4);
2150     Zvtx_alltrigger->SetFillColor(7);
2151     Zvtx_alltrigger->SetMinimum(0); // start plots at zero
2152 
2153 
2154     // Get Maximum at the inside of BBC which is 130cm from center;
2155     float maxEntries = 10;
2156 
2157     for (int i = 0; i < Zvtx_alltrigger->GetNbinsX(); i++)
2158     {
2159       if (fabs(Zvtx_alltrigger->GetBinCenter(i)) < 130)
2160       {
2161         if (maxEntries < Zvtx_alltrigger->GetBinContent(i + 1))
2162         {
2163           maxEntries = Zvtx_alltrigger->GetBinContent(i + 1);
2164         }
2165       }
2166     }
2167     
2168     // Fit No-Vertex Distribution
2169     FitZvtx->SetRange(-75, 75);
2170     FitZvtx->SetParameters(100, 0, 15);
2171     FitZvtx->SetLineColor(1);
2172     // Zvtx->Fit("FitZvtx", "LRQ");
2173     Zvtx_alltrigger->Fit("FitZvtx", "LRQ");
2174 
2175     // here we get the relative scaling right to put all on the same plot
2176     // the binning might be different, so we first find the bins corresponding to
2177     // +- 20cm and then get the Integral corrected for the binwidth
2178     // this is then used to get the histograms on the same scale
2179 
2180     Zvtx_alltrigger->SetMaximum(maxEntries * 1.05);
2181     Zvtx_alltrigger->SetTitle("MBD ZVertex (south<-->north)");
2182 
2183     if (Zvtx_alltrigger->GetEntries() > 0)
2184     {
2185       Zvtx_alltrigger->Draw("hist");
2186       FitZvtx->Draw("same");
2187     }
2188     // trigger rate between BBCLL1 and Zvertex within +- BBC_ZVERTEX_CUT_FOR_TRIG_RATE
2189     // bbll1, zdc, bbll1_novtx
2190 
2191     float trig_rate[3], trig_rate_err[3];
2192     double nevent[3];
2193     memset(trig_rate, 0, sizeof(trig_rate));
2194     memset(trig_rate_err, 0, sizeof(trig_rate_err));
2195     memset(nevent, 0, sizeof(nevent));
2196 
2197     TH1 *Zvtx_array[4];  // with narrow
2198     // TH1 *Zvtx_array[3];
2199     Zvtx_array[0] = Zvtx_alltrigger;
2200 
2201     int i = 0;
2202 
2203   
2204     otext.str("");
2205    
2206     otext << " ( " << Prescale_hist->GetBinContent(i + 1) << " ) "
2207           << " ";
2208     // otext << nevent[i]/Prescale_hist->GetBinContent(i+1) ;
2209     otext << Zvtx_array[i]->GetEntries();
2210 
2211     text = otext.str();
2212     TextZVertex_scale[i]->SetText(xpos[i], 0.50, text.c_str());
2213     // TextZVertex_scale[i]->Draw();
2214 
2215     // mean and RMS ---------------------------------------------------------------------
2216     otext.str("");
2217     otext << ((float) int(Zvtx_array[i]->GetMean() * 10)) / 10.0 << "cm ( "
2218           << ((float) int(Zvtx_array[i]->GetRMS() * 10)) / 10.0 << " cm) ";
2219     text = otext.str();
2220 
2221     TextZVertex_mean[i]->SetText(xpos[i], 0.25, text.c_str());
2222    
2223     // Draw Status
2224     otext.str("");
2225     otext << "Z_{MBDNS}^{Fit}= " << ((float) int(FitZvtx->GetParameter(1) * 10)) / 10.0 
2226           << " #pm " << ((float)int(FitZvtx->GetParError(1)*10))/10.0
2227           << " cm";
2228 
2229     text = otext.str();
2230     // TextZvtxStatus[0]->SetText(0.0, 0.85, text.c_str());
2231     //TextZvtxStatus[0]->SetText(-230., maxEntries * 0.8, text.c_str());
2232     TextZvtxStatus[0]->SetTextSize(0.10);
2233     TextZvtxStatus[0]->DrawLatexNDC(0.3,0.75,text.c_str());
2234 
2235     otext.str("");
2236     otext << "#sigma = " << int(FitZvtx->GetParameter(2))
2237           << " #pm " << ((float)int(FitZvtx->GetParError(2)*10))/10.0
2238           << " cm";
2239     text = otext.str();
2240     //TextZvtxStatus[1]->SetText(100., maxEntries * 0.8, text.c_str());
2241     TextZvtxStatus[1]->SetTextSize(0.10);
2242     TextZvtxStatus[1]->DrawLatexNDC(0.7,0.75,text.c_str());
2243 
2244     // chiu TextBbcSummaryTrigRate->Draw();
2245 
2246     PadZVertex->cd();
2247 
2248     if (Zvtx_alltrigger->GetEntries() > 0)
2249     {
2250       Zvtx_alltrigger->GetXaxis()->SetRangeUser(-60, 60);
2251       Zvtx_alltrigger->Draw("hist");
2252       //      Zvtx_10->Draw("histsame");
2253     }
2254 
2255     // Status of sending vertex
2256     GetSendFlag();
2257     if ( sendflag==1 )
2258     {
2259       text = "Sending Vertex to MCR";
2260     }
2261     else
2262     {
2263       text = "NOT Sending Vertex to MCR";
2264     }
2265 
2266     //TextZvtxStatus[2]->SetText(-55., Zvtx_alltrigger->GetMaximum()*0.95, text.c_str());
2267     TextZvtxStatus[2]->SetTextSize(0.05);
2268     TextZvtxStatus[2]->DrawLatexNDC(0.15,0.8,text.c_str());
2269 
2270     // double nevents = bbc_nevent_counter->GetBinContent(2);
2271     // PadSouthHitMap->cd();
2272     // SouthHitMap->Scale(1.0 / nevents);
2273     // SouthHitMap->Draw("colz");
2274 
2275     // PadNorthHitMap->cd();
2276     // NorthHitMap->Scale(1.0 / nevents);
2277     // NorthHitMap->Draw("colz");
2278     TC[6]->Update();
2279   }
2280 
2281   if (TC[7])
2282   {
2283     TC[7]->cd();
2284     TC[7]->Clear("D");
2285     TC[7]->SetEditable(true);
2286 
2287     TH1 *packetStatusFull[nPacketStatus] = {nullptr};
2288     int nServer = 0;
2289     int colorsThatDontSuck[] = {kGreen+2,1,2,4, kViolet,kCyan,kOrange+2,kMagenta+2,kAzure-2};
2290     int isAlert = false;
2291     for (auto server = ServerBegin(); server != ServerEnd(); ++server)
2292     {   
2293       TH1 *packetStatus[nPacketStatus] = {nullptr};
2294       for(int i = 0; i < nPacketStatus; i++)
2295       {
2296         packetStatus[i] = cl->getHisto(*server, Form("h1_packet_status_%d",i));
2297       }
2298       
2299       if(!packetStatus[0])continue;
2300       for(int i = 0; i < nPacketStatus; i++)
2301       {
2302         if(((packetStatus[i]->Integral()) && (i != 0)) || (isAlert == true))isAlert = true;
2303         packetStatus[i]->SetFillColor(colorsThatDontSuck[i]);
2304         packetStatus[i]->SetLineColor(kBlack);
2305         packetStatus[i]->SetLineWidth(1); // Optional: make lines thicker if needed
2306       }
2307 
2308       //Normalize
2309       const int nBins = packetStatus[0]->GetNbinsX();
2310       float norm[nBins] = {0};
2311       for(int i = 0; i < nBins; i++)
2312       {
2313         for(int j = 0; j < nPacketStatus; j++)
2314         {
2315           norm[i]+=packetStatus[j]->GetBinContent(i+1);
2316         }
2317       }
2318       for(int i = 0; i < nPacketStatus; i++)
2319       {
2320         for(int j = 0; j < nBins; j++)
2321         {
2322           if(norm[j]>0)packetStatus[i]->SetBinContent(j+1, packetStatus[i]->GetBinContent(j+1)/norm[j]);
2323           else packetStatus[i]->SetBinContent(j+1,0); 
2324         }
2325         nServer > 0 ? (TH1*)(packetStatusFull[i]->Add(packetStatus[i])) : packetStatusFull[i] = packetStatus[i];
2326       }
2327       nServer++;
2328     }
2329 
2330     THStack *hs = new THStack("hs", "Event-Averaged Packet Status");
2331 
2332     
2333     TLegend *leg = new TLegend(0.05,0.1,0.95,1);
2334     leg->SetFillStyle(0);
2335     leg->SetTextSize(0.06);
2336     //leg->SetBorderSize(0);
2337     std::string stati[nPacketStatus] = {"Good", "Malformed Packet Header", "Unknown Word Classifier", "Too Many FEMs in Packet", "Malformed FEM Header", "Wrong Number of FEMs"};
2338 
2339     if(packetStatusFull[0])
2340     {
2341       for(int i = 0; i < nPacketStatus; i++)
2342       {
2343         hs->Add(packetStatusFull[i]);
2344         leg->AddEntry(packetStatusFull[i],stati[i].c_str(),"f");
2345       } 
2346     }
2347     else{
2348       DrawDeadServer(transparent[7]);
2349       return -1;
2350     }
2351     std::cout << "THStack prep" << std::endl;
2352 
2353     Pad[2]->cd(); 
2354     if(hs)
2355     {
2356       hs->Draw("BAR");
2357       hs->GetXaxis()->SetNdivisions(3);
2358       hs->GetXaxis()->SetTitle("Packet Number");
2359       hs->GetYaxis()->SetTitle("Event Fraction");
2360       hs->GetYaxis()->SetTitleOffset(0.9);
2361     }
2362     Pad[3]->cd();
2363     leg->Draw();
2364     TC[7]->Update();
2365     TC[7]->Show();
2366     packetWarning->cd();
2367     TText warn;
2368     warn.SetTextFont(62);
2369     if(isAlert)
2370     {
2371       warn.SetTextSize(.15);
2372       warn.SetTextColor(kRed);
2373       warn.DrawText(0.05,0.5,"!WARNING!");
2374       warn.DrawText(0.05,0.35,"POSSIBLE DATA CORRUPTION");
2375       warn.DrawText(0.05,0.2,"CONTACT DAQ EXPERT");
2376     }
2377     else
2378     {
2379       warn.SetTextSize(0.3);
2380       warn.DrawText(0.1,0.5,"All good!");
2381     }
2382     
2383     TC[7]->SetEditable(false);
2384 
2385   }
2386   //  bbc_t0_pave->Draw("same");
2387 
2388   // ------------------------------------------------------------------------
2389   // Draw 2nd Page
2390   // ------------------------------------------------------------------------
2391   if (TC[1])
2392   {
2393     TC[1]->cd();
2394 
2395     PadTop[1]->cd();
2396     PaveTop->Draw();
2397     TextTop->Draw();
2398 
2399     if (PadAvrHitTime)
2400     {
2401       PadAvrHitTime->cd();
2402       AvrHitTime->Draw();
2403 
2404       if ( AvrHitTime->GetEntries() > 0 )
2405       {
2406     float rangemin;
2407     float rangemax;
2408     int npeak = tspec->Search(AvrHitTime, 5, "goff",0.2);  // finds the highest peak, draws marker
2409     if (npeak < 3 )                                     // no center peak
2410     {
2411       AvrHitTime->Fit("FitAvrHitTime", "QN0L");
2412       rangemin = FitAvrHitTime->GetParameter(1) - FitAvrHitTime->GetParameter(2);
2413       rangemax = FitAvrHitTime->GetParameter(1) + FitAvrHitTime->GetParameter(2);
2414     }
2415     else
2416     {
2417       double *peakpos = tspec->GetPositionX();
2418       float centerpeak = peakpos[0];
2419       float sidepeak[2];
2420       if (peakpos[2] > peakpos[1])
2421       {
2422         sidepeak[0] = peakpos[1];
2423         sidepeak[1] = peakpos[2];
2424       }
2425       else
2426       {
2427         sidepeak[1] = peakpos[1];
2428         sidepeak[0] = peakpos[2];
2429       }
2430       rangemin = centerpeak - (centerpeak - sidepeak[0]) / 2.;
2431       rangemax = centerpeak + (sidepeak[1] - centerpeak) / 2.;
2432     }
2433 
2434     FitAvrHitTime->SetRange(rangemin, rangemax);
2435     AvrHitTime->Fit("FitAvrHitTime", "QRL");
2436     FitAvrHitTime->Draw("same");
2437 
2438     float height = AvrHitTime->GetMaximum();
2439     FitAvrHitTime->Draw("same");
2440 
2441     LineAvrHitTime[1]->SetY2(height);
2442     LineAvrHitTime[0]->SetY2(height);
2443     ArrowAvrHitTime->SetY1(height * 0.90);
2444     ArrowAvrHitTime->SetY2(height * 0.90);
2445     TextAvrHitTime->SetY(height * 0.88);
2446     LineAvrHitTime[0]->Draw();
2447     LineAvrHitTime[1]->Draw();
2448     ArrowAvrHitTime->Draw();
2449     TextAvrHitTime->Draw();
2450       }
2451     }
2452 
2453     if (PadTimeWave)
2454     {
2455       PadTimeWave->cd();
2456       TimeWave->GetXaxis()->SetRangeUser(-0.5,15.5);
2457       TimeWave->Draw("colz");
2458       PadTimeWave->Update();
2459       TLine aline;
2460       aline.SetLineColor(kRed);
2461       aline.SetLineWidth(4);
2462       aline.DrawLine( 6.5,gPad->GetFrame()->GetY1(), 6.5,gPad->GetFrame()->GetY2());
2463       aline.DrawLine(10.5,gPad->GetFrame()->GetY1(),10.5,gPad->GetFrame()->GetY2());
2464     }
2465 
2466     if (PadSouthHitTime)
2467     {
2468       PadSouthHitTime->cd();
2469       SouthHitTime->Draw();
2470 
2471       if ( SouthHitTime->GetEntries() > 0 )
2472       {
2473     float rangemin;
2474     float rangemax;
2475     int npeak = tspec->Search(SouthHitTime, 5, "goff",0.2);  // finds the highest peak, draws marker
2476 
2477     //std::cout << "NPEAKS " << npeak << std::endl;
2478     if (npeak < 3)                                       
2479     {
2480       SouthHitTime->Fit("FitSouthHitTime", "QN0L");
2481       rangemin = FitSouthHitTime->GetParameter(1) - 1.0*FitSouthHitTime->GetParameter(2);
2482       rangemax = FitSouthHitTime->GetParameter(1) + 1.0*FitSouthHitTime->GetParameter(2);
2483     }
2484     else
2485     {
2486       double *peakpos = tspec->GetPositionX();
2487       float centerpeak = peakpos[0];
2488       float sidepeak[2];
2489       if (peakpos[2] > peakpos[1])
2490       {
2491         sidepeak[0] = peakpos[1];
2492         sidepeak[1] = peakpos[2];
2493       }
2494       else
2495       {
2496         sidepeak[1] = peakpos[1];
2497         sidepeak[0] = peakpos[2];
2498       }
2499       rangemin = centerpeak - (centerpeak - sidepeak[0]) / 2.;
2500       rangemax = centerpeak + (sidepeak[1] - centerpeak) / 2.;
2501     }
2502 
2503     rangemin = -3;
2504     rangemax = 3;
2505     FitSouthHitTime->SetRange(rangemin, rangemax);
2506     SouthHitTime->Fit("FitSouthHitTime", "QRL");
2507     FitSouthHitTime->Draw("same");
2508 
2509     PadSouthHitTime->Update();
2510     TLine aline;
2511     aline.SetLineStyle(7);
2512     aline.SetLineColor(kRed);
2513     aline.SetLineWidth(4);
2514     aline.DrawLine(-3.0, gPad->GetFrame()->GetY1(), -3.0, gPad->GetFrame()->GetY2());
2515     aline.DrawLine(+3.0, gPad->GetFrame()->GetY1(), +3.0, gPad->GetFrame()->GetY2());
2516 
2517     /*
2518     // Lines to indicate good mean
2519     float height = SouthHitTime->GetMaximum();
2520     LineSouthHitTime[1]->SetY2(height);
2521     LineSouthHitTime[0]->SetY2(height);
2522     ArrowSouthHitTime->SetY1(height * 0.90);
2523     ArrowSouthHitTime->SetY2(height * 0.90);
2524     TextSouthHitTime->SetY(height * 0.88);
2525     LineSouthHitTime[0]->Draw();
2526     LineSouthHitTime[1]->Draw();
2527     ArrowSouthHitTime->Draw();
2528     TextSouthHitTime->Draw();
2529     */
2530 
2531       }
2532     }
2533 
2534     if (PadNorthHitTime)
2535     {
2536       PadNorthHitTime->cd();
2537       NorthHitTime->Draw();
2538 
2539       if ( NorthHitTime->GetEntries() > 0 )
2540       {
2541     float rangemin;
2542     float rangemax;
2543     int npeak = tspec->Search(NorthHitTime, 5, "goff",0.2);  // finds the highest peak, draws marker
2544     if (npeak < 3)                                       // no center peak
2545     {
2546       NorthHitTime->Fit("FitNorthHitTime", "QN0L");
2547       rangemin = FitNorthHitTime->GetParameter(1) - 1.0*FitNorthHitTime->GetParameter(2);
2548       rangemax = FitNorthHitTime->GetParameter(1) + 1.0*FitNorthHitTime->GetParameter(2);
2549     }
2550     else
2551     {
2552       double *peakpos = tspec->GetPositionX();
2553       float centerpeak = peakpos[0];
2554       float sidepeak[2];
2555       if (peakpos[2] > peakpos[1])
2556       {
2557         sidepeak[0] = peakpos[1];
2558         sidepeak[1] = peakpos[2];
2559       }
2560       else
2561       {
2562         sidepeak[1] = peakpos[1];
2563         sidepeak[0] = peakpos[2];
2564       }
2565       rangemin = centerpeak - (centerpeak - sidepeak[0]) / 2.;
2566       rangemax = centerpeak + (sidepeak[1] - centerpeak) / 2.;
2567     }
2568 
2569     rangemin = -3;
2570     rangemax = 3;
2571     FitNorthHitTime->SetRange(rangemin, rangemax);
2572     NorthHitTime->Fit("FitNorthHitTime", "QRL");
2573     FitNorthHitTime->Draw("same");
2574 
2575     PadNorthHitTime->Update();
2576     TLine aline;
2577     aline.SetLineStyle(7);
2578     aline.SetLineColor(kRed);
2579     aline.SetLineWidth(4);
2580     aline.DrawLine(-3.0 ,gPad->GetFrame()->GetY1(),-3.0,gPad->GetFrame()->GetY2());
2581     aline.DrawLine(+3.0,gPad->GetFrame()->GetY1(),+3.0,gPad->GetFrame()->GetY2());
2582 
2583     /*
2584     // Lines to indicate good mean
2585     float height = NorthHitTime->GetMaximum();
2586     LineNorthHitTime[1]->SetY2(height);
2587     LineNorthHitTime[0]->SetY2(height);
2588     ArrowNorthHitTime->SetY1(height * 0.90);
2589     ArrowNorthHitTime->SetY2(height * 0.90);
2590     TextNorthHitTime->SetY(height * 0.88);
2591     LineNorthHitTime[0]->Draw();
2592     LineNorthHitTime[1]->Draw();
2593     ArrowNorthHitTime->Draw();
2594     TextNorthHitTime->Draw();
2595     */
2596       }
2597     }
2598 
2599     /*
2600       PadArmHit->cd();
2601       if (ArmHit)
2602       {
2603       ArmHit->Draw("colz");
2604       ArcArmHit->Draw();
2605       TextArmHit->Draw();
2606       }
2607     */
2608 
2609     if (PadBbcSummary)
2610     {
2611       PadBbcSummary->cd();
2612       otext.str("");
2613       otext << "South:" << ((float) int(FitSouthHitTime->GetParameter(1) * 10)) / 10 << "[ns]  ";
2614       otext << "North:" << ((float) int(FitNorthHitTime->GetParameter(1) * 10)) / 10 << "[ns]  ";
2615       otext << "...  ";
2616       if (BbcMonDefs::BBC_MIN_REGULAR_TDC0_MEAN < FitNorthHitTime->GetParameter(1) &&
2617           BbcMonDefs::BBC_MAX_REGULAR_TDC0_MEAN > FitNorthHitTime->GetParameter(1) &&
2618           BbcMonDefs::BBC_MIN_REGULAR_TDC0_MEAN < FitSouthHitTime->GetParameter(1) &&
2619           BbcMonDefs::BBC_MAX_REGULAR_TDC0_MEAN > FitSouthHitTime->GetParameter(1))
2620       {
2621         // otext << "OK";
2622         textok = "                                                         OK";
2623       }
2624       else
2625       {
2626         textok = " ";
2627         if (BbcMonDefs::BBC_MIN_WORNING_STATISTICS_FOR_ZVERTEX_MEAN > Zvtx->GetEntries())
2628         {
2629           otext << "Too low statistics";
2630         }
2631         else
2632         {
2633           otext << "Change Global-Offset on V124";
2634         }
2635       }
2636       text = otext.str();
2637       TextBbcSummaryHitTime[0]->SetText(0.01, 0.75, text.c_str());
2638       TextBbcSummaryHitTime[0]->Draw();
2639       text = textok;
2640       // TextBbcSummaryHitTime[1]->SetText(0.65, 0.75, text.c_str() );
2641       TextBbcSummaryHitTime[1]->SetText(0.01, 0.75, text.c_str());
2642       TextBbcSummaryHitTime[1]->SetTextColor(3);
2643       TextBbcSummaryHitTime[1]->Draw();
2644     }
2645 
2646     // Global offset
2647     float delay = (BbcMonDefs::BBC_DEFAULT_OFFSET -
2648                    ((FitNorthHitTime->GetParameter(1) + FitSouthHitTime->GetParameter(1)) * 0.5));  //[ns]
2649     otext.str("");
2650     otext << "   Global offset : ";
2651     if (int(-2 * delay) == 0)
2652     {
2653       // otext << "need not to move ... OK";
2654       // otext << " ... OK";
2655       // textok = "                                    ... OK";
2656     }
2657     else
2658     {
2659       // textok = " ";
2660       if (!(BbcMonDefs::BBC_MIN_REGULAR_TDC0_MEAN < FitNorthHitTime->GetParameter(1) &&
2661             BbcMonDefs::BBC_MAX_REGULAR_TDC0_MEAN > FitNorthHitTime->GetParameter(1) &&
2662             BbcMonDefs::BBC_MIN_REGULAR_TDC0_MEAN < FitSouthHitTime->GetParameter(1) &&
2663             BbcMonDefs::BBC_MAX_REGULAR_TDC0_MEAN > FitSouthHitTime->GetParameter(1)))
2664       {
2665         // otext << "Need to move " << int( -2*delay) << "count";
2666         otext << "Need to call BBC expert: " << int(-2 * delay) << "count shifted";
2667       }
2668       else
2669       {
2670         // otext << " ... OK";
2671         // textok = "                            OK";
2672       }
2673     }
2674 
2675     /*chiu
2676     // otext << "...     )" ;
2677     text = otext.str();
2678     TextBbcSummaryGlobalOffset[0]->SetText(0.01, 0.50, text.c_str());
2679     TextBbcSummaryGlobalOffset[0]->Draw();
2680     text = textok;
2681     // TextBbcSummaryGlobalOffset[1]->SetText(0.35, 0.50, text.c_str() );
2682     TextBbcSummaryGlobalOffset[1]->SetText(0.01, 0.50, text.c_str());
2683     TextBbcSummaryGlobalOffset[1]->SetTextColor(3);
2684     TextBbcSummaryGlobalOffset[1]->Draw();
2685     // textok = " ";
2686     */
2687 
2688     // ZVertex
2689     /*
2690       otext.str("");
2691       otext << "Mean ZVertex:" << ((float)int(FitZvtx->GetParameter(1)*10)) / 10 << "[cm] ";
2692       if(Zvtx->GetEntries() > BbcMonDefs::BBC_MIN_WORNING_STATISTICS_FOR_ZVERTEX_MEAN ) {
2693       otext << "( sigma " << ((float)int(FitZvtx->GetParameter(2)*10)) / 10 << "cm)";
2694       }else{
2695       // otext << "( RMS " << ((float)int(Zvtx->GetRMS()*10)) / 10 << "cm)";
2696       }
2697       otext << " ... ";
2698       if ( BbcMonDefs::BBC_MIN_REGULAR_ZVERTEX_MEAN < FitZvtx->GetParameter(1) &&
2699       BbcMonDefs::BBC_MAX_REGULAR_ZVERTEX_MEAN > FitZvtx->GetParameter(1) )
2700       otext << "OK";
2701       else {
2702       if( BbcMonDefs::BBC_MIN_WORNING_STATISTICS_FOR_ZVERTEX_MEAN > Zvtx->GetEntries() )
2703       otext << "Too low statistics";
2704       else
2705       otext << "Ask SL to contact MCR";
2706       }
2707       text = otext.str();
2708       TextBbcSummaryZvertex->SetText(0.01, 0.25, text.c_str() );
2709     */
2710 
2711     /*chiu
2712       TextBbcSummaryZvertex->SetText(0.01, 0.25, "Shown data are triggered by BBLL1 |z|<130cm");
2713       TextBbcSummaryZvertex->Draw();
2714     */
2715   }
2716 
2717   // ------------------------------------------------------------------------
2718   // Draw 3rd Page
2719   // ------------------------------------------------------------------------
2720   bbcStyle->cd();
2721   if (TC[2])
2722   {
2723     PadTop[2]->cd();
2724     PaveTop->Draw();
2725     TextTop->Draw();
2726 
2727     /*
2728       for (int side = 0; side < nSIDE; side++)
2729       {
2730       PadHitTime[side]->cd();
2731 
2732       HitTime[side]->Draw();
2733       float rangemin;
2734       float rangemax;
2735       int npeak = tspec->Search(HitTime[side], 2, "goff");  // finds the highest peak, draws marker
2736       if (npeak < 3)                                        // no center peak
2737       {
2738       FitHitTime[side]->SetRange(BbcMonDefs::TDC_FIT_MIN, BbcMonDefs::TDC_FIT_MAX);
2739       HitTime[side]->Fit(FitHitTime[side]->GetName(), "QRNL");
2740       rangemax = std::min(BbcMonDefs::TDC_FIT_MAX,
2741       FitHitTime[side]->GetParameter(1) + FitHitTime[side]->GetParameter(2));
2742       rangemin = std::max(BbcMonDefs::TDC_FIT_MIN,
2743       FitHitTime[side]->GetParameter(1) - FitHitTime[side]->GetParameter(2));
2744       }
2745       else
2746       {
2747       double *peakpos = tspec->GetPositionX();
2748       float centerpeak = peakpos[0];
2749       float sidepeak[2];
2750       if (peakpos[2] > peakpos[1])
2751       {
2752       sidepeak[0] = peakpos[1];
2753       sidepeak[1] = peakpos[2];
2754       }
2755       else
2756       {
2757       sidepeak[1] = peakpos[1];
2758       sidepeak[0] = peakpos[2];
2759       }
2760       rangemin = centerpeak - (centerpeak - sidepeak[0]) / 2.;
2761       rangemax = centerpeak + (sidepeak[1] - centerpeak) / 2.;
2762       }
2763 
2764       FitHitTime[side]->SetRange(rangemin, rangemax);
2765 
2766       HitTime[side]->Fit(FitHitTime[side]->GetName(), "QRL");
2767       float height = HitTime[side]->GetMaximum();
2768       FitHitTime[side]->Draw("same");
2769 
2770       LineHitTime[side][1]->SetY2(height);
2771       LineHitTime[side][0]->SetY2(height);
2772       ArrowHitTime[side]->SetY1(height * 0.90);
2773       ArrowHitTime[side]->SetY2(height * 0.90);
2774       TextHitTime[side]->SetY(height * 0.88);
2775       LineHitTime[side][0]->Draw();
2776       LineHitTime[side][1]->Draw();
2777       ArrowHitTime[side]->Draw();
2778       TextHitTime[side]->Draw();
2779       }
2780       // PadWarnings->cd();
2781       // PaveWarnings->Draw();
2782       */
2783 
2784     if (PadZvtx)
2785     {
2786       PadZvtx->cd();
2787 
2788 
2789       if (Zvtx_ns_chk->GetEntries() > 0)
2790       {
2791         Zvtx_ns_chk->SetMinimum(0); // start plots at zero
2792 
2793         /*
2794       double prescale = Prescale_hist->GetBinContent(11);
2795       Zvtx_10_chk->Scale( prescale+1.0 );
2796       Zvtx_30_chk->Scale( prescale+1.0 );
2797       Zvtx_60_chk->Scale( prescale+1.0 );
2798         */
2799 
2800         std::vector<double> max;
2801         max.push_back( Zvtx_ns_chk->GetBinContent( Zvtx_ns_chk->GetMaximumBin() ) );
2802         max.push_back( Zvtx_60_chk->GetBinContent( Zvtx_60_chk->GetMaximumBin() ) );
2803         max.push_back( Zvtx_30_chk->GetBinContent( Zvtx_30_chk->GetMaximumBin() ) );
2804         max.push_back( Zvtx_10_chk->GetBinContent( Zvtx_10_chk->GetMaximumBin() ) );
2805         max.push_back( Zvtx_zdcns->GetBinContent( Zvtx_zdcns->GetMaximumBin() ) );
2806         double maximum = *std::max_element(max.begin(), max.end());
2807 
2808         //Zvtx_ns_chk->GetXaxis()->SetRangeUser(-60, 60);
2809         Zvtx_ns_chk->SetMaximum(maximum*1.1);
2810         Zvtx_ns_chk->Draw("hist");
2811 
2812         Zvtx_60_chk->SetLineColor(40);
2813         Zvtx_60_chk->SetFillColor(6);
2814 
2815         Zvtx_30_chk->SetLineColor(30);
2816         Zvtx_30_chk->SetFillColor(3);
2817 
2818         Zvtx_10_chk->SetLineColor(46);
2819         Zvtx_10_chk->SetFillColor(2);
2820 
2821         Zvtx_zdcns->Draw("histsame");
2822         Zvtx_60_chk->Draw("histsame");
2823         Zvtx_30_chk->Draw("histsame");
2824         Zvtx_10_chk->Draw("histsame");
2825         //std::cout << "aaa " << Zvtx_ns->GetEntries() << " " << Zvtx_10->GetEntries() << " " << Zvtx_30->GetEntries() << " " << Zvtx_60->GetEntries() << " " << Zvtx_zdcns->GetEntries() << " " << std::endl;
2826       }
2827 
2828       /*
2829     Zvtx->Draw();
2830     Zvtx->Fit("FitZvtx", "QN0L");
2831     FitZvtx->SetRange(FitZvtx->GetParameter(1) - FitZvtx->GetParameter(2) * 2,
2832     FitZvtx->GetParameter(1) + FitZvtx->GetParameter(2) * 2);
2833     Zvtx->Fit("FitZvtx", "QRL");
2834     FitZvtx->Draw("same");
2835 
2836     float height = Zvtx->GetMaximum();
2837     FitZvtx->Draw("same");
2838 
2839     LineZvtx[1]->SetY2(height);
2840     LineZvtx[0]->SetY2(height);
2841     ArrowZvtx->SetY1(height * 0.90);
2842     ArrowZvtx->SetY2(height * 0.90);
2843     TextZvtx->SetY(height * 0.88);
2844     LineZvtx[0]->Draw();
2845     LineZvtx[1]->Draw();
2846     ArrowZvtx->Draw();
2847     TextZvtx->Draw();
2848     TextZvtxNorth->Draw();
2849     TextZvtxSouth->Draw();
2850       */
2851     }
2852 
2853     if (PadNhits)
2854     {
2855       PadNhits->cd();
2856 
2857       std::vector<double> max;
2858       max.push_back( South_Nhit->GetBinContent( South_Nhit->GetMaximumBin() ) );
2859       max.push_back( North_Nhit->GetBinContent( North_Nhit->GetMaximumBin() ) );
2860       double maximum = *std::max_element(max.begin(), max.end());
2861 
2862       South_Nhit->SetLineColor(2);
2863       North_Nhit->SetLineColor(4);
2864       South_Nhit->GetXaxis()->SetRangeUser(0,64);
2865       North_Nhit->GetXaxis()->SetRangeUser(0,64);
2866       South_Nhit->SetTitle("MBD Nhits, MBD trig");
2867       North_Nhit->SetTitle("MBD Nhits, MBD trig");
2868       South_Nhit->SetMaximum( maximum*1.1 );
2869       South_Nhit->Draw();
2870       North_Nhit->Draw("same");
2871     }
2872 
2873     if (PadZvtxEMCAL)
2874     {
2875       PadZvtxEMCAL->cd();
2876 
2877       Zvtx_emcal->GetXaxis()->SetRangeUser(-60, 60);
2878       Zvtx_emcalmbd->GetXaxis()->SetRangeUser(-60, 60);
2879       if ( Zvtx_emcal->GetEntries() > Zvtx_emcalmbd->GetEntries() )
2880       {
2881         Zvtx_emcal->Draw();
2882         Zvtx_emcalmbd->Draw("same");
2883       }
2884       else
2885       {
2886         Zvtx_emcalmbd->Draw();
2887         Zvtx_emcal->Draw("same");
2888       }
2889     }
2890     if (PadNhitsEMCAL)
2891     {
2892       PadNhitsEMCAL->cd();
2893 
2894       Nhit_emcal[0]->SetLineColor(2);
2895       Nhit_emcal[1]->SetLineColor(4);
2896       Nhit_emcalmbd[0]->SetLineColor(2);
2897       Nhit_emcalmbd[1]->SetLineColor(4);
2898       Nhit_emcalmbd[0]->SetLineStyle(7);
2899       Nhit_emcalmbd[1]->SetLineStyle(7);
2900       for (int iarm=0; iarm<2; iarm++)
2901       {
2902         Nhit_emcal[iarm]->GetXaxis()->SetRangeUser(0,64);
2903         Nhit_emcalmbd[iarm]->GetXaxis()->SetRangeUser(0,64);
2904 
2905         if ( iarm==0 ) Nhit_emcal[iarm]->Draw();
2906         else           Nhit_emcal[iarm]->Draw("same");
2907         Nhit_emcalmbd[iarm]->Draw("same");
2908       }
2909     }
2910 
2911     if (PadZvtxHCAL)
2912     {
2913       PadZvtxHCAL->cd();
2914       Zvtx_hcal->GetXaxis()->SetRangeUser(-60, 60);
2915       Zvtx_hcalmbd->GetXaxis()->SetRangeUser(-60, 60);
2916 
2917       if ( Zvtx_hcal->GetEntries() > Zvtx_hcalmbd->GetEntries() )
2918       {
2919         Zvtx_hcal->Draw();
2920         Zvtx_hcalmbd->Draw("same");
2921       }
2922       else
2923       {
2924         Zvtx_hcalmbd->Draw();
2925         Zvtx_hcal->Draw("same");
2926       }
2927     }
2928     if (PadNhitsHCAL)
2929     {
2930       PadNhitsHCAL->cd();
2931 
2932       Nhit_hcal[0]->SetLineColor(2);
2933       Nhit_hcal[1]->SetLineColor(4);
2934       Nhit_hcalmbd[0]->SetLineColor(2);
2935       Nhit_hcalmbd[1]->SetLineColor(4);
2936       Nhit_hcalmbd[0]->SetLineStyle(7);
2937       Nhit_hcalmbd[1]->SetLineStyle(7);
2938       for (int iarm=0; iarm<2; iarm++)
2939       {
2940         Nhit_hcal[iarm]->GetXaxis()->SetRangeUser(0,64);
2941         Nhit_hcalmbd[iarm]->GetXaxis()->SetRangeUser(0,64);
2942 
2943         if ( iarm==0 ) Nhit_hcal[iarm]->Draw();
2944         else           Nhit_hcal[iarm]->Draw("same");
2945         Nhit_hcalmbd[iarm]->Draw("same");
2946       }
2947     }
2948 
2949 
2950     if (PadChargeSum)
2951     {
2952       PadChargeSum->cd();
2953 
2954       NorthChargeSum->SetLineColor(4);
2955       SouthChargeSum->SetLineColor(2);
2956       NorthChargeSum->GetXaxis()->SetRangeUser(0,60);
2957       SouthChargeSum->GetXaxis()->SetRangeUser(0,60);
2958       NorthChargeSum->Draw();
2959       SouthChargeSum->Draw("same");
2960 
2961       //       float height = NorthChargeSum->GetMaximum();
2962       //       TextNorthChargeSum->SetY( height*0.88);
2963       //       TextSouthChargeSum->SetY( height*0.88);
2964       TextNorthChargeSum->SetTextColor(4);
2965       TextSouthChargeSum->SetTextColor(2);
2966       TextNorthChargeSum->Draw();
2967       TextSouthChargeSum->Draw();
2968     }
2969 
2970     if (PadAdc)
2971     {
2972       PadAdc->cd();
2973       Adc->Draw("colz");
2974     }
2975   }  // TC[2]
2976 
2977   // ------------------------------------------------------------------------
2978   // Draw 4th Page
2979   // ------------------------------------------------------------------------
2980   if (TC[3])
2981   {
2982     TC[3]->cd();
2983 
2984     // -------------------------------------------------------------------------
2985     PadTop[3]->cd();
2986     PaveTop->Draw();
2987     TextTop->Draw();
2988 
2989     // TDC
2990 
2991     for (int side = 0; side < nSIDE; side++)
2992     {
2993       PadTdcOver[side]->cd();
2994       // ifdelete( FrameTdcOver[side] );
2995       FrameTdcOver[side] = TC[0]->DrawFrame(0.5, -5, 64.5, 5);
2996       // FrameTdcOver[side] = gPad->DrawFrame( 0.5, -5, 64.5, 5);
2997 
2998       std::cout << "FrameTdcOver[" << side << "] = " << (unsigned long) FrameTdcOver[side] << std::endl;
2999       BoxTdcOver[side]->Draw();
3000 
3001       name << BbcMonDefs::SIDE_Str[side] << " MBD TDC Distribution";
3002       FrameTdcOver[side]->SetTitle(name.str().c_str());
3003       name.str("");
3004 
3005       FrameTdcOver[side]->GetXaxis()->SetTitle("PMT Number");
3006       FrameTdcOver[side]->GetXaxis()->SetTitleSize(0.05);
3007       FrameTdcOver[side]->GetXaxis()->SetLabelSize(0.05);
3008       FrameTdcOver[side]->GetYaxis()->SetTitle("Deviation [#sigma]");
3009       FrameTdcOver[side]->GetYaxis()->SetTitleSize(0.07);
3010       FrameTdcOver[side]->GetYaxis()->SetTitleOffset(0.50);
3011       FrameTdcOver[side]->GetYaxis()->SetLabelSize(0.05);
3012       TdcOver[side]->SetMarkerStyle(21);
3013       TdcOver[side]->SetMarkerSize(0.5);
3014       TdcOver[side]->SetMarkerColor(2);
3015       TdcOver[side]->Draw("P");
3016 
3017       // Check Warning
3018       if (nhit[0] > 100)
3019       {
3020         for (int i = 0; i < nPMT_1SIDE_BBC; i++)
3021         {
3022           if (tdcOverMean[side][i] == 0 && tdcOverErrY[side][i] == 0)
3023           {
3024             msg.str("");
3025             msg << "Stop the run (ch " << i + 1 << " is out of the range)";
3026             std::string wmsg = msg.str();
3027             Warning(PadTdcOver[side], i, tdcOverMean[side][i], 1, wmsg);
3028             wmsg.erase();
3029             msg.str("");
3030           }
3031           if (tdcOverMean[side][i] < BbcMonDefs::BBC_TDC_OVERFLOW_REGULAR_MIN)
3032           {
3033             msg.str("");
3034             msg << "ch " << i + 1 << " is too low ( " << std::fixed << std::setprecision(1) << tdcOverMean[side][i] << " #sigma)";
3035             std::string wmsg = msg.str();
3036             Warning(PadTdcOver[side], i, tdcOverMean[side][i], 1, wmsg);
3037             wmsg.erase();
3038             msg.str("");
3039           }
3040           if (tdcOverMean[side][i] > BbcMonDefs::BBC_TDC_OVERFLOW_REGULAR_MAX)
3041           {
3042             msg.str("");
3043             msg << "ch " << i + 1 << " is too high ( " << std::fixed << std::setprecision(1) << tdcOverMean[side][i] << " #sigma)";
3044             std::string wmsg = msg.str();
3045             Warning(PadTdcOver[side], i, tdcOverMean[side][i], 1, wmsg);
3046             wmsg.erase();
3047             msg.str("");
3048           }
3049           if (tdcOverMean[side][i] > BbcMonDefs::BBC_TDC_OVERFLOW_REGULAR_RMS_MAX)
3050           {
3051             msg.str("");
3052             msg << "ch " << i + 1 << " is too wide ( " << std::fixed << std::setprecision(1) << tdcOverErrY[side][i] << " #sigma)";
3053             std::string wmsg = msg.str();
3054             Warning(PadTdcOver[side], i, tdcOverMean[side][i], 1, wmsg);
3055             wmsg.erase();
3056             msg.str("");
3057           }
3058         }
3059       }
3060 
3061       PadnHit[side]->cd();
3062       // ifdelete( FramenHit[side] );
3063       // FramenHit[side] = TC[0]->DrawFrame( 0.5, 0, 64.5, 1);
3064       FramenHit[side] = gPad->DrawFrame(0.5, 0, 64.5, 1);
3065       BoxnHit[0][side]->Draw();
3066       BoxnHit[1][side]->Draw();
3067 
3068       name << BbcMonDefs::SIDE_Str[side] << " MBD number of Hit per Event";
3069       FramenHit[side]->SetTitle(name.str().c_str());
3070       name.str("");
3071 
3072       FramenHit[side]->GetXaxis()->SetTitle("PMT Number");
3073       FramenHit[side]->GetXaxis()->SetTitleSize(0.05);
3074       FramenHit[side]->GetXaxis()->SetLabelSize(0.05);
3075       FramenHit[side]->GetYaxis()->SetTitleSize(0.05);
3076       FramenHit[side]->GetYaxis()->SetLabelSize(0.05);
3077       nHit[0][side]->SetMarkerStyle(21);
3078       nHit[0][side]->SetMarkerSize(0.5);
3079       nHit[0][side]->SetMarkerColor(2);
3080       nHit[0][side]->Draw("P");
3081       nHit[1][side]->SetMarkerStyle(22);
3082       nHit[1][side]->SetMarkerSize(0.5);
3083       nHit[1][side]->SetMarkerColor(4);
3084       nHit[1][side]->Draw("P");
3085 
3086       for (int i = 0; i < nPMT_1SIDE_BBC; i++)
3087       {
3088         if (nhit[0] > 100)
3089         {
3090           if (nhitPmt[0][side][i] < BbcMonDefs::BBC_nHIT_MB_MIN[side])
3091           {
3092             // RUN11: to ignore hit rate since ch29 before FEM input is dead.
3093             // RUN11: to ignore hit rate since the gain for ch40 is unstable.
3094             // if( (side==0)&&(i==29 || i==40))
3095             //{
3096             // if(i == 29){
3097             //    std::cout << "ch29 : Ignore hit rate into ch29" << std::endl;
3098             // }
3099             // else {
3100             //    std::cout << "ch40 : Ignore hit rate into ch40" << std::endl;
3101             // }
3102             //  continue;
3103             // }
3104 
3105             msg.str("");
3106             msg << "Too low hit-rate into ch " << i + 1 << " ("
3107                 << std::fixed << std::setprecision(2) << nhitPmt[0][side][i]
3108                 << "/" << std::setprecision(0) << nhit[0] << "evt)";
3109             std::string wmsg = msg.str();
3110             Warning(PadnHit[side], i, nhitPmt[0][side][i], 1, wmsg);
3111             wmsg.erase();
3112             msg.str("");
3113           }
3114           if (nhitPmt[0][side][i] > BbcMonDefs::BBC_nHIT_MB_MAX[side])
3115           {
3116             msg.str("");
3117             msg << "Too high hit-rate into ch " << i + 1 << " ("
3118                 << std::fixed << std::setprecision(2) << nhitPmt[0][side][i]
3119                 << "/" << std::setprecision(0) << nhit[0] << "evt)";
3120             std::string wmsg = msg.str();
3121             Warning(PadnHit[side], i, nhitPmt[0][side][i], 1, wmsg);
3122             wmsg.erase();
3123             msg.str("");
3124           }
3125         }
3126 
3127         if (nhit[1] > 0)
3128         {
3129           if (nhitPmt[1][side][i] < BbcMonDefs::BBC_nHIT_LASER_MIN[side])
3130           {
3131             if (side == 0)
3132             {
3133               // RUN12: to ignore lack of laser rate, since fiber of ch7 is broken.
3134               if (i == 7)
3135               {
3136                 std::cout << "ch7(S8) : Ignore hit rate of laser" << std::endl;
3137                 continue;
3138               }
3139               msg.str("");
3140               msg << "Lack of laser's hit into ch " << i + 1 << " ("
3141                   << std::fixed << std::setprecision(2) << nhitPmt[1][side][i]
3142                   << "/" << std::setprecision(0) << nhit[1] << "evt)";
3143               std::string wmsg = msg.str();
3144               Warning(PadnHit[side], i, nhitPmt[1][side][i], 1, wmsg);
3145               wmsg.erase();
3146               msg.str("");
3147             }
3148             else  // North
3149             {
3150               msg.str("");
3151               msg << "Lack of laser's hit into ch " << i + 1 << " ("
3152                   << std::fixed << std::setprecision(2) << nhitPmt[1][side][i]
3153                   << "/" << std::setprecision(0) << nhit[1] << "evt)";
3154               std::string wmsg = msg.str();
3155               Warning(PadnHit[side], i, nhitPmt[1][side][i], 1, wmsg);
3156               wmsg.erase();
3157               msg.str("");
3158             }
3159           }
3160         }
3161       }
3162       // if ( nhit[0] == 0 )
3163       // Warning( PadnHit[side], 1, 0, 1, "No Event" );
3164       // if ( nhit[1] == 0 )
3165       // Warning( PadnHit[side], 1, 0, 1, "No Laser Event" );
3166     }
3167 
3168     PadnHitStatus->cd();
3169     TextnHitStatus->Draw();
3170   }
3171 
3172 
3173   if(TC[8])
3174   {
3175     TC[8]->cd();
3176     for (int i = 0; i < static_cast<int>(TriggerEnum::NUM_MBD_TRIGGERS); i++)
3177     {
3178       PadAutoUpdate[i]->cd();
3179       if (ZvrtxAuto[i])
3180       {
3181         ZvrtxAuto[i]->Draw();
3182         ZvrtxAuto[i]->SetTitle(TriggerEnum::MBTriggerNames[i]);
3183         ZvrtxAuto[i]->GetXaxis()->SetTitle(Form("MBD ZVertex Trigger %s", TriggerEnum::MBTriggerNames[i]));
3184         ZvrtxAuto[i]->GetYaxis()->SetTitle("Number of Event");
3185         ZvrtxAuto[i]->GetXaxis()->SetTitleOffset(0.70);
3186         ZvrtxAuto[i]->GetYaxis()->SetTitleOffset(1.75);
3187         ZvrtxAuto[i]->GetXaxis()->SetLabelSize(0.07);
3188         ZvrtxAuto[i]->GetXaxis()->SetTickSize(0.1);
3189         ZvrtxAuto[i]->GetYaxis()->SetTitleOffset(1.75);
3190         ZvrtxAuto[i]->GetYaxis()->SetTitleSize(0.05);
3191       } else
3192       {
3193         TH1 *dummy = new TH1F(Form("DummyZvrtxAuto_%d", i), "No Data", 100, -60, 60);
3194         dummy->GetXaxis()->SetTitle(Form("MBD ZVertex Trigger %s",
3195                      TriggerEnum::MBTriggerNames[i]));
3196         dummy->GetYaxis()->SetTitle("Number of Event");   
3197         dummy->GetXaxis()->SetTitleOffset(0.70);
3198         dummy->GetYaxis()->SetTitleOffset(1.75);
3199         dummy->GetXaxis()->SetLabelSize(0.07);
3200         dummy->GetXaxis()->SetTickSize(0.1);
3201         dummy->GetYaxis()->SetTitleOffset(1.75);
3202         dummy->GetYaxis()->SetTitleSize(0.05);
3203         dummy->Draw();
3204       }
3205       PadAutoUpdate[i]->Update();
3206       
3207     }
3208 
3209   }
3210 
3211   //  std::cout << "Got Histgram Got-Pad 0" << std::endl;
3212 
3213   if (TC[0])
3214   {
3215     TC[0]->Update();
3216   }
3217   if (TC[1])
3218   {
3219     TC[1]->Update();
3220   }
3221   if (TC[2])
3222   {
3223     TC[2]->Update();
3224   }
3225   if (TC[3])
3226   {
3227     TC[3]->Update();
3228   }
3229   if (TC[8])
3230   {
3231     TC[8]->Update();
3232   }
3233 
3234 
3235   // std::cout << "Got Histgram Got-Pad 1" << std::endl;
3236 
3237   oldStyle->cd();
3238 
3239   //******************************************
3240   int iret = 0;
3241   //  int idraw = 0;
3242   /*
3243     if (what == "ALL" || what == "FIRST")
3244     {
3245     iret += DrawFirst(what);
3246     idraw++;
3247     }
3248     if (what == "ALL" || what == "SECOND")
3249     {
3250     iret += DrawSecond(what);
3251     idraw++;
3252     }
3253     if (what == "ALL" || what == "HISTORY")
3254     {
3255     iret += DrawHistory(what);
3256     //    idraw++;
3257     }
3258   */
3259 
3260   /*
3261     if (!idraw)
3262     {
3263     std::cout << __PRETTY_FUNCTION__ << " Unimplemented Drawing option: " << what << std::endl;
3264     iret = -1;
3265     }
3266   */
3267 
3268   //******************************************
3269   return iret;
3270 }
3271 
3272 int BbcMonDraw::DrawFirst(const std::string & /*unused*/)
3273 {
3274   PRINT_DEBUG("In BbcMonDraw::DrawFirst()");
3275   OnlMonClient *cl = OnlMonClient::instance();
3276   TH1 *bbcmon_hist1 = cl->getHisto("BBCMON_0", "bbc_zvertex");
3277   TH2 *bbcmon_hist2 = (TH2 *) cl->getHisto("BBCMON_0", "bbc_tzero_zvtx");
3278 
3279   if (!gROOT->FindObject("BbcMon1"))
3280   {
3281     MakeCanvas("BbcMon1");
3282   }
3283   TC[0]->SetEditable(true);
3284   TC[0]->Clear("D");
3285   Pad[0]->cd();
3286   if (bbcmon_hist1)
3287   {
3288     bbcmon_hist1->DrawCopy();
3289   }
3290   else
3291   {
3292     DrawDeadServer(transparent[0]);
3293     TC[0]->SetEditable(false);
3294     return -1;
3295   }
3296 
3297   Pad[1]->cd();
3298   if (bbcmon_hist2)
3299   {
3300     bbcmon_hist2->DrawCopy();
3301   }
3302   TText PrintRun;
3303   PrintRun.SetTextFont(62);
3304   PrintRun.SetTextSize(0.04);
3305   PrintRun.SetNDC();          // set to normalized coordinates
3306   PrintRun.SetTextAlign(23);  // center/top alignment
3307   std::ostringstream runnostream;
3308   std::string runstring;
3309   std::pair<time_t,int> evttime = cl->EventTime("CURRENT");
3310   // fill run number and event time into string
3311   runnostream << ThisName << "_1 Run " << cl->RunNumber()
3312               << ", Time: " << ctime(&evttime.first);
3313   runstring = runnostream.str();
3314   transparent[0]->cd();
3315   PrintRun.DrawText(0.5, 1., runstring.c_str());
3316   TC[0]->Update();
3317   TC[0]->Show();
3318   TC[0]->SetEditable(false);
3319   return 0;
3320 }
3321 
3322 int BbcMonDraw::DrawSecond(const std::string & /*unused*/)
3323 {
3324   PRINT_DEBUG("In BbcMonDraw::DrawSecond()");
3325 
3326   OnlMonClient *cl = OnlMonClient::instance();
3327   TH1 *bbcmon_hist1 = cl->getHisto("BBCMON_0", "bbc_zvertex");
3328   TH2 *bbcmon_hist2 = (TH2 *) cl->getHisto("BBCMON_0", "bbc_tzero_zvtx");
3329   if (!gROOT->FindObject("BbcMon2"))
3330   {
3331     MakeCanvas("BbcMon2");
3332   }
3333   TC[1]->SetEditable(true);
3334   TC[1]->Clear("D");
3335   Pad[2]->cd();
3336   if (bbcmon_hist1)
3337   {
3338     bbcmon_hist1->DrawCopy();
3339   }
3340   else
3341   {
3342     DrawDeadServer(transparent[1]);
3343     TC[1]->SetEditable(false);
3344     return -1;
3345   }
3346   Pad[3]->cd();
3347   if (bbcmon_hist2)
3348   {
3349     bbcmon_hist2->DrawCopy();
3350   }
3351   TText PrintRun;
3352   PrintRun.SetTextFont(62);
3353   PrintRun.SetTextSize(0.04);
3354   PrintRun.SetNDC();          // set to normalized coordinates
3355   PrintRun.SetTextAlign(23);  // center/top alignment
3356   std::ostringstream runnostream;
3357   std::string runstring;
3358   std::pair<time_t,int> evttime = cl->EventTime("CURRENT");
3359 
3360   // fill run number and event time into string
3361   runnostream << ThisName << "_2 Run " << cl->RunNumber()
3362               << ", Time: " << ctime(&evttime.first);
3363   runstring = runnostream.str();
3364   transparent[1]->cd();
3365   PrintRun.SetTextColor(evttime.second);
3366   PrintRun.DrawText(0.5, 1., runstring.c_str());
3367   TC[1]->Update();
3368   TC[1]->Show();
3369   TC[1]->SetEditable(false);
3370   return 0;
3371 }
3372 
3373 int BbcMonDraw::SavePlot(const std::string &what, const std::string &type)
3374 {
3375   OnlMonClient *cl = OnlMonClient::instance();
3376   int iret = Draw(what);
3377   if (iret)  // on error no png files please
3378   {
3379     return iret;
3380   }
3381   int icnt = 0;
3382   for (TCanvas *canvas : TC)
3383   {
3384     if (canvas == nullptr)
3385     {
3386       continue;
3387     }
3388     icnt++;
3389     std::string filename = ThisName + "_" + std::to_string(icnt) + "_" +
3390       std::to_string(cl->RunNumber()) + "." + type;
3391     cl->CanvasToPng(canvas, filename);
3392   }
3393   return 0;
3394 }
3395 
3396 int BbcMonDraw::MakeHtml(const std::string &what)
3397 {
3398   int iret = Draw(what);
3399   if (iret)  // on error no html output please
3400   {
3401     return iret;
3402   }
3403 
3404   OnlMonClient *cl = OnlMonClient::instance();
3405 
3406   int icnt = 0;
3407   for (TCanvas *canvas : TC)
3408   {
3409     if (canvas == nullptr)
3410     {
3411       continue;
3412     }
3413     icnt++;
3414     // Register the canvas png file to the menu and produces the png file.
3415     std::string pngfile = cl->htmlRegisterPage(*this, canvas->GetTitle(), std::to_string(icnt), "png");
3416     cl->CanvasToPng(canvas, pngfile);
3417   }
3418 
3419   // Now register also EXPERTS html pages, under the EXPERTS subfolder.
3420   // std::string logfile = cl->htmlRegisterPage(*this, "EXPERTS/Log", "log", "html");
3421   // std::ofstream out(logfile.c_str());
3422   // out << "<HTML><HEAD><TITLE>Log file for run " << cl->RunNumber()
3423   //     << "</TITLE></HEAD>" << std::endl;
3424   // out << "<P>Some log file output would go here." << std::endl;
3425   // out.close();
3426 
3427   // std::string status = cl->htmlRegisterPage(*this, "EXPERTS/Status", "status", "html");
3428   // std::ofstream out2(status.c_str());
3429   // out2 << "<HTML><HEAD><TITLE>Status file for run " << cl->RunNumber()
3430   //     << "</TITLE></HEAD>" << std::endl;
3431   // out2 << "<P>Some status output would go here." << std::endl;
3432   // out2.close();
3433 
3434   // cl->SaveLogFile(*this);
3435 
3436   return 0;
3437 }
3438 
3439 int BbcMonDraw::DrawHistory(const std::string & /* what */)
3440 {
3441   // you need to provide the following vectors
3442   // which are filled from the db
3443   std::vector<float> var;
3444   std::vector<float> varerr;
3445   std::vector<time_t> timestamp;
3446   /*chiu
3447     std::vector<int> runnumber;
3448     //std::string varname = "bbcmondummy";
3449     // this sets the time range from whihc values should be returned
3450     time_t begin = 0;            // begin of time (1.1.1970)
3451     time_t end = time(nullptr);  // current time (right NOW)
3452     int iret = dbvars->GetVar(begin, end, varname, timestamp, runnumber, var, varerr);
3453     if (iret)
3454     {
3455     std::cout << __PRETTY_FUNCTION__ << " Error in db access" << std::endl;
3456     return iret;
3457     }
3458   */
3459   if (!gROOT->FindObject("BbcMon4"))
3460   {
3461     MakeCanvas("BbcMon4");
3462   }
3463   // timestamps come sorted in ascending order
3464   float *x = new float[var.size()];
3465   float *y = new float[var.size()];
3466   float *ex = new float[var.size()];
3467   float *ey = new float[var.size()];
3468   // int n = var.size();
3469   for (unsigned int i = 0; i < var.size(); i++)
3470   {
3471     //       std::cout << "timestamp: " << ctime(&timestamp[i])
3472     //     << ", run: " << runnumber[i]
3473     //     << ", var: " << var[i]
3474     //     << ", varerr: " << varerr[i]
3475     //     << std::endl;
3476     x[i] = timestamp[i] - TimeOffsetTicks;
3477     y[i] = var[i];
3478     ex[i] = 0;
3479     ey[i] = varerr[i];
3480   }
3481 
3482   /* need to implement history for BBC
3483      Pad[4]->cd(); // neeed to fix
3484      if (gr[0])
3485      {
3486      delete gr[0];
3487      }
3488      gr[0] = new TGraphErrors(n, x, y, ex, ey);
3489      gr[0]->SetMarkerColor(4);
3490      gr[0]->SetMarkerStyle(21);
3491      gr[0]->Draw("ALP");
3492      gr[0]->GetXaxis()->SetTimeDisplay(1);
3493      gr[0]->GetXaxis()->SetLabelSize(0.03);
3494      // the x axis labeling looks like crap
3495      // please help me with this, the SetNdivisions
3496      // don't do the trick
3497      gr[0]->GetXaxis()->SetNdivisions(-1006);
3498      gr[0]->GetXaxis()->SetTimeOffset(TimeOffsetTicks);
3499      gr[0]->GetXaxis()->SetTimeFormat("%Y/%m/%d %H:%M");
3500   */
3501   delete[] x;
3502   delete[] y;
3503   delete[] ex;
3504   delete[] ey;
3505 
3506   /*chiu
3507     varname = "bbcmoncount";
3508     iret = dbvars->GetVar(begin, end, varname, timestamp, runnumber, var, varerr);
3509     if (iret)
3510     {
3511     std::cout << __PRETTY_FUNCTION__ << " Error in db access" << std::endl;
3512     return iret;
3513     }
3514   */
3515   x = new float[var.size()];
3516   y = new float[var.size()];
3517   ex = new float[var.size()];
3518   ey = new float[var.size()];
3519   // n = var.size();
3520   for (unsigned int i = 0; i < var.size(); i++)
3521   {
3522     //       std::cout << "timestamp: " << ctime(&timestamp[i])
3523     //     << ", run: " << runnumber[i]
3524     //     << ", var: " << var[i]
3525     //     << ", varerr: " << varerr[i]
3526     //     << std::endl;
3527     x[i] = timestamp[i] - TimeOffsetTicks;
3528     y[i] = var[i];
3529     ex[i] = 0;
3530     ey[i] = varerr[i];
3531   }
3532 
3533   /* Need to implement
3534      Pad[5]->cd();
3535      if (gr[1])
3536      {
3537      delete gr[1];
3538      }
3539      gr[1] = new TGraphErrors(n, x, y, ex, ey);
3540      gr[1]->SetMarkerColor(4);
3541      gr[1]->SetMarkerStyle(21);
3542      gr[1]->Draw("ALP");
3543      gr[1]->GetXaxis()->SetTimeDisplay(1);
3544      // TC[2]->Update();
3545      //    h1->GetXaxis()->SetTimeDisplay(1);
3546      //    h1->GetXaxis()->SetLabelSize(0.03);
3547      gr[1]->GetXaxis()->SetLabelSize(0.03);
3548      gr[1]->GetXaxis()->SetTimeOffset(TimeOffsetTicks);
3549      gr[1]->GetXaxis()->SetTimeFormat("%Y/%m/%d %H:%M");
3550      //    h1->Draw();
3551      */
3552 
3553   delete[] x;
3554   delete[] y;
3555   delete[] ex;
3556   delete[] ey;
3557 
3558   TC[3]->Update();
3559   return 0;
3560 }
3561 
3562 int BbcMonDraw::DrawDeadServer(TPad *transparent_pad)
3563 {
3564   transparent_pad->cd();
3565   TText FatalMsg;
3566   FatalMsg.SetTextFont(62);
3567   FatalMsg.SetTextSize(0.1);
3568   FatalMsg.SetTextColor(4);
3569   FatalMsg.SetNDC();          // set to normalized coordinates
3570   FatalMsg.SetTextAlign(23);  // center/top alignment
3571   FatalMsg.DrawText(0.5, 0.9, "BBCMONITOR");
3572   FatalMsg.SetTextAlign(22);  // center/center alignment
3573   FatalMsg.DrawText(0.5, 0.5, "SERVER");
3574   FatalMsg.SetTextAlign(21);  // center/bottom alignment
3575   FatalMsg.DrawText(0.5, 0.1, "DEAD");
3576   transparent_pad->Update();
3577   return 0;
3578 }
3579 
3580 void BbcMonDraw::GetMinBiasTrigName()
3581 {
3582   // look for MB triggers
3583   for ( int itrig = 0; itrig < TriggerEnum::nMBTriggers; itrig++ )
3584   {
3585     int ibit = static_cast<int>(TriggerEnum::MBTriggers[itrig]);
3586     double prescale = Prescale_hist->GetBinContent(ibit+1);
3587     if ( prescale >= 0 )
3588     {
3589       TrigName = TriggerEnum::MBTriggerNames[itrig];
3590       return;
3591     }
3592   }
3593   // maybe here we could fall back to a coincidence with an MBD trigger?
3594 
3595   // no mb bit found, take all triggers
3596   TrigName = "ALL TRIGGERS";
3597   return;
3598 }
3599