Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // use #include "" only for your local include and put
0002 // those in the first line(s) before any #include <>
0003 // otherwise you are asking for weird behavior
0004 // (more info - check the difference in include path search when using "" versus <>)
0005 
0006 #include "MvtxMon.h"
0007 
0008 //#include <fun4allraw/SingleMvtxInput.h>
0009 //#include <ffarawobjects/MvtxRawHitContainerv1.h>
0010 //#include <ffarawobjects/MvtxRawHitv1.h>
0011 //#include <ffarawobjects/MvtxRawEvtHeaderv1.h>
0012 
0013 #include <onlmon/OnlMon.h>  // for OnlMon
0014 #include <onlmon/OnlMonDB.h>
0015 #include <onlmon/OnlMonServer.h>
0016 
0017 #include <Event/msg_profile.h>
0018 
0019 #include <TH1.h>
0020 #include <TH2.h>
0021 #include <TH2Poly.h>
0022 #include <TH3.h>
0023 #include <TLatex.h>
0024 #include <TLine.h>
0025 #include <TList.h>
0026 #include <TString.h>
0027 
0028 #include <Event/Event.h>
0029 #include <Event/packet.h>
0030 
0031 #include <cmath>
0032 #include <cstdio>  // for printf
0033 #include <fstream>
0034 #include <iostream>
0035 #include <numeric>
0036 #include <sstream>
0037 #include <string>  // for allocator, string, char_traits
0038 #include <utility>
0039 
0040 enum
0041 {
0042   TRGMESSAGE = 1,
0043   FILLMESSAGE = 2
0044 };
0045 
0046 MvtxMon::MvtxMon(const std::string& name)
0047   : OnlMon(name)
0048 {
0049   // leave ctor fairly empty, its hard to debug if code crashes already
0050   // during a new MvtxMon()
0051   return;
0052 }
0053 
0054 MvtxMon::~MvtxMon()
0055 {
0056   // you can delete NULL pointers it results in a NOOP (No Operation)
0057   delete [] plist;
0058   return;
0059 }
0060 
0061 int MvtxMon::Init()
0062 {
0063   plist = new Packet*[2];
0064   // read our calibrations from MvtxMonData.dat
0065   const char* mvtxcalib = getenv("MVTXCALIB");
0066   if (!mvtxcalib)
0067   {
0068     std::cout << "MVTXCALIB environment variable not set" << std::endl;
0069     exit(1);
0070   }
0071   std::string fullfile = std::string(mvtxcalib) + "/" + "MvtxMonData.dat";
0072   std::ifstream calib(fullfile);
0073   calib.close();
0074   // use printf for stuff which should go the screen but not into the message
0075   // system (all couts are redirected)
0076   printf("doing the Init\n");
0077   OnlMonServer* se = OnlMonServer::instance();
0078 
0079   // register histograms with server otherwise client won't get them
0080   mvtxmon_ChipStaveOcc = new TH2D("OCC_ChipStaveOcc", "Average Occupancy: Stave Number and Chip Number", NCHIP, -0.5, NCHIP - 0.5, NSTAVE, -0.5, NSTAVE - 0.5);
0081   mvtxmon_ChipStave1D = new TH1D("OCC_ChipStave1D", "Average Occupancy per Chip", NCHIP * NSTAVE, -0.5, NCHIP * NSTAVE - 0.5);
0082   mvtxmon_ChipFiredHis = new TH1D("OCC_ChipFiredFLX", "Number of Chips Fired per Felix per RCDAQ event Distribution", NCHIP * NSTAVE / 6 +1 , -0.5, NCHIP * NSTAVE / 6 + 0.5);
0083   mvtxmon_EvtHitChip = new TH1D("OCC_HitChipPerStrobe", "Number of Hits Per Strobe Per Chip Distribution", 51, -0.5, 50.5);
0084   mvtxmon_EvtHitDis = new TH1D("OCC_HitFLXPerStrobe", "Number of Hits Per Strobe Distribution", 501, -0.5, 2000.5);
0085 
0086   mvtxmon_ChipStaveOcc->SetStats(false);
0087   mvtxmon_ChipStave1D->SetStats(false);
0088   mvtxmon_ChipFiredHis->SetStats(false);
0089   mvtxmon_EvtHitChip->SetStats(false);
0090   mvtxmon_EvtHitDis->SetStats(false);
0091 
0092   mvtxmon_ChipStave1D->GetXaxis()->SetTitle("Chip");
0093   mvtxmon_ChipStave1D->GetYaxis()->SetTitle("Occupancy");
0094 
0095   mvtxmon_ChipFiredHis->GetXaxis()->SetTitle("Number of Chips");
0096   mvtxmon_ChipFiredHis->GetYaxis()->SetTitle("Counts");
0097 
0098   mvtxmon_EvtHitChip->GetXaxis()->SetTitle("Number of Hits");
0099   mvtxmon_EvtHitChip->GetYaxis()->SetTitle("Counts");
0100 
0101   mvtxmon_EvtHitDis->GetXaxis()->SetTitle("Number of Hits");
0102   mvtxmon_EvtHitDis->GetYaxis()->SetTitle("Counts");
0103 
0104   se->registerHisto(this, mvtxmon_ChipStaveOcc);
0105   se->registerHisto(this, mvtxmon_ChipStave1D);
0106   se->registerHisto(this, mvtxmon_ChipFiredHis);
0107   se->registerHisto(this, mvtxmon_EvtHitChip);
0108   se->registerHisto(this, mvtxmon_EvtHitDis);
0109 
0110   mRCDAQevt = new TH1I("RCDAQ_evt", "Number of RCDAQ events processed", 6, -0.5, 5.5);
0111   mRCDAQevt->GetXaxis()->SetTitle("FELIX server");
0112   mRCDAQevt->GetYaxis()->SetTitle("Counts");
0113   mRCDAQevt->SetStats(false);
0114   se->registerHisto(this, mRCDAQevt);
0115 
0116 
0117   hStrobesDMA = new TH1I("hStrobesDMA", "Minimum number of stobes processed per DMA", 12, -0.5, 11.5);
0118   hStrobesDMA->GetXaxis()->SetTitle("DMA");
0119   hStrobesDMA->GetYaxis()->SetTitle("Counts");
0120   hStrobesDMA->SetStats(false);
0121   se->registerHisto(this, hStrobesDMA);
0122 
0123   hDMAstatus = new TH1I("hDMAstatus", "Is DMA receiving Strobes", 12, -0.5, 11.5);
0124   hDMAstatus->GetXaxis()->SetTitle("DMA");
0125   hDMAstatus->GetYaxis()->SetTitle("Alive");
0126   hDMAstatus->SetStats(false);
0127   se->registerHisto(this, hDMAstatus);
0128 
0129   for (int i = 0; i < NFlags+1; i++)
0130   {
0131     mvtxmon_LaneStatusOverview[i] = new TH2Poly();
0132     mvtxmon_LaneStatusOverview[i]->SetName(Form("FEE_LaneStatus_Overview_Flag%s", mLaneStatusFlag[i].c_str()));
0133     TString title = Form("Fraction of lanes into %s", mLaneStatusFlag[i].c_str());
0134     // title += ";mm (IB 3x);mm (IB 3x)";
0135     title += "; ; ";
0136     mvtxmon_LaneStatusOverview[i]->SetTitle(title);
0137     createPoly(mvtxmon_LaneStatusOverview[i]);
0138     se->registerHisto(this, mvtxmon_LaneStatusOverview[i]);  // mLaneStatusOverview
0139   }
0140 
0141  /* mLaneInfo = new TH2I("MVTXMON/FEE/LaneInfo", "Lane Information", NLanesMax, -.5, NLanesMax - 0.5, NFlags, -.5, NFlags - 0.5);
0142   mLaneInfo->GetXaxis()->SetTitle("Lane");
0143   mLaneInfo->GetYaxis()->SetTitle("Flag");
0144   mLaneInfo->SetStats(0);
0145   se->registerHisto(this, mLaneInfo);*/
0146 
0147   for (int i = 0; i < NFlags; i++) {
0148     mLaneStatus[i] = new TH2I(Form("FEE_LaneStatus_Flag_%s", mLaneStatusFlag[i].c_str()), Form("Lane Status Flag: %s", mLaneStatusFlag[i].c_str()), 9, -0.5, 8.5, 48, -0.5, 47.5);
0149     mLaneStatus[i]->GetXaxis()->SetTitle("Chip");
0150     mLaneStatus[i]->GetYaxis()->SetTitle("Stave");
0151     mLaneStatus[i]->SetStats(false);
0152     /*for (const int& lay : LayerBoundaryFEE)
0153     {
0154       auto l = new TLine(lay, 0, lay, mLaneStatus[i]->GetNbinsY());
0155       mLaneStatus[i]->GetListOfFunctions()->Add(l);
0156     }*/
0157     se->registerHisto(this, mLaneStatus[i]);
0158 
0159     mLaneStatusCumulative[i] = new TH2I(Form("FEE_LaneStatusFromSOX_Flag_%s", mLaneStatusFlag[i].c_str()), Form("Lane Status Flags since SOX: %s", mLaneStatusFlag[i].c_str()), 9, -0.5, 8.5, 48, -0.5, 47.5);
0160     mLaneStatusCumulative[i]->GetXaxis()->SetTitle("Chip");
0161     mLaneStatusCumulative[i]->GetYaxis()->SetTitle("Stave");
0162     mLaneStatusCumulative[i]->SetStats(false);
0163    /* for (const int& lay : LayerBoundaryFEE)
0164     {
0165       auto l = new TLine(lay, 0, lay, mLaneStatusCumulative[i]->GetNbinsY());
0166       mLaneStatusCumulative[i]->GetListOfFunctions()->Add(l);
0167     }*/
0168     se->registerHisto(this, mLaneStatusCumulative[i]);
0169   }
0170 
0171   /*for (int i = 0; i < NLAYERS; i++)
0172   {
0173     mLaneStatusSummary[i] = new TH1I(Form("FEE_LaneStatusSummary_Layer_%i", i), Form("Lane Status Summary L%i", i), 3, 0, 3);
0174     mLaneStatusSummary[i]->GetYaxis()->SetTitle("#Lanes");
0175     for (int j = 0; j < NFlags; j++)
0176     {
0177       mLaneStatusSummary[i]->GetXaxis()->SetBinLabel(j + 1, mLaneStatusFlag[j].c_str());
0178     }
0179     mLaneStatusSummary[i]->GetXaxis()->CenterLabels();
0180     mLaneStatusSummary[i]->SetStats(false);
0181     mLaneStatusSummary[i]->SetFillColor(kRed);
0182     se->registerHisto(this, mLaneStatusSummary[i]);
0183   }
0184 
0185   mLaneStatusSummaryIB = new TH1I("FEE_LaneStatusSummary", "Lane Status Summary", 3, 0, 3);
0186   mLaneStatusSummaryIB->GetYaxis()->SetTitle("#Lanes");
0187   for (int j = 0; j < NFlags; j++)
0188   {
0189     mLaneStatusSummaryIB->GetXaxis()->SetBinLabel(j + 1, mLaneStatusFlag[j].c_str());
0190   }
0191   mLaneStatusSummaryIB->GetXaxis()->CenterLabels();
0192   mLaneStatusSummaryIB->SetStats(false);
0193   mLaneStatusSummaryIB->SetFillColor(kRed);*/
0194   //se->registerHisto(this, mLaneStatusSummaryIB);
0195 
0196   // raw task
0197   hErrorPlots = new TH1D("General_DecErrors", "Decoding Errors", NError, 0.5, NError + 0.5);
0198   hErrorPlots->GetYaxis()->SetTitle("Counts");
0199   hErrorPlots->GetXaxis()->SetTitle("Error ID");
0200   hErrorPlots->SetMinimum(0);       // remove
0201   hErrorPlots->SetFillColor(kRed);  // remove
0202   hErrorPlots->SetStats(false);
0203 
0204   se->registerHisto(this, hErrorPlots);
0205 
0206   hErrorPlotsTime = new TH1D("General_DecErrorsTime", "Rolling History Of Decoding Errors", 50, 0.5, 50.5);
0207   hErrorPlotsTime->GetYaxis()->SetTitle("Number of Errors");
0208   hErrorPlotsTime->GetXaxis()->SetTitle("Time");
0209   hErrorPlotsTime->SetMinimum(0);       // remove
0210   hErrorPlotsTime->SetFillColor(kRed);  // remove
0211   hErrorPlotsTime->SetStats(false);
0212 
0213   se->registerHisto(this, hErrorPlotsTime);
0214 
0215 
0216   hErrorFile = new TH2D("General_DecErrorsEndpoint", "Decoding Errors vs Packet ID", 6 * 2, 0, 6 * 2 + 1, NError, 0.5, NError + 0.5);
0217   hErrorFile->GetYaxis()->SetTitle("Error ID");
0218   hErrorFile->GetXaxis()->SetTitle("2*FLX+endpoint");
0219   hErrorFile->GetZaxis()->SetTitle("Counts");
0220   hErrorFile->SetMinimum(0);
0221   hErrorFile->SetStats(false);
0222 /*
0223   TPaveText* pt[NError] = {nullptr};
0224   for (int i = 0; i < NError; i++)
0225   {
0226     pt[i] = new TPaveText(0.50, 0.825 - i * 0.025, 0.9, 0.85 - i * 0.025, "NDC");
0227     pt[i]->SetTextSize(0.015);
0228     pt[i]->SetTextAlign(12);
0229     pt[i]->SetFillColor(0);
0230     pt[i]->SetTextColor(2);
0231     pt[i]->AddText(ErrorType[i].Data());
0232     hErrorFile->GetListOfFunctions()->Add(pt[i]);
0233   }*/
0234 
0235   for (int i = 1; i < 6; i++)
0236   {
0237     auto l = new TLine(i * 2 + 0.2 + ((i - 3) * 0.12), 0.5, i * 2 + 0.2 + ((i - 3) * 0.12), hErrorFile->GetNbinsY() + 0.5);
0238     hErrorFile->GetListOfFunctions()->Add(l);
0239   }
0240 
0241   se->registerHisto(this, hErrorFile);
0242 
0243   for (int aLayer = 0; aLayer < 3; aLayer++)
0244   {
0245     hOccupancyPlot[aLayer] = new TH1D(Form("OCC_Occupancy1D_Layer%d", aLayer), Form("MVTX Layer %d, Occupancy Distribution", aLayer), 151, -5, 0.05);
0246     hOccupancyPlot[aLayer]->GetYaxis()->SetTitle("Counts");
0247     hOccupancyPlot[aLayer]->GetXaxis()->SetTitle("log10(Pixel Occupancy)");
0248     hOccupancyPlot[aLayer]->SetStats(false);
0249     se->registerHisto(this, hOccupancyPlot[aLayer]);
0250 
0251 
0252     hChipStaveOccupancy[aLayer] = new TH2D(Form("OCC_OccupancyChipStave_Layer_%d", aLayer), Form("MVTX Layer%d, Occupancy vs Chip and Stave", aLayer), 9, -.5, 9 - .5, NStaves[aLayer], -.5, NStaves[aLayer] - .5);
0253     hChipStaveOccupancy[aLayer]->GetYaxis()->SetTitle("Stave Number");
0254     hChipStaveOccupancy[aLayer]->GetXaxis()->SetTitle("Chip Number");
0255     hChipStaveOccupancy[aLayer]->GetZaxis()->SetTitle("Number of Hits");
0256     hChipStaveOccupancy[aLayer]->SetStats(false);
0257     se->registerHisto(this, hChipStaveOccupancy[aLayer]);
0258 
0259     TString tmp = Form("MVTX Layer %d", aLayer);
0260     tmp += ", Number of noisy pixels (>20\% stobes in event) per RCDAQ event";
0261 
0262 
0263     hChipStaveNoisy[aLayer] = new TH2D(Form("FHR_NoisyChipStave_Layer%d", aLayer), tmp, 9, -.5, 9 - .5, NStaves[aLayer], -.5, NStaves[aLayer] - .5);
0264     hChipStaveNoisy[aLayer]->GetYaxis()->SetTitle("Stave Number");
0265     hChipStaveNoisy[aLayer]->GetXaxis()->SetTitle("Chip Number");
0266     hChipStaveNoisy[aLayer]->GetZaxis()->SetTitle("Number of Noisy Pixels");
0267     hChipStaveNoisy[aLayer]->SetStats(false);
0268     se->registerHisto(this, hChipStaveNoisy[aLayer]);
0269   }
0270 
0271   hChipHitmap = new TH3I("MVTXMON_chipHitmap", "MVTXMON_chipHitmap", 1024, -.5, 1023.5, 512, -.5, 511.5, 8 * 9 * 6, -.5, 8 * 9 * 6 - 0.5);
0272   //hChipHitmap_evt = new TH3I(Form("MVTXMON_chipHitmapFLX%d_evt", this->MonitorServerId()), Form("MVTXMON_chipHitmapFLX%d_evt", this->MonitorServerId()), 1024, -.5, 1023.5, 512, -.5, 511.5, 8 * 9 * 6, -.5, 8 * 9 * 6 - 0.5);
0273   hChipHitmap->GetXaxis()->SetTitle("Col");
0274   hChipHitmap->GetYaxis()->SetTitle("Row");
0275   //hChipHitmap->SetStats(false);
0276   // se->registerHisto(this, hChipHitmap);
0277 
0278   hChipStrobes = new TH1I("General_hChipStrobes", "Chip Strobes vs Chip*Stave", 8 * 9 * 6, -.5, 8 * 9 * 6 - 0.5);
0279   hChipStrobes->GetXaxis()->SetTitle("Chip");
0280   hChipStrobes->GetYaxis()->SetTitle("Counts");
0281   hChipStrobes->SetStats(false);
0282   se->registerHisto(this, hChipStrobes);
0283 
0284   hChipL1 = new TH1I("General_ChipL1", "L1 triggers vs Chip*Stave", 8 * 9 * 6, -.5, 8 * 9 * 6 - 0.5);
0285   hChipL1->GetXaxis()->SetTitle("Chip");
0286   hChipL1->GetYaxis()->SetTitle("Counts");
0287   hChipL1->SetStats(false);
0288   se->registerHisto(this, hChipL1);
0289 
0290   hFeeStrobes = new TH1I("General_hfeeStrobes", "Chip Strobes vs FeeId", NFees,0,NFees);
0291   hFeeStrobes->GetXaxis()->SetTitle("FEE ID");
0292   hFeeStrobes->GetYaxis()->SetTitle("Number of MVTX strobes");
0293   hFeeStrobes->SetStats(0);
0294   se->registerHisto(this, hFeeStrobes);
0295 
0296   hFeeL1 = new TH1I("General_feeL1", "L1 triggers vs FeeId", NFees,0,NFees);
0297   hFeeL1->GetXaxis()->SetTitle("FEE ID");
0298   hFeeL1->GetYaxis()->SetTitle("L1 Triggers");
0299   hFeeL1->SetStats(0);
0300   se->registerHisto(this, hFeeL1);
0301 
0302   hFeeRDHErrors = new TH1I("RDHErrors_hfeeRDHErrors", "RDH Errors vs FeeId", NFees,0,NFees);
0303   hFeeRDHErrors->GetXaxis()->SetTitle("FEE ID");
0304   hFeeRDHErrors->GetYaxis()->SetTitle("Number of MVTX RDH Errors");
0305   hFeeRDHErrors->SetStats(0);
0306   se->registerHisto(this, hFeeRDHErrors);
0307 
0308   // fhr
0309   mErrorVsFeeid = new TH2I("FHR_ErrorVsFeeid", "Decoder error vs FeeID", 3 * StaveBoundary[3], 0, 3 * StaveBoundary[3], NError, 0.5, NError + 0.5);;
0310   mErrorVsFeeid->GetXaxis()->SetTitle("FEE ID");
0311   mErrorVsFeeid->GetYaxis()->SetTitle("Error ID");
0312   mErrorVsFeeid->SetStats(false);
0313   se->registerHisto(this, mErrorVsFeeid);
0314 
0315   mGeneralOccupancy = new TH2Poly();
0316   mGeneralOccupancy->SetTitle("General Occupancy; ; ");
0317   mGeneralOccupancy->SetName("MVTXMON_General_Occupancy");
0318   // mGeneralOccupancy->GetXaxis()->SetTitle("");
0319   // mGeneralOccupancy->GetYaxis()->SetTitle("");
0320   mGeneralOccupancy->SetStats(false);
0321 
0322   mGeneralNoisyPixel = new TH2Poly();
0323   mGeneralNoisyPixel->SetTitle("Unmasked Noisy Pixel Number; ; ");
0324   mGeneralNoisyPixel->SetName("MVTXMON_General_Noisy_Pixel");
0325   mGeneralNoisyPixel->GetXaxis()->SetTitle("");
0326   mGeneralNoisyPixel->GetYaxis()->SetTitle("");
0327   mGeneralNoisyPixel->SetStats(false);
0328   mGeneralNoisyPixel->SetMinimum(-1);
0329   mGeneralNoisyPixel->SetMaximum(1200);
0330 
0331   createPoly(mGeneralOccupancy);
0332   createPoly(mGeneralNoisyPixel);
0333   se->registerHisto(this, mGeneralOccupancy);
0334   se->registerHisto(this, mGeneralNoisyPixel);
0335 
0336   for (int mLayer = 0; mLayer < 3; mLayer++)
0337   {
0338     mDeadChipPos[mLayer] = new TH2D(Form("MVTXMON_Occupancy_Layer%d_Layer%dDeadChipPos", mLayer, mLayer), Form("DeadChipPos on Layer %d", mLayer), 9, -0.5, 9 - 0.5, NStaves[mLayer], -0.5, NStaves[mLayer] - 0.5);
0339     mDeadChipPos[mLayer]->GetXaxis()->SetTitle("Chip Number");
0340     mDeadChipPos[mLayer]->GetYaxis()->SetTitle("Stave Number");
0341     mDeadChipPos[mLayer]->SetStats(false);
0342     //mAliveChipPos[mLayer] = new TH2D(Form("MVTXMON_Occupancy_Layer%d_Layer%dAliveChipPos", mLayer, mLayer), Form("Fraction of RCDAQ events a chip received data on Layer %d", mLayer), 9, -0.5, 9 - 0.5, NStaves[mLayer], -0.5, NStaves[mLayer] - 0.5);
0343     //mAliveChipPos[mLayer]->GetXaxis()->SetTitle("Chip Number");
0344     //mAliveChipPos[mLayer]->GetYaxis()->SetTitle("Stave Number");
0345     //mAliveChipPos[mLayer]->SetStats(false);
0346 
0347     mDeadChipPos[mLayer]->SetStats(false);
0348     //mAliveChipPos[mLayer]->SetStats(false);
0349 
0350     se->registerHisto(this, mDeadChipPos[mLayer]);
0351     //se->registerHisto(this, mAliveChipPos[mLayer]);
0352   }
0353 
0354   /*mTotalDeadChipPos = new TH2D(Form("MVTXMON_Occupancy_TotalDeadChipPos"), Form("TotalDeadChipPos "), 9, -0.5, 9 - 0.5, NStaves[2], -0.5, NStaves[2] - 0.5);
0355   mTotalDeadChipPos->GetXaxis()->SetTitle("Chip Number");
0356   mTotalDeadChipPos->GetYaxis()->SetTitle("Stave Number");
0357   se->registerHisto(this, mTotalDeadChipPos);
0358 
0359   mTotalAliveChipPos = new TH2D(Form("MVTXMON_Occupancy_TotalAliveChipPos"), Form("TotalAliveChipPos "), 9, -0.5, 9 - 0.5, NStaves[2], -0.5, NStaves[2] - 0.5);
0360   mTotalAliveChipPos->GetXaxis()->SetTitle("Chip Number");
0361   mTotalAliveChipPos->GetYaxis()->SetTitle("Stave Number");
0362   se->registerHisto(this, mTotalAliveChipPos);*/
0363 
0364   Reset();
0365   return 0;
0366 }
0367 
0368 int MvtxMon::BeginRun(const int /* runno */)
0369 {
0370   // if you need to read calibrations on a run by run basis
0371   // this is the place to do it
0372   return 0;
0373 }
0374 
0375 int MvtxMon::process_event(Event* evt)
0376 {
0377   std::cout<<"event "<<evtcnt<<std::endl;
0378   evtcnt++;
0379   mRCDAQevt->Fill(this->MonitorServerId());
0380 
0381   OnlMonServer* se = OnlMonServer::instance();
0382 
0383 
0384   //per event resets
0385   for (int iLayer = 0; iLayer < 3; iLayer++) 
0386   {
0387     for (int iStave = 0; iStave < NStaves[iLayer]; iStave++) 
0388     {
0389       for (int iChip = 0; iChip < 9; iChip++) 
0390       {
0391         mHitPerChip[iLayer][iStave][iChip] = 0;
0392         mNoisyPixelNumber[iLayer][iStave][iChip] = 0;
0393       }
0394     }
0395   }
0396 
0397   //hChipHitmap_evt->Reset("ICESM");
0398   mLaneStatus[0]->Reset("ICESM");
0399   mLaneStatus[1]->Reset("ICESM");
0400   mLaneStatus[2]->Reset("ICESM");
0401   mvtxmon_LaneStatusOverview[0]->Reset("ICESM");
0402   mvtxmon_LaneStatusOverview[1]->Reset("ICESM");
0403   mvtxmon_LaneStatusOverview[2]->Reset("ICESM");
0404   mvtxmon_LaneStatusOverview[3]->Reset("ICESM");
0405   hOccupancyPlot[0]->Reset("ICESM");
0406   hOccupancyPlot[1]->Reset("ICESM");
0407   hOccupancyPlot[2]->Reset("ICESM");
0408   hChipStaveNoisy[0]->Reset("ICESM");
0409   hChipStaveNoisy[1]->Reset("ICESM");
0410   hChipStaveNoisy[2]->Reset("ICESM");
0411   //hStrobesDMA->Reset("ICESM");
0412 
0413   //set DMA dead
0414   hDMAstatus->SetBinContent((this->MonitorServerId() * 2) + 1, 0);
0415   hDMAstatus->SetBinContent((this->MonitorServerId() * 2) + 2, 0);
0416 
0417 
0418   int nChipStrobes[8 * 9 * 6] = {0};
0419 
0420 
0421   int nDecError = 0;
0422 
0423   int npackets = evt->getPacketList(plist, 2);
0424 
0425   if (npackets > 2)
0426   {
0427     delete plist[0];
0428     delete plist[1];
0429     return 0;
0430   }
0431 
0432   for (int i = 0; i < npackets; i++)
0433   {
0434     // Ignoring packet not from MVTX detector (e.g. begin/end run)
0435     if ((plist[i]->getIdentifier() < 2001) || (plist[i]->getIdentifier() > 2052))
0436     {
0437       delete plist[i];
0438       continue;
0439     }
0440     if (Verbosity() > 1)
0441     {
0442       plist[i]->identify();
0443     }
0444     int num_feeId = plist[i]->iValue(-1, "NR_LINKS");
0445     if (Verbosity() > 1)
0446     {
0447       std::cout << "Number of feeid in RCDAQ events: " << num_feeId << " for packet "
0448                 << plist[i]->getIdentifier() << std::endl;
0449     }
0450     if (num_feeId > 0)
0451     {
0452       int  min_strobes = 1000000000;
0453       int sum_strobes = 0;
0454       for (int i_fee{0}; i_fee < num_feeId; ++i_fee)
0455       {      
0456         auto feeId = plist[i]->iValue(i_fee, "FEEID");
0457         int cur_strobes = plist[i]->iValue(feeId, "NR_STROBES");
0458         if (cur_strobes < min_strobes) min_strobes = cur_strobes;
0459         sum_strobes += cur_strobes;
0460       }
0461       if(plist[i]->getIdentifier()%10 == 1) hStrobesDMA->SetBinContent((this->MonitorServerId() * 2) +1, hStrobesDMA->GetBinContent((this->MonitorServerId() * 2) +1) + min_strobes);
0462       if(plist[i]->getIdentifier()%10 == 2) hStrobesDMA->SetBinContent((this->MonitorServerId() * 2) +2, hStrobesDMA->GetBinContent((this->MonitorServerId() * 2) +2) + min_strobes);
0463 
0464       if(sum_strobes > 0){ //alive
0465         if(plist[i]->getIdentifier()%10 == 1) hDMAstatus->SetBinContent((this->MonitorServerId() * 2) +1, 1);
0466         if(plist[i]->getIdentifier()%10 == 2) hDMAstatus->SetBinContent((this->MonitorServerId() * 2) +2, 1);
0467       }
0468       
0469       for (int i_fee{0}; i_fee < num_feeId; ++i_fee)
0470       {
0471         auto feeId = plist[i]->iValue(i_fee, "FEEID");
0472         auto link = DecodeFeeid(feeId);
0473         auto num_strobes = plist[i]->iValue(feeId, "NR_STROBES");
0474         auto num_RDHErrors = plist[i]->iValue(feeId, "RDH_ERRORS");
0475         ntriggers = num_strobes;
0476         auto num_L1Trgs = plist[i]->iValue(feeId, "NR_PHYS_TRG");
0477         for (int iL1 = 0; iL1 < num_L1Trgs; ++iL1)
0478         {
0479           // auto l1Trg_bco = plist[i]->lValue(feeId, iL1, "L1_IR_BCO");
0480           hChipL1->Fill((StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + 0);  // same for chip id 0 1 and 2
0481           hChipL1->Fill((StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + 1);
0482           hChipL1->Fill((StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + 2);
0483           hFeeL1->Fill((StaveBoundary[link.layer] + link.stave % 20) * 3 + link.gbtid);
0484         }
0485 
0486         for (int irdhe = 0; irdhe < num_RDHErrors; ++irdhe)
0487         {
0488           hFeeRDHErrors->Fill((StaveBoundary[link.layer] + link.stave % 20) * 3 + link.gbtid);
0489         }
0490 
0491         for (int i_strb{0}; i_strb < num_strobes; ++i_strb)
0492         {
0493           auto strb_bco = plist[i]->lValue(feeId, i_strb, "TRG_IR_BCO");
0494           auto num_hits = plist[i]->iValue(feeId, i_strb, "TRG_NR_HITS");
0495           if (Verbosity() > 4)
0496           {
0497             if (link.layer == 0)
0498             {
0499               std::cout << "evtno: "
0500                         << ", Fee: " << feeId;
0501               std::cout << " Layer: " << link.layer << " Stave: " << link.stave;
0502               std::cout << " GBT: " << link.gbtid << ", bco: 0x" << std::hex << strb_bco << std::dec;
0503               std::cout << ", n_hits: " << num_hits << std::endl;
0504             }
0505           }
0506           hChipStrobes->Fill((StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + 0);  // same for chip id 0 1 and 2
0507           hChipStrobes->Fill((StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + 1);
0508           hChipStrobes->Fill((StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + 2);
0509           hFeeStrobes->Fill((StaveBoundary[link.layer] + link.stave % 20) * 3 + link.gbtid);
0510           nChipStrobes[(StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + 0]++;
0511           nChipStrobes[(StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + 1]++;
0512           nChipStrobes[(StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + 2]++;
0513 
0514           for (int i_hit{0}; i_hit < num_hits; ++i_hit)
0515           {
0516             auto chip_id = plist[i]->iValue(feeId, i_strb, i_hit, "HIT_CHIP_ID");
0517             auto chip_row = plist[i]->iValue(feeId, i_strb, i_hit, "HIT_ROW");
0518             auto chip_col = plist[i]->iValue(feeId, i_strb, i_hit, "HIT_COL");
0519 
0520             mHitPerChip[link.layer][link.stave % 20][3 * link.gbtid + chip_id]++;
0521             hChipHitmap->Fill(chip_col, chip_row, (StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + chip_id);
0522             
0523             //std::cout<<"fill "<<chip_col<<" "<<chip_row<<" "<<(StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + chip_id<<std::endl;
0524             hChipStaveOccupancy[link.layer]->Fill(3 * link.gbtid + chip_id, link.stave % 20);
0525             //hChipHitmap_evt->Fill(chip_col, chip_row, (StaveBoundary[link.layer] + link.stave % 20) * 9 + 3 * link.gbtid + chip_id);
0526           }
0527         }
0528 
0529         int ifee_plot = 3 * StaveBoundary[link.layer] + 3 * link.stave + link.gbtid;
0530         auto lane_error = plist[i]->iValue(feeId, "tdt_lanestatus_error");
0531   
0532 
0533         while (lane_error != -1)
0534         {
0535           //std::cout<<"feeid: "<<feeId<<" lane error: "<<lane_error<<std::endl;
0536 
0537           for (unsigned int ilane = 0; ilane < NLanesMax; ilane++)
0538           {
0539             unsigned int laneValue = lane_error >> (2 * ilane) & 0x3;
0540 
0541             if (laneValue)
0542             {
0543               // mStatusFlagNumber[link.layer][link.stave][i%3][laneValue]++;
0544               // std::cout<<"lanevalue: "<<laneValue<<" layer "<<link.layer<<" stave "<<link.stave<<" lane "<<ilane<<std::endl;
0545 
0546               mLaneStatus[laneValue - 1]->Fill(ilane, StaveBoundary[link.layer] + link.stave);
0547               mLaneStatusCumulative[laneValue - 1]->Fill(ilane, StaveBoundary[link.layer] + link.stave);
0548               //mLaneStatusSummary[link.layer]->Fill(laneValue - 0.5);
0549 
0550               mStatusFlagNumber[laneValue - 1][link.layer][link.stave][ilane]++;
0551               //mLaneStatusSummaryIB->Fill(laneValue - 0.5);
0552             }
0553           }
0554 
0555           lane_error = plist[i]->iValue(feeId, "tdt_lanestatus_error");
0556         }
0557 
0558         auto decoder_error = plist[i]->lValue(feeId, "decoder_error");
0559         while (decoder_error != -1)
0560         {
0561          
0562           int error = decoder_error & 0xFFFFFFFF;
0563           hErrorPlots->Fill(error);
0564           nDecError++;
0565           
0566           hErrorFile->Fill((this->MonitorServerId() * 2) + i + 0.5, error);
0567           mErrorVsFeeid->Fill(ifee_plot,error);
0568           decoder_error = plist[i]->lValue(feeId, "decoder_error");
0569         }
0570       }
0571     }
0572     delete plist[i];
0573   }
0574 
0575   for(int bin = 1; bin < 50 ; bin ++){
0576     hErrorPlotsTime->SetBinContent(bin,hErrorPlotsTime->GetBinContent(bin+1));
0577   }
0578   hErrorPlotsTime->SetBinContent(50,nDecError);
0579 
0580 
0581   int firedChips = 0;
0582   int firedPixels = 0;
0583   int sumstrobes = 0;
0584   int nstrobes = 0;
0585   for (int l = 0; l < NLAYERS; l++)
0586   {
0587     for (int s = 0; s < NStaves[l]; s++)
0588     {
0589       for (int j = 0; j < NCHIP; j++)
0590       {
0591         if (mHitPerChip[l][s][j] > 0)
0592         {
0593           firedChips++;
0594           mvtxmon_EvtHitChip->Fill(mHitPerChip[l][s][j] / nChipStrobes[(StaveBoundary[l] + s) * 9 + j]);
0595           firedPixels += mHitPerChip[l][s][j];
0596           sumstrobes += nChipStrobes[(StaveBoundary[l] + s) * 9 + j];
0597           nstrobes++;
0598         }
0599       }
0600     }
0601   }
0602   mvtxmon_ChipFiredHis->Fill(firedChips);
0603   mvtxmon_EvtHitDis->Fill((double) firedPixels / ((double) sumstrobes / (double) nstrobes));
0604 
0605 
0606 
0607   //OCCUPANCY PLOTS
0608 
0609   double chip_occ[9]{};
0610   double pixelOccupancy, chipOccupancy;
0611 
0612   for (int iLayer = 0; iLayer < 3; iLayer++)
0613   {
0614     for (int iStave = 0; iStave < NStaves[iLayer]; iStave++)
0615     {   
0616       for (int iChip = 0; iChip < 9; iChip++)
0617       {
0618         chip_occ[iChip] = 0;
0619         chipOccupancy = hChipHitmap->Integral(0, -1, 0, -1, (StaveBoundary[iLayer] + iStave) * 9 + iChip + 1, (StaveBoundary[iLayer] + iStave) * 9 + iChip + 1);  // scale at client
0620         double chipOccupancyNorm = -1;
0621         if(hChipStrobes->GetBinContent((StaveBoundary[iLayer] + iStave) * 9 + iChip + 1) > 0) chipOccupancyNorm = chipOccupancy / hChipStrobes->GetBinContent((StaveBoundary[iLayer] + iStave) * 9 + iChip + 1) / 1024 / 512;
0622         if (chipOccupancyNorm > 0)
0623         {
0624           mvtxmon_ChipStave1D->SetBinContent((StaveBoundary[iLayer] + iStave) * 9 + iChip + 1, chipOccupancyNorm);
0625           chip_occ[iChip] = chipOccupancyNorm;
0626         }
0627         
0628         for (int iCol = 0; iCol < NCols; iCol++)
0629         {
0630           for (int iRow = 0; iRow < NRows; iRow++)
0631           {
0632             pixelOccupancy = hChipHitmap->GetBinContent(iCol + 1, iRow + 1, (StaveBoundary[iLayer] + iStave) * 9 + iChip + 1);
0633             if (pixelOccupancy > 0)
0634             {
0635               hOccupancyPlot[iLayer]->Fill(log10(pixelOccupancy / (double)(hChipStrobes->GetBinContent((StaveBoundary[iLayer] + iStave) * 9 + iChip + 1))));
0636               if (pixelOccupancy / (double) (hChipStrobes->GetBinContent((StaveBoundary[iLayer] + iStave) * 9 + iChip + 1)) > mOccupancyCutForNoisyPixel)
0637               {
0638                 mNoisyPixelNumber[iLayer][iStave][iChip]++;
0639               }
0640             }
0641           }
0642         }
0643       }
0644       //max occupancy in a stave
0645       mGeneralOccupancy->SetBinContent(mapstave[iLayer][iStave], *(std::max_element(chip_occ, chip_occ + 9)));
0646     }
0647   }
0648 
0649   //NOISY PIXELS
0650   for (int iLayer = 0; iLayer < 3; iLayer++)
0651   {
0652     for (int iStave = 0; iStave < NStaves[iLayer]; iStave++)
0653     {
0654       for (int iChip = 0; iChip < 9; iChip++)
0655       {
0656         double noisy = std::accumulate(mNoisyPixelNumber[iLayer][iStave], mNoisyPixelNumber[iLayer][iStave] + 9,0);
0657         mGeneralNoisyPixel->SetBinContent(mapstave[iLayer][iStave], noisy);
0658         if (mNoisyPixelNumber[iLayer][iStave][iChip] > 0)
0659         {
0660           hChipStaveNoisy[iLayer]->SetBinContent(iChip + 1, iStave + 1, mNoisyPixelNumber[iLayer][iStave][iChip]);
0661         }
0662       }
0663     }
0664   }
0665 
0666   //CHIP ERRORS
0667   for (int iLayer = 0; iLayer < 3; iLayer++)
0668   {
0669     for (int iStave = 0; iStave < NStaves[iLayer]; iStave++)
0670     {
0671       for (int iFlag = 0; iFlag < 3; iFlag++)
0672       {
0673         for (int iChip = 0; iChip < 9; iChip++)
0674         {
0675           if (mStatusFlagNumber[iFlag][iLayer][iStave][iChip] > 0)
0676           {
0677             mvtxmon_LaneStatusOverview[iFlag]->SetBinContent(mapstave[iLayer][iStave], mvtxmon_LaneStatusOverview[iFlag]->GetBinContent(mapstave[iLayer][iStave]) + 1);
0678             mvtxmon_LaneStatusOverview[3]->SetBinContent(mapstave[iLayer][iStave], mvtxmon_LaneStatusOverview[iFlag]->GetBinContent(mapstave[iLayer][iStave]) + 1);
0679           }
0680         }
0681         mvtxmon_LaneStatusOverview[iFlag]->SetBinContent(mapstave[iLayer][iStave], static_cast<double>(mvtxmon_LaneStatusOverview[iFlag]->GetBinContent(mapstave[iLayer][iStave])) / 9);
0682       }
0683     }
0684   }
0685 
0686 
0687   for (int iLayer = 0; iLayer < 3; iLayer++)
0688   {
0689     for (int iStave = 0; iStave < NStaves[iLayer]; iStave++)
0690     {
0691       for (int iChip = 0; iChip < 9; iChip++)
0692       {
0693         if (!mHitPerChip[iLayer][iStave][iChip])
0694         {
0695           if (mDeadChipPos[iLayer]->GetBinContent(mDeadChipPos[iLayer]->GetXaxis()->FindBin(iChip), mDeadChipPos[iLayer]->GetYaxis()->FindBin(iStave)) > 0.5)
0696           {
0697             mDeadChipPos[iLayer]->SetBinContent(mDeadChipPos[iLayer]->GetXaxis()->FindBin(iChip), mDeadChipPos[iLayer]->GetYaxis()->FindBin(iStave), 1);
0698           }
0699         }
0700         else
0701         {
0702           mDeadChipPos[iLayer]->SetBinContent(mDeadChipPos[iLayer]->GetXaxis()->FindBin(iChip), mDeadChipPos[iLayer]->GetYaxis()->FindBin(iStave), 0);  // not dead
0703         }
0704       }
0705     }
0706   }
0707 
0708   // get temporary pointers to histograms
0709   // one can do in principle directly se->getHisto("mvtxhist1")->Fill()
0710   // but the search in the histogram Map is somewhat expensive and slows
0711   // things down if you make more than one operation on a histogram
0712 
0713   std::ostringstream msg;
0714   msg << "Filling Histos";
0715   se->send_message(this, MSG_SOURCE_UNSPECIFIED, MSG_SEV_INFORMATIONAL, msg.str(), FILLMESSAGE);
0716   idummy = 0;
0717   return 0;
0718 }
0719 
0720 int MvtxMon::Reset()
0721 {
0722 
0723   hChipHitmap->Reset("ICESM");
0724 
0725   for (int mLayer = 0; mLayer < 3; mLayer++)
0726   {
0727     for (int binx = 0; binx < mDeadChipPos[mLayer]->GetNbinsX(); binx++)
0728     {
0729       for (int biny = 0; biny < mDeadChipPos[mLayer]->GetNbinsY(); biny++)
0730       {
0731         mDeadChipPos[mLayer]->SetBinContent(binx + 1, biny + 1, 1);
0732       }
0733     }
0734   }
0735 
0736   for (int iLayer = 0; iLayer < 3; iLayer++) 
0737   {
0738     for (int iStave = 0; iStave < NStaves[iLayer]; iStave++) 
0739     {
0740       for (int iChip = 0; iChip < 9; iChip++) 
0741       {
0742         for (int iFlag = 0; iFlag < 3; iFlag++) {
0743           mStatusFlagNumber[iFlag][iLayer][iStave][iChip] = 0;
0744         }
0745       }
0746     }
0747   }
0748   // reset our internal counters
0749 
0750   evtcnt = 0;
0751   idummy = 0;
0752   return 0;
0753 }
0754 
0755 void MvtxMon::getStavePoint(int layer, int stave, double* px, double* py)
0756 {
0757   float stepAngle = M_PI * 2 / NStaves[layer];               // the angle between to stave
0758   float midAngle = StartAngle[layer] + (stave * stepAngle);  // mid point angle
0759   float staveRotateAngle = M_PI / 2 - (stave * stepAngle);   // how many angle this stave rotate(compare with first stave)
0760   px[1] = MidPointRad[layer] * std::cos(midAngle);           // there are 4 point to decide this TH2Poly bin
0761                                                              // 0:left point in this stave;
0762                                                              // 1:mid point in this stave;
0763                                                              // 2:right point in this stave;
0764                                                              // 3:higher point int this stave;
0765   py[1] = MidPointRad[layer] * std::sin(midAngle);           // 4 point calculated accord the blueprint
0766                                                              // roughly calculate
0767   px[0] = 7.7 * std::cos(staveRotateAngle) + px[1];
0768   py[0] = -7.7 * std::sin(staveRotateAngle) + py[1];
0769   px[2] = -7.7 * std::cos(staveRotateAngle) + px[1];
0770   py[2] = 7.7 * std::sin(staveRotateAngle) + py[1];
0771   px[3] = 5.623 * std::sin(staveRotateAngle) + px[1];
0772   py[3] = 5.623 * std::cos(staveRotateAngle) + py[1];
0773 }
0774 
0775 void MvtxMon::createPoly(TH2Poly* h)
0776 {
0777   for (int ilayer = 0; ilayer < NLAYERS; ilayer++)
0778   {
0779     for (int istave = 0; istave < NStaves[ilayer]; istave++)
0780     {
0781       double* px = new double[4];
0782       double* py = new double[4];
0783       getStavePoint(ilayer, istave, px, py);
0784       for (int icoo = 0; icoo < 4; icoo++)
0785       {
0786         px[icoo] *= 3.;
0787         py[icoo] *= 3.;
0788       }
0789       h->AddBin(4, px, py);
0790     }
0791   }
0792 }