CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/DQM/DTMonitorModule/src/DTTimeEvolutionHisto.cc

Go to the documentation of this file.
00001 
00002 /*
00003  *  See header file for a description of this class.
00004  *
00005  *  $Date: 2011/06/10 13:23:26 $
00006  *  $Revision: 1.6 $
00007  *  \author G. Cerminara - INFN Torino
00008  */
00009 
00010 #include "DQM/DTMonitorModule/interface/DTTimeEvolutionHisto.h"
00011 
00012 #include "DQMServices/Core/interface/DQMStore.h"
00013 #include "DQMServices/Core/interface/MonitorElement.h"
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015 
00016 using namespace std;
00017 using namespace edm;
00018 
00019 
00020 DTTimeEvolutionHisto::DTTimeEvolutionHisto(DQMStore *dbe, const string& name,
00021                                            const string& title,
00022                                            int nbins,
00023                                            int lsPrescale,
00024                                            bool sliding,
00025                                            int mode) : valueLastTimeSlot(0),
00026                                                        theFirstLS(1),
00027                                                        theLSPrescale(lsPrescale),
00028                                                        doSlide(sliding),
00029                                                        theMode(mode) {
00030   
00031   DTTimeEvolutionHisto(dbe, name, title, nbins, theFirstLS, lsPrescale, sliding, mode);
00032   histo = dbe->get(dbe->pwd() + "/" + name);
00033   nBookedBins = histo->getNbinsX();
00034 }
00035 
00036 
00037 // DTTimeEvolutionHisto::DTTimeEvolutionHisto(DQMStore *dbe, const string& name,
00038 //                                         const string& title,
00039 //                                         int nbins,
00040 //                                         int lsPrescale,
00041 //                                         bool sliding,
00042 //                                         int mode) : valueLastTimeSlot(0),
00043 //                                                     theLSPrescale(lsPrescale),
00044 //                                                     doSlide(sliding),
00045 //                                                     theMode(mode) {
00046 //   // set the number of bins to be booked
00047 //   nBookedBins = nbins;
00048 //   if(sliding) nBookedBins++;
00049 //   if(!sliding && theMode == 0)
00050 //     LogWarning("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00051 //       << "[DTTimeEvolutionHisto]***Error: wrong configuration" << endl;
00052 
00053 
00054 //   stringstream realTitle; realTitle << title << "/" <<  theLSPrescale << " LS";
00055 
00056 //   // book the ME
00057 //   histo = dbe->book1D(name, realTitle.str(), nBookedBins, 1., nBookedBins+1.);
00058 
00059 //   // set the axis label
00060 //   if(sliding) {
00061 //     histo->setBinLabel(1,"avg. previous",1);
00062 //   } else {
00063 //     // loop over bins and 
00064 //     for(int bin =1; bin != nBookedBins+1; ++bin) {    
00065 //       stringstream label;
00066 //       if(theLSPrescale > 1) {
00067 //      label << "LS " << ((bin-1)*theLSPrescale)+1 << "-" << bin*theLSPrescale;
00068 //       } else {
00069 //      label << "LS " << ((bin-1)*theLSPrescale)+1;
00070 //       }
00071 //       histo->setBinLabel(bin, label.str(),1);
00072 
00073 //     }
00074 
00075 //   }
00076 
00077 // }
00078 
00079 
00080 
00081 
00082 DTTimeEvolutionHisto::DTTimeEvolutionHisto(DQMStore *dbe, const string& name,
00083                                            const string& title,
00084                                            int nbins,
00085                                            int firstLS,
00086                                            int lsPrescale,
00087                                            bool sliding,
00088                                            int mode) : valueLastTimeSlot(0),
00089                                                        theFirstLS(firstLS),
00090                                                        theLSPrescale(lsPrescale),
00091                                                        doSlide(sliding),
00092                                                        theMode(mode) {
00093   // set the number of bins to be booked
00094   nBookedBins = nbins;
00095   if(sliding) nBookedBins++;
00096   if(!sliding && theMode == 0)
00097     LogWarning("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00098       << "[DTTimeEvolutionHisto]***Error: wrong configuration" << endl;
00099 
00100 
00101   stringstream realTitle; realTitle << title << "/" <<  theLSPrescale << " LS";
00102 
00103   // book the ME
00104   histo = dbe->book1D(name, realTitle.str(), nBookedBins, (float)theFirstLS, nBookedBins+1.);
00105 
00106   // set the axis label
00107   if(sliding) {
00108     histo->setBinLabel(1,"avg. previous",1);
00109   } else {
00110     // loop over bins and 
00111     for(int bin =1; bin != nBookedBins+1; ++bin) {    
00112       stringstream label;
00113       if(theLSPrescale > 1) {
00114         label << "LS " << ((bin-1)*theLSPrescale)+theFirstLS << "-" << bin*theLSPrescale+theFirstLS;
00115       } else {
00116         label << "LS " << ((bin-1)*theLSPrescale)+theFirstLS;
00117       }
00118       histo->setBinLabel(bin, label.str(),1);
00119 
00120     }
00121 
00122   }
00123 
00124 }
00125 
00126 
00127 
00128 DTTimeEvolutionHisto::DTTimeEvolutionHisto(DQMStore *dbe, const string& name) : valueLastTimeSlot(0),
00129                                                                                 theFirstLS(1),
00130                                                                                 theLSPrescale(-1),
00131                                                                                 doSlide(false),
00132                                                                                 theMode(0) { // FIXME: set other memebers to sensible values
00133   LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00134     << "[DTTimeEvolutionHisto] Retrieve ME with name: " << name << endl;
00135   histo = dbe->get(name);
00136 }
00137 
00138 
00139 DTTimeEvolutionHisto::~DTTimeEvolutionHisto(){}
00140 
00141 
00142 
00143 void DTTimeEvolutionHisto::setTimeSlotValue(float value, int timeSlot) {
00144   //   LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00145   //   << "[DTTimeEvolutionHisto] ME name: " <<  histo->getName() << endl;
00146   if(!doSlide) {
00147     //     LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00148     //     << "        fill bin: " << timeSlot << " with value: " << value << endl;
00149     histo->Fill(timeSlot,value);
00150   } else {
00151     for(int bin = 1; bin != nBookedBins; ++bin) {
00152       float value = histo->getBinContent(bin);
00153       //       LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00154       //  << "        bin: " << bin << " has value: " << value << endl;
00155       if(bin == 1) { // average of previous time slots (fixme)
00156         histo->setBinContent(bin, (value + histo->getBinContent(bin+1))/2.);
00157       } else if(bin != nBookedBins) {
00158         histo->setBinContent(bin, histo->getBinContent(bin+1));
00159         histo->setBinError(bin, histo->getBinError(bin+1));
00160         histo->setBinLabel(bin, histo->getTH1F()->GetXaxis()->GetBinLabel(bin+1),1);
00161       }
00162     }
00163     histo->setBinContent(nBookedBins, value);
00164   }
00165 }
00166 
00167 
00168 void DTTimeEvolutionHisto::accumulateValueTimeSlot(float value) {
00169   valueLastTimeSlot += value;
00170 }
00171 
00172 
00173 
00174 void DTTimeEvolutionHisto::updateTimeSlot(int ls, int nEventsInLS) {
00175   
00176   if(doSlide) { // sliding bins
00177     // count LS in this time-slot
00178     if (nEventsInLastTimeSlot.find(ls) != nEventsInLastTimeSlot.end()) {
00179       nEventsInLastTimeSlot[ls] += nEventsInLS;
00180       nLumiTrInLastTimeSlot[ls]++;
00181     } else {
00182       nEventsInLastTimeSlot[ls] = nEventsInLS;
00183       nLumiTrInLastTimeSlot[ls] = 1;
00184     }
00185     
00186 
00187     if(nEventsInLastTimeSlot.size() > 0 && nEventsInLastTimeSlot.size()%theLSPrescale==0) { // update the value of the slot and reset the counters
00188       int firstLSinTimeSlot = nEventsInLastTimeSlot.begin()->first;
00189       int lastLSinTimeSlot  = nEventsInLastTimeSlot.rbegin()->first;
00190       
00191       map<int,int>::const_iterator nEventsIt  = nEventsInLastTimeSlot.begin();
00192       map<int,int>::const_iterator nEventsEnd = nEventsInLastTimeSlot.end();
00193 
00194       int nEvents = 0;
00195       for (;nEventsIt!=nEventsEnd;++nEventsIt) 
00196         nEvents+=nEventsIt->second;     
00197         
00198       LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00199         << "[DTTimeEvolutionHisto] Update time-slot, # entries: " << valueLastTimeSlot 
00200         << " # events: " << nEvents << endl;
00201       // set the bin content
00202 
00203       float value = 0;
00204       if(theMode == 0) {
00205         if(nEvents != 0) value = valueLastTimeSlot/(float)nEvents;
00206       } else if(theMode == 1) {
00207         value = valueLastTimeSlot;
00208       } else if(theMode == 2) {
00209         value = nEvents;
00210       } else if(theMode == 3) {
00211         map<int,int>::const_iterator nLumiTrIt  = nLumiTrInLastTimeSlot.begin();
00212         map<int,int>::const_iterator nLumiTrEnd = nLumiTrInLastTimeSlot.end();
00213 
00214         float nLumiTr = 0.;
00215         for (;nLumiTrIt!=nLumiTrEnd;++nLumiTrIt) 
00216           nLumiTr+=nLumiTrIt->second;   
00217 
00218         value = valueLastTimeSlot/nLumiTr;
00219       }
00220       setTimeSlotValue(value, nBookedBins);
00221       LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00222         << "       updated value: " << histo->getBinContent(nBookedBins) << endl;
00223 
00224       // set the bin label
00225       stringstream binLabel; 
00226       binLabel << "LS " << firstLSinTimeSlot;
00227       if(nEventsInLastTimeSlot.size() > 1) 
00228         binLabel << "-" << lastLSinTimeSlot;
00229 
00230       histo->setBinLabel(nBookedBins,binLabel.str(),1);
00231 
00232       // reset the counters for the time slot
00233       nEventsInLastTimeSlot.clear();
00234       nLumiTrInLastTimeSlot.clear();
00235       valueLastTimeSlot = 0;
00236     }
00237 
00238 
00239   } else {
00240     int binN = (int)ls-(theFirstLS-1)/(int)theLSPrescale;
00241     // set the bin content
00242     float value = 0;
00243     if(theMode == 1) {
00244       value = valueLastTimeSlot;
00245     } else if(theMode == 2) {
00246       value = nEventsInLS;
00247     } else if(theMode == 3) {
00248       value = valueLastTimeSlot/theLSPrescale;
00249     }
00250     LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00251       << "[DTTimeEvolutionHisto] Update time-slot: "<< binN << " with value: " << value << endl;
00252     setTimeSlotValue(value,binN);
00253   }
00254 }
00255 
00256 
00257 
00258 
00259 void DTTimeEvolutionHisto::normalizeTo(const MonitorElement *histForNorm) {
00260   if(histo == 0) {
00261     LogWarning("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00262       << "[DTTimeEvolutionHisto]***Error: pointer to ME is NULL" << endl;
00263     return;
00264   }
00265   int nBins = histo->getNbinsX();
00266   if(histForNorm->getNbinsX() != nBins) {
00267     LogWarning("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00268       << "[DTTimeEvolutionHisto]***Error: normalizing histos with != # of bins" << endl;
00269     return;
00270   }
00271   for(int bin = 1; bin <= nBins; ++bin) { // loop over bins
00272     if(histForNorm->getBinContent(bin) != 0) {
00273       double normValue = histo->getBinContent(bin)/histForNorm->getBinContent(bin);
00274       LogVerbatim("DTDQM|DTMonitorModule|DTMonitorClient|DTTimeEvolutionHisto")
00275         << "[DTTimeEvolutionHisto] Normalizing bin: " << bin << " to: " <<  histo->getBinContent(bin) << " / " << histForNorm->getBinContent(bin)
00276         << " = " << normValue << endl;
00277       histo->setBinContent(bin, normValue);
00278     } else {
00279       histo->setBinContent(bin, 0);
00280     }
00281   }
00282 }