Back to home page

sPhenix code displayed by LXR

 
 

    


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

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