Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:09:20

0001 /*
0002  * \file AlcaBeamMonitorClient.cc
0003  * \author Lorenzo Uplegger/FNAL
0004  *
0005  */
0006 
0007 #include "DQM/BeamMonitor/plugins/AlcaBeamMonitorClient.h"
0008 #include <numeric>
0009 #include "FWCore/Framework/interface/ESHandle.h"
0010 #include "FWCore/Framework/interface/EventSetup.h"
0011 #include "FWCore/Framework/interface/LuminosityBlock.h"
0012 #include "FWCore/Framework/interface/MakerMacros.h"
0013 #include "FWCore/Framework/interface/Run.h"
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 #include "FWCore/ServiceRegistry/interface/Service.h"
0016 //#include <iostream>
0017 
0018 using namespace std;
0019 using namespace edm;
0020 // using namespace reco;
0021 
0022 //----------------------------------------------------------------------------------------------------------------------
0023 AlcaBeamMonitorClient::AlcaBeamMonitorClient(const ParameterSet& ps)
0024     : parameters_(ps),
0025       monitorName_(parameters_.getUntrackedParameter<string>("MonitorName", "YourSubsystemName")),
0026       numberOfValuesToSave_(0) {
0027   dbe_ = Service<DQMStore>().operator->();
0028 
0029   if (!monitorName_.empty())
0030     monitorName_ = monitorName_ + "/";
0031 
0032   varNamesV_.push_back("x");
0033   varNamesV_.push_back("y");
0034   varNamesV_.push_back("z");
0035   varNamesV_.push_back("sigmaX");
0036   varNamesV_.push_back("sigmaY");
0037   varNamesV_.push_back("sigmaZ");
0038 
0039   histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased BeamSpotFit"));
0040   histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased PrimaryVertex"));
0041   histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased DataBase"));
0042   histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased Scalers"));
0043   histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased PrimaryVertex-DataBase fit"));
0044   histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased PrimaryVertex-Scalers fit"));
0045   histoByCategoryNames_.insert(pair<string, string>("validation", "Lumibased Scalers-DataBase fit"));
0046   histoByCategoryNames_.insert(pair<string, string>("validation", "Lumibased PrimaryVertex-DataBase"));
0047   histoByCategoryNames_.insert(pair<string, string>("validation", "Lumibased PrimaryVertex-Scalers"));
0048 
0049   for (vector<string>::iterator itV = varNamesV_.begin(); itV != varNamesV_.end(); itV++) {
0050     for (multimap<string, string>::iterator itM = histoByCategoryNames_.begin(); itM != histoByCategoryNames_.end();
0051          itM++) {
0052       histosMap_[*itV][itM->first][itM->second] = nullptr;
0053       positionsMap_[*itV][itM->first][itM->second] = 3 * numberOfValuesToSave_;  // value, error, ok
0054       ++numberOfValuesToSave_;
0055     }
0056   }
0057 }
0058 
0059 AlcaBeamMonitorClient::~AlcaBeamMonitorClient() {}
0060 
0061 //----------------------------------------------------------------------------------------------------------------------
0062 void AlcaBeamMonitorClient::beginJob() {}
0063 
0064 //----------------------------------------------------------------------------------------------------------------------
0065 void AlcaBeamMonitorClient::beginRun(const edm::Run& r, const EventSetup& context) {}
0066 
0067 //----------------------------------------------------------------------------------------------------------------------
0068 
0069 //----------------------------------------------------------------------------------------------------------------------
0070 void AlcaBeamMonitorClient::analyze(const Event& iEvent, const EventSetup& iSetup) {}
0071 
0072 //----------------------------------------------------------------------------------------------------------------------
0073 void AlcaBeamMonitorClient::endLuminosityBlock(const LuminosityBlock& iLumi, const EventSetup& iSetup) {
0074   MonitorElement* tmp = nullptr;
0075   tmp = dbe_->get(monitorName_ + "Service/hHistoLumiValues");
0076   if (!tmp) {
0077     return;
0078   }
0079   valuesMap_[iLumi.id().luminosityBlock()] = vector<double>();
0080   for (int i = 0; i < 3 * numberOfValuesToSave_; i++) {
0081     valuesMap_[iLumi.id().luminosityBlock()].push_back(tmp->getTProfile()->GetBinContent(i + 1));
0082   }
0083 }
0084 
0085 //----------------------------------------------------------------------------------------------------------------------
0086 void AlcaBeamMonitorClient::endRun(const Run& iRun, const EventSetup& context) {
0087   // use this in case any LS is missing.
0088   if (valuesMap_.empty()) {
0089     LogInfo("AlcaBeamMonitorClient") << "The histogram "
0090                                      << monitorName_ +
0091                                             "Service/hHistoLumiValues which contains all the values has not "
0092                                             "been found in any lumi!";
0093     return;
0094   }
0095   int lastLumi = (--valuesMap_.end())->first;
0096   int firstLumi = valuesMap_.begin()->first;
0097 
0098   // create and cd into new folder
0099   dbe_->setCurrentFolder(monitorName_ + "Validation");
0100 
0101   LogInfo("AlcaBeamMonitorClient") << "End of run " << iRun.id().run() << "(" << firstLumi << "-" << lastLumi << ")";
0102 
0103   string name;
0104   string title;
0105   // x,y,z,sigmaX,sigmaY,sigmaZ
0106   for (HistosContainer::iterator itM = histosMap_.begin(); itM != histosMap_.end(); itM++) {
0107     for (map<string, map<string, MonitorElement*> >::iterator itMM = itM->second.begin(); itMM != itM->second.end();
0108          itMM++) {
0109       if (itMM->first != "run") {
0110         for (map<string, MonitorElement*>::iterator itMMM = itMM->second.begin(); itMMM != itMM->second.end();
0111              itMMM++) {
0112           name = string("h") + itM->first + itMMM->first;
0113           title = itM->first + "_{0} " + itMMM->first;
0114           if (itMM->first == "lumi") {
0115             dbe_->setCurrentFolder(monitorName_ + "Debug");
0116             itMMM->second = dbe_->book1D(name, title, lastLumi - firstLumi + 1, firstLumi - 0.5, lastLumi + 0.5);
0117             itMMM->second->setEfficiencyFlag();
0118           } else if (itMM->first == "validation" && itMMM->first == "Lumibased Scalers-DataBase fit") {
0119             dbe_->setCurrentFolder(monitorName_ + "Validation");
0120             itMMM->second = dbe_->book1D(name, title, lastLumi - firstLumi + 1, firstLumi - 0.5, lastLumi + 0.5);
0121             itMMM->second->setEfficiencyFlag();
0122           } else if (itMM->first == "validation" && itMMM->first != "Lumibased Scalers-DataBase fit" &&
0123                      (itM->first == "x" || itM->first == "y" || itM->first == "z")) {
0124             dbe_->setCurrentFolder(monitorName_ + "Validation");
0125             itMMM->second = dbe_->book1D(name, title, lastLumi - firstLumi + 1, firstLumi - 0.5, lastLumi + 0.5);
0126             itMMM->second->setEfficiencyFlag();
0127           } else if (itMM->first == "validation" &&
0128                      (itM->first == "sigmaX" || itM->first == "sigmaY" || itM->first == "sigmaZ")) {
0129             dbe_->setCurrentFolder(monitorName_ + "Validation");
0130             itMMM->second = nullptr;
0131           } else {
0132             LogInfo("AlcaBeamMonitorClient") << "Unrecognized category " << itMM->first;
0133             // assert(0);
0134           }
0135           if (itMMM->second != nullptr) {
0136             if (itMMM->first.find('-') != string::npos) {
0137               itMMM->second->setAxisTitle(string("#Delta ") + itM->first + "_{0} (cm)", 2);
0138             } else {
0139               itMMM->second->setAxisTitle(itM->first + "_{0} (cm)", 2);
0140             }
0141             itMMM->second->setAxisTitle("Lumisection", 1);
0142           }
0143         }
0144       }
0145     }
0146   }
0147 
0148   unsigned int bin = 0;
0149   for (HistosContainer::iterator itH = histosMap_.begin(); itH != histosMap_.end(); itH++) {
0150     for (map<string, map<string, MonitorElement*> >::iterator itHH = itH->second.begin(); itHH != itH->second.end();
0151          itHH++) {
0152       for (map<string, MonitorElement*>::iterator itHHH = itHH->second.begin(); itHHH != itHH->second.end(); itHHH++) {
0153         for (map<LuminosityBlockNumber_t, vector<double> >::iterator itVal = valuesMap_.begin();
0154              itVal != valuesMap_.end();
0155              itVal++) {
0156           if (itHHH->second != nullptr) {
0157             //      cout << positionsMap_[itH->first][itHH->first][itHHH->first]
0158             //<< endl;
0159             if (itVal->second[positionsMap_[itH->first][itHH->first][itHHH->first] + 2] == 1) {
0160               bin = itHHH->second->getTH1()->FindBin(itVal->first);
0161               itHHH->second->setBinContent(bin, itVal->second[positionsMap_[itH->first][itHH->first][itHHH->first]]);
0162               itHHH->second->setBinError(bin, itVal->second[positionsMap_[itH->first][itHH->first][itHHH->first] + 1]);
0163             }
0164           }
0165         }
0166       }
0167     }
0168   }
0169 
0170   /**/
0171   const double bigNumber = 1000000.;
0172   for (HistosContainer::iterator itH = histosMap_.begin(); itH != histosMap_.end(); itH++) {
0173     for (map<string, map<string, MonitorElement*> >::iterator itHH = itH->second.begin(); itHH != itH->second.end();
0174          itHH++) {
0175       double min = bigNumber;
0176       double max = -bigNumber;
0177       double minDelta = bigNumber;
0178       double maxDelta = -bigNumber;
0179       //      double minDeltaProf = bigNumber;
0180       //      double maxDeltaProf = -bigNumber;
0181       if (itHH->first != "run") {
0182         for (map<string, MonitorElement*>::iterator itHHH = itHH->second.begin(); itHHH != itHH->second.end();
0183              itHHH++) {
0184           if (itHHH->second != nullptr) {
0185             for (int bin = 1; bin <= itHHH->second->getTH1()->GetNbinsX(); bin++) {
0186               if (itHHH->second->getTH1()->GetBinError(bin) != 0 || itHHH->second->getTH1()->GetBinContent(bin) != 0) {
0187                 if (itHHH->first == "Lumibased BeamSpotFit" || itHHH->first == "Lumibased PrimaryVertex" ||
0188                     itHHH->first == "Lumibased DataBase" || itHHH->first == "Lumibased Scalers") {
0189                   if (min > itHHH->second->getTH1()->GetBinContent(bin)) {
0190                     min = itHHH->second->getTH1()->GetBinContent(bin);
0191                   }
0192                   if (max < itHHH->second->getTH1()->GetBinContent(bin)) {
0193                     max = itHHH->second->getTH1()->GetBinContent(bin);
0194                   }
0195                 } else if (itHHH->first == "Lumibased PrimaryVertex-DataBase fit" ||
0196                            itHHH->first == "Lumibased PrimaryVertex-Scalers fit" ||
0197                            itHHH->first == "Lumibased Scalers-DataBase fit" ||
0198                            itHHH->first == "Lumibased PrimaryVertex-DataBase" ||
0199                            itHHH->first == "Lumibased PrimaryVertex-Scalers") {
0200                   if (minDelta > itHHH->second->getTH1()->GetBinContent(bin)) {
0201                     minDelta = itHHH->second->getTH1()->GetBinContent(bin);
0202                   }
0203                   if (maxDelta < itHHH->second->getTH1()->GetBinContent(bin)) {
0204                     maxDelta = itHHH->second->getTH1()->GetBinContent(bin);
0205                   }
0206                 } else {
0207                   LogInfo("AlcaBeamMonitorClient") << "The histosMap_ have a histogram named " << itHHH->first
0208                                                    << " that I can't recognize in this loop!";
0209                   // assert(0);
0210                 }
0211               }
0212             }
0213           }
0214         }
0215         for (map<string, MonitorElement*>::iterator itHHH = itHH->second.begin(); itHHH != itHH->second.end();
0216              itHHH++) {
0217           if (itHHH->second != nullptr) {
0218             if (itHHH->first == "Lumibased BeamSpotFit" || itHHH->first == "Lumibased PrimaryVertex" ||
0219                 itHHH->first == "Lumibased DataBase" || itHHH->first == "Lumibased Scalers") {
0220               if ((max == -bigNumber && min == bigNumber) || max - min == 0) {
0221                 itHHH->second->getTH1()->SetMinimum(itHHH->second->getTH1()->GetMinimum() - 0.01);
0222                 itHHH->second->getTH1()->SetMaximum(itHHH->second->getTH1()->GetMaximum() + 0.01);
0223               } else {
0224                 itHHH->second->getTH1()->SetMinimum(min - 0.1 * (max - min));
0225                 itHHH->second->getTH1()->SetMaximum(max + 0.1 * (max - min));
0226               }
0227             } else if (itHHH->first == "Lumibased PrimaryVertex-DataBase fit" ||
0228                        itHHH->first == "Lumibased PrimaryVertex-Scalers fit" ||
0229                        itHHH->first == "Lumibased Scalers-DataBase fit" ||
0230                        itHHH->first == "Lumibased PrimaryVertex-DataBase" ||
0231                        itHHH->first == "Lumibased PrimaryVertex-Scalers") {
0232               if ((maxDelta == -bigNumber && minDelta == bigNumber) || maxDelta - minDelta == 0) {
0233                 itHHH->second->getTH1()->SetMinimum(itHHH->second->getTH1()->GetMinimum() - 0.01);
0234                 itHHH->second->getTH1()->SetMaximum(itHHH->second->getTH1()->GetMaximum() + 0.01);
0235               } else {
0236                 itHHH->second->getTH1()->SetMinimum(minDelta - 2 * (maxDelta - minDelta));
0237                 itHHH->second->getTH1()->SetMaximum(maxDelta + 2 * (maxDelta - minDelta));
0238               }
0239             } else {
0240               LogInfo("AlcaBeamMonitorClient") << "The histosMap_ have a histogram named " << itHHH->first
0241                                                << " that I can't recognize in this loop!";
0242             }
0243           }
0244         }
0245       }
0246     }
0247   }
0248   /**/
0249 }
0250 
0251 DEFINE_FWK_MODULE(AlcaBeamMonitorClient);