Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-03 04:09:55

0001 
0002 /*
0003  *  See header file for a description of this class.
0004  *
0005  *  \author G. Cerminara - INFN Torino
0006  */
0007 
0008 #include "DQM/DTMonitorModule/interface/DTTimeEvolutionHisto.h"
0009 
0010 #include "DQMServices/Core/interface/DQMStore.h"
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 
0013 using namespace std;
0014 using namespace edm;
0015 
0016 DTTimeEvolutionHisto::DTTimeEvolutionHisto(DQMStore::IBooker& ibooker,
0017                                            const string& name,
0018                                            const string& title,
0019                                            int nbins,
0020                                            int lsPrescale,
0021                                            bool sliding,
0022                                            int mode)
0023     : DTTimeEvolutionHisto(ibooker, name, title, nbins, 1, lsPrescale, sliding, mode) {
0024   nBookedBins = histo->getNbinsX();
0025 }
0026 
0027 DTTimeEvolutionHisto::DTTimeEvolutionHisto(DQMStore::IBooker& ibooker,
0028                                            const string& name,
0029                                            const string& title,
0030                                            int nbins,
0031                                            int firstLS,
0032                                            int lsPrescale,
0033                                            bool sliding,
0034                                            int mode)
0035     : valueLastTimeSlot(0), theFirstLS(firstLS), theLSPrescale(lsPrescale), doSlide(sliding), theMode(mode) {
0036   // set the number of bins to be booked
0037   nBookedBins = nbins;
0038   binLabelCounter = -1;
0039   if (sliding)
0040     nBookedBins++;
0041   if (!sliding && theMode == 0)
0042     LogWarning("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
0043         << "[DTTimeEvolutionHisto]***Error: wrong configuration" << endl;
0044 
0045   stringstream realTitle;
0046   realTitle << title << "/" << theLSPrescale << " LS";
0047 
0048   // book the ME
0049 
0050   histo = ibooker.book1D(name, realTitle.str(), nBookedBins, (float)theFirstLS, nBookedBins + 1.);
0051 
0052   // set the axis label
0053   if (sliding) {
0054     histo->setBinLabel(1, "avg. previous", 1);
0055   } else {
0056     // loop over bins and
0057 
0058     for (int bin = 1; bin != nBookedBins + 1; ++bin) {
0059       stringstream label;
0060       if (theLSPrescale > 1) {
0061         label << "LS " << ((bin - 1) * theLSPrescale) + theFirstLS << "-" << bin * theLSPrescale + theFirstLS;
0062       } else {
0063         label << "LS " << ((bin - 1) * theLSPrescale) + theFirstLS;
0064       }
0065       if (bin % (2 * (int)theLSPrescale) == 0)
0066         histo->setBinLabel(bin, label.str(), 1);  //JF to allow easy reading of x-axis
0067     }
0068   }
0069 }
0070 
0071 //FR changed previous constructor with 2 arguments:
0072 //no igetter here!! so I get the histo from the client and just instanciate here a DTTimeEvolutionHisto
0073 
0074 DTTimeEvolutionHisto::DTTimeEvolutionHisto(MonitorElement* histoGot)
0075     : valueLastTimeSlot(0),
0076       theFirstLS(1),
0077       theLSPrescale(-1),
0078       doSlide(false),
0079       theMode(0) {  // FIXME: set other memebers to sensible values
0080   LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
0081       << "[DTTimeEvolutionHisto] Retrieve ME with name: "
0082       << " " << endl;
0083   histo = histoGot;
0084 }
0085 
0086 DTTimeEvolutionHisto::~DTTimeEvolutionHisto() {}
0087 
0088 void DTTimeEvolutionHisto::setTimeSlotValue(float value, int timeSlot) {
0089   if (!doSlide) {
0090     histo->Fill(timeSlot, value);
0091   } else {
0092     for (int bin = 1; bin != nBookedBins; ++bin) {
0093       float value = histo->getBinContent(bin);
0094 
0095       if (bin == 1) {  // average of previous time slots (fixme)
0096         histo->setBinContent(bin, (value + histo->getBinContent(bin + 1)) / 2.);
0097       } else if (bin != nBookedBins) {
0098         histo->setBinContent(bin, histo->getBinContent(bin + 1));
0099         histo->setBinError(bin, histo->getBinError(bin + 1));
0100         histo->setBinLabel(bin, histo->getTH1F()->GetXaxis()->GetBinLabel(bin + 1), 1);  //slide to left
0101         histo->setBinLabel(bin + 1, "", 1);  //delete old label to avoid duplication
0102       }
0103     }
0104     histo->setBinContent(nBookedBins, value);
0105   }
0106 }
0107 
0108 void DTTimeEvolutionHisto::accumulateValueTimeSlot(float value) { valueLastTimeSlot += value; }
0109 
0110 void DTTimeEvolutionHisto::updateTimeSlot(int ls, int nEventsInLS) {
0111   if (doSlide) {  // sliding bins
0112     // count LS in this time-slot
0113     if (nEventsInLastTimeSlot.find(ls) != nEventsInLastTimeSlot.end()) {
0114       nEventsInLastTimeSlot[ls] += nEventsInLS;
0115       nLumiTrInLastTimeSlot[ls]++;
0116     } else {
0117       nEventsInLastTimeSlot[ls] = nEventsInLS;
0118       nLumiTrInLastTimeSlot[ls] = 1;
0119     }
0120 
0121     if (!nEventsInLastTimeSlot.empty() &&
0122         nEventsInLastTimeSlot.size() % theLSPrescale == 0) {  // update the value of the slot and reset the counters
0123       int firstLSinTimeSlot = nEventsInLastTimeSlot.begin()->first;
0124       int lastLSinTimeSlot = nEventsInLastTimeSlot.rbegin()->first;
0125 
0126       map<int, int>::const_iterator nEventsIt = nEventsInLastTimeSlot.begin();
0127       map<int, int>::const_iterator nEventsEnd = nEventsInLastTimeSlot.end();
0128 
0129       int nEvents = 0;
0130       for (; nEventsIt != nEventsEnd; ++nEventsIt)
0131         nEvents += nEventsIt->second;
0132 
0133       LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
0134           << "[DTTimeEvolutionHisto] Update time-slot, # entries: " << valueLastTimeSlot << " # events: " << nEvents
0135           << endl;
0136       // set the bin content
0137 
0138       float value = 0;
0139       if (theMode == 0) {
0140         if (nEvents != 0)
0141           value = valueLastTimeSlot / (float)nEvents;
0142       } else if (theMode == 1) {
0143         value = valueLastTimeSlot;
0144       } else if (theMode == 2) {
0145         value = nEvents;
0146       } else if (theMode == 3) {
0147         map<int, int>::const_iterator nLumiTrIt = nLumiTrInLastTimeSlot.begin();
0148         map<int, int>::const_iterator nLumiTrEnd = nLumiTrInLastTimeSlot.end();
0149 
0150         float nLumiTr = 0.;
0151         for (; nLumiTrIt != nLumiTrEnd; ++nLumiTrIt)
0152           nLumiTr += nLumiTrIt->second;
0153 
0154         value = valueLastTimeSlot / nLumiTr;
0155       }
0156       setTimeSlotValue(value, nBookedBins);
0157       LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
0158           << "       updated value: " << histo->getBinContent(nBookedBins) << endl;
0159 
0160       // set the bin label
0161       stringstream binLabel;
0162       binLabel << "LS " << firstLSinTimeSlot;
0163       if (nEventsInLastTimeSlot.size() > 1) {
0164         binLabel << "-" << lastLSinTimeSlot;
0165         binLabelCounter++;
0166       }
0167 
0168       // Set only labels which can be seen in the plot without zooming
0169       if (binLabelCounter % ((int)(nBookedBins / 25)) == 0)  //around 25 labels fit in a full size hist
0170         histo->setBinLabel(nBookedBins, binLabel.str(), 1);
0171       //first label, LS=1 ideally, is guaranteed by setting binLabelCounter=-1 in constructor
0172 
0173       // reset the counters for the time slot
0174       nEventsInLastTimeSlot.clear();
0175       nLumiTrInLastTimeSlot.clear();
0176       valueLastTimeSlot = 0;
0177     }
0178 
0179   } else {
0180     int binN = (int)ls - (theFirstLS - 1) / (int)theLSPrescale;
0181     // set the bin content
0182     float value = 0;
0183     if (theMode == 1) {
0184       value = valueLastTimeSlot;
0185     } else if (theMode == 2) {
0186       value = nEventsInLS;
0187     } else if (theMode == 3) {
0188       value = valueLastTimeSlot / theLSPrescale;
0189     }
0190     LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
0191         << "[DTTimeEvolutionHisto] Update time-slot: " << binN << " with value: " << value << endl;
0192     setTimeSlotValue(value, binN);
0193   }
0194 }
0195 
0196 void DTTimeEvolutionHisto::normalizeTo(const MonitorElement* histForNorm) {
0197   if (histo == nullptr) {
0198     LogWarning("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
0199         << "[DTTimeEvolutionHisto]***Error: pointer to ME is NULL" << endl;
0200     return;
0201   }
0202   int nBins = histo->getNbinsX();
0203   if (histForNorm->getNbinsX() != nBins) {
0204     LogWarning("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
0205         << "[DTTimeEvolutionHisto]***Error: normalizing histos with != # of bins" << endl;
0206     return;
0207   }
0208   for (int bin = 1; bin <= nBins; ++bin) {  // loop over bins
0209     if (histForNorm->getBinContent(bin) != 0) {
0210       double normValue = histo->getBinContent(bin) / histForNorm->getBinContent(bin);
0211       LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
0212           << "[DTTimeEvolutionHisto] Normalizing bin: " << bin << " to: " << histo->getBinContent(bin) << " / "
0213           << histForNorm->getBinContent(bin) << " = " << normValue << endl;
0214       histo->setBinContent(bin, normValue);
0215     } else {
0216       histo->setBinContent(bin, 0);
0217     }
0218   }
0219 }
0220 
0221 // Local Variables:
0222 // show-trailing-whitespace: t
0223 // truncate-lines: t
0224 // End: