Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:06:52

0001 /*
0002  * \file OnlineBeamMonitor.cc
0003  * \author Lorenzo Uplegger/FNAL
0004  * modified by Simone Gennai INFN/Bicocca
0005  */
0006 
0007 #include "FWCore/Framework/interface/MakerMacros.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/Framework/interface/ESHandle.h"
0010 #include "FWCore/ServiceRegistry/interface/Service.h"
0011 #include "DQM/BeamMonitor/plugins/OnlineBeamMonitor.h"
0012 #include "DataFormats/Common/interface/Handle.h"
0013 #include "DataFormats/Common/interface/View.h"
0014 #include "FWCore/Framework/interface/ConsumesCollector.h"
0015 #include "FWCore/Framework/interface/EventSetup.h"
0016 #include "FWCore/Framework/interface/LuminosityBlock.h"
0017 #include "FWCore/Framework/interface/Run.h"
0018 #include "RecoVertex/BeamSpotProducer/interface/BeamFitter.h"
0019 #include "RecoVertex/BeamSpotProducer/interface/PVFitter.h"
0020 #include <memory>
0021 
0022 #include <numeric>
0023 
0024 using namespace std;
0025 using namespace edm;
0026 using namespace reco;
0027 
0028 //----------------------------------------------------------------------------------------------------------------------
0029 OnlineBeamMonitor::OnlineBeamMonitor(const ParameterSet& ps)
0030     : monitorName_(ps.getUntrackedParameter<string>("MonitorName")),
0031       bsTransientToken_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
0032       bsHLTToken_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
0033       bsLegacyToken_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
0034       numberOfValuesToSave_(0),
0035       appendRunTxt_(ps.getUntrackedParameter<bool>("AppendRunToFileName")),
0036       writeDIPTxt_(ps.getUntrackedParameter<bool>("WriteDIPAscii")),
0037       outputDIPTxt_(ps.getUntrackedParameter<std::string>("DIPFileName")) {
0038   if (!monitorName_.empty())
0039     monitorName_ = monitorName_ + "/";
0040 
0041   processedLumis_.clear();
0042 
0043   varNamesV_.push_back("x");
0044   varNamesV_.push_back("y");
0045   varNamesV_.push_back("z");
0046   varNamesV_.push_back("sigmaX");
0047   varNamesV_.push_back("sigmaY");
0048   varNamesV_.push_back("sigmaZ");
0049 
0050   //histoByCategoryNames_.insert(pair<string, string>("run", "Coordinate"));
0051   //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex fit-DataBase"));
0052   //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex fit-BeamFit"));
0053   //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex fit-Scalers"));
0054   //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex-DataBase"));
0055   //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex-BeamFit"));
0056   //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex-Scalers"));
0057 
0058   histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased BeamSpotHLT"));
0059   histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased BeamSpotLegacy"));
0060   histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased BeamSpotTransient"));
0061 
0062   for (const auto& itV : varNamesV_) {
0063     for (const auto& itM : histoByCategoryNames_) {
0064       histosMap_[itV][itM.first][itM.second] = nullptr;
0065     }
0066   }
0067 }
0068 
0069 void OnlineBeamMonitor::fillDescriptions(edm::ConfigurationDescriptions& iDesc) {
0070   edm::ParameterSetDescription ps;
0071   ps.addUntracked<std::string>("MonitorName", "YourSubsystemName");
0072   ps.addUntracked<bool>("AppendRunToFileName", false);
0073   ps.addUntracked<bool>("WriteDIPAscii", true);
0074   ps.addUntracked<std::string>("DIPFileName", "BeamFitResultsForDIP.txt");
0075 
0076   iDesc.addWithDefaultLabel(ps);
0077 }
0078 
0079 //----------------------------------------------------------------------------------------------------------------------
0080 void OnlineBeamMonitor::bookHistograms(DQMStore::IBooker& ibooker,
0081                                        edm::Run const& iRun,
0082                                        edm::EventSetup const& iSetup) {
0083   string name;
0084   string title;
0085   int firstLumi = 1;
0086   int lastLumi = 3000;
0087   for (auto& itM : histosMap_) {
0088     //Making histos per Lumi
0089     // x,y,z,sigmaX,sigmaY,sigmaZ
0090     for (auto& itMM : itM.second) {
0091       if (itMM.first != "run") {
0092         for (auto& itMMM : itMM.second) {
0093           name = string("h") + itM.first + itMMM.first;
0094           title = itM.first + "_{0} " + itMMM.first;
0095           if (itMM.first == "lumi") {
0096             ibooker.setCurrentFolder(monitorName_ + "Debug");
0097             itMMM.second = ibooker.book1D(name, title, lastLumi - firstLumi + 1, firstLumi - 0.5, lastLumi + 0.5);
0098             itMMM.second->setEfficiencyFlag();
0099           } else {
0100             LogInfo("OnlineBeamMonitorClient") << "Unrecognized category " << itMM.first;
0101           }
0102           if (itMMM.second != nullptr) {
0103             if (itMMM.first.find('-') != string::npos) {
0104               itMMM.second->setAxisTitle(string("#Delta ") + itM.first + "_{0} (cm)", 2);
0105             } else {
0106               itMMM.second->setAxisTitle(itM.first + "_{0} (cm)", 2);
0107             }
0108             itMMM.second->setAxisTitle("Lumisection", 1);
0109           }
0110         }
0111       }
0112     }
0113   }
0114 
0115   // create and cd into new folder
0116   ibooker.setCurrentFolder(monitorName_ + "Validation");
0117   //Book histograms
0118   bsChoice_ = ibooker.bookProfile("bsChoice",
0119                                   "BS Choice: +1=HLT / -1=Legacy / -10=Fake (fallback to PCL) / 0=No Transient ",
0120                                   lastLumi - firstLumi + 1,
0121                                   firstLumi - 0.5,
0122                                   lastLumi + 0.5,
0123                                   100,
0124                                   -10,
0125                                   1,
0126                                   "");
0127   bsChoice_->setAxisTitle("Lumisection", 1);
0128   bsChoice_->setAxisTitle("Choice", 2);
0129 }
0130 
0131 //----------------------------------------------------------------------------------------------------------------------
0132 // Handle exceptions for the schema evolution of the BeamSpotOnline CondFormat
0133 
0134 // Slightly better error handler
0135 static void print_error(const std::exception& e) { edm::LogError("BeamSpotOnlineParameters") << e.what() << '\n'; }
0136 
0137 // Method to catch exceptions
0138 template <typename T, class Except, class Func, class Response>
0139 T try_(Func f, Response r) {
0140   try {
0141     LogDebug("BeamSpotOnlineParameters") << "I have tried" << std::endl;
0142     return f();
0143   } catch (Except& e) {
0144     LogDebug("BeamSpotOnlineParameters") << "I have caught!" << std::endl;
0145     r(e);
0146     return static_cast<T>("-999");
0147   }
0148 }
0149 
0150 // Enum the BS string parameters
0151 enum BSparameters {
0152   startTime = 0,  // 0 additional std::string parameters
0153   endTime = 1,    // 1
0154   lumiRange = 2,  // 2
0155   END_OF_TYPES = 3,
0156 };
0157 
0158 // Functor
0159 std::function<std::string(BSparameters, BeamSpotOnlineObjects)> myStringFunctor = [](BSparameters my_param,
0160                                                                                      BeamSpotOnlineObjects m_payload) {
0161   std::string ret("");
0162   switch (my_param) {
0163     case startTime:
0164       return m_payload.startTime();
0165     case endTime:
0166       return m_payload.endTime();
0167     case lumiRange:
0168       return m_payload.lumiRange();
0169     default:
0170       return ret;
0171   }
0172 };
0173 
0174 //----------------------------------------------------------------------------------------------------------------------
0175 std::shared_ptr<onlinebeammonitor::BeamSpotInfo> OnlineBeamMonitor::globalBeginLuminosityBlock(
0176     const LuminosityBlock& iLumi, const EventSetup& iSetup) const {
0177   // Always create a beamspot group for each lumi weather we have results or not! Each Beamspot will be of unknown type!
0178 
0179   auto beamSpotInfo = std::make_shared<onlinebeammonitor::BeamSpotInfo>();
0180 
0181   //Read BeamSpot from DB
0182   ESHandle<BeamSpotOnlineObjects> bsHLTHandle;
0183   ESHandle<BeamSpotOnlineObjects> bsLegacyHandle;
0184   ESHandle<BeamSpotObjects> bsTransientHandle;
0185   //int lastLumiHLT_ = 0;
0186   //int lastLumiLegacy_ = 0;
0187   std::string startTimeStamp_ = "0";
0188   std::string startTimeStampHLT_ = "0";
0189   std::string startTimeStampLegacy_ = "0";
0190   std::string stopTimeStamp_ = "0";
0191   std::string stopTimeStampHLT_ = "0";
0192   std::string stopTimeStampLegacy_ = "0";
0193   std::string lumiRange_ = "0 - 0";
0194   std::string lumiRangeHLT_ = "0 - 0";
0195   std::string lumiRangeLegacy_ = "0 - 0";
0196 
0197   if (auto bsHLTHandle = iSetup.getHandle(bsHLTToken_)) {
0198     auto const& spotDB = *bsHLTHandle;
0199 
0200     //lastLumiHLT_ = spotDB.lastAnalyzedLumi();
0201     startTimeStampHLT_ =
0202         try_<std::string, std::out_of_range>(std::bind(myStringFunctor, BSparameters::startTime, spotDB), print_error);
0203     stopTimeStampHLT_ =
0204         try_<std::string, std::out_of_range>(std::bind(myStringFunctor, BSparameters::endTime, spotDB), print_error);
0205     lumiRangeHLT_ =
0206         try_<std::string, std::out_of_range>(std::bind(myStringFunctor, BSparameters::lumiRange, spotDB), print_error);
0207 
0208     // translate from BeamSpotObjects to reco::BeamSpot
0209     BeamSpot::Point apoint(spotDB.x(), spotDB.y(), spotDB.z());
0210 
0211     BeamSpot::CovarianceMatrix matrix;
0212     for (int i = 0; i < 7; ++i) {
0213       for (int j = 0; j < 7; ++j) {
0214         matrix(i, j) = spotDB.covariance(i, j);
0215       }
0216     }
0217 
0218     beamSpotInfo->beamSpotsMap_["HLT"] =
0219         BeamSpot(apoint, spotDB.sigmaZ(), spotDB.dxdz(), spotDB.dydz(), spotDB.beamWidthX(), matrix);
0220 
0221     BeamSpot* aSpot = &(beamSpotInfo->beamSpotsMap_["HLT"]);
0222 
0223     aSpot->setBeamWidthY(spotDB.beamWidthY());
0224     aSpot->setEmittanceX(spotDB.emittanceX());
0225     aSpot->setEmittanceY(spotDB.emittanceY());
0226     aSpot->setbetaStar(spotDB.betaStar());
0227 
0228     if (spotDB.beamType() == 2) {
0229       aSpot->setType(reco::BeamSpot::Tracker);
0230     } else {
0231       aSpot->setType(reco::BeamSpot::Fake);
0232     }
0233     //LogInfo("OnlineBeamMonitor")
0234     //  << *aSpot << std::endl;
0235   } else {
0236     LogInfo("OnlineBeamMonitor") << "Database BeamSpot is not valid at lumi: " << iLumi.id().luminosityBlock();
0237   }
0238   if (auto bsLegacyHandle = iSetup.getHandle(bsLegacyToken_)) {
0239     auto const& spotDB = *bsLegacyHandle;
0240 
0241     // translate from BeamSpotObjects to reco::BeamSpot
0242     BeamSpot::Point apoint(spotDB.x(), spotDB.y(), spotDB.z());
0243 
0244     //lastLumiLegacy_ = spotDB.lastAnalyzedLumi();
0245     startTimeStampLegacy_ =
0246         try_<std::string, std::out_of_range>(std::bind(myStringFunctor, BSparameters::startTime, spotDB), print_error);
0247     stopTimeStampLegacy_ =
0248         try_<std::string, std::out_of_range>(std::bind(myStringFunctor, BSparameters::endTime, spotDB), print_error);
0249     lumiRangeLegacy_ =
0250         try_<std::string, std::out_of_range>(std::bind(myStringFunctor, BSparameters::lumiRange, spotDB), print_error);
0251 
0252     BeamSpot::CovarianceMatrix matrix;
0253     for (int i = 0; i < 7; ++i) {
0254       for (int j = 0; j < 7; ++j) {
0255         matrix(i, j) = spotDB.covariance(i, j);
0256       }
0257     }
0258 
0259     beamSpotInfo->beamSpotsMap_["Legacy"] =
0260         BeamSpot(apoint, spotDB.sigmaZ(), spotDB.dxdz(), spotDB.dydz(), spotDB.beamWidthX(), matrix);
0261 
0262     BeamSpot* aSpot = &(beamSpotInfo->beamSpotsMap_["Legacy"]);
0263 
0264     aSpot->setBeamWidthY(spotDB.beamWidthY());
0265     aSpot->setEmittanceX(spotDB.emittanceX());
0266     aSpot->setEmittanceY(spotDB.emittanceY());
0267     aSpot->setbetaStar(spotDB.betaStar());
0268 
0269     if (spotDB.beamType() == 2) {
0270       aSpot->setType(reco::BeamSpot::Tracker);
0271     } else {
0272       aSpot->setType(reco::BeamSpot::Fake);
0273     }
0274     //LogInfo("OnlineBeamMonitor")
0275     //  << *aSpot << std::endl;
0276   } else {
0277     LogInfo("OnlineBeamMonitor") << "Database BeamSpot is not valid at lumi: " << iLumi.id().luminosityBlock();
0278   }
0279   if (auto bsTransientHandle = iSetup.getHandle(bsTransientToken_)) {
0280     auto const& spotDB = *bsTransientHandle;
0281     //std::cout << " from the DB " << spotDB << std::endl;
0282 
0283     // translate from BeamSpotObjects to reco::BeamSpot
0284     BeamSpot::Point apoint(spotDB.x(), spotDB.y(), spotDB.z());
0285 
0286     BeamSpot::CovarianceMatrix matrix;
0287     for (int i = 0; i < 7; ++i) {
0288       for (int j = 0; j < 7; ++j) {
0289         matrix(i, j) = spotDB.covariance(i, j);
0290       }
0291     }
0292 
0293     beamSpotInfo->beamSpotsMap_["Transient"] =
0294         BeamSpot(apoint, spotDB.sigmaZ(), spotDB.dxdz(), spotDB.dydz(), spotDB.beamWidthX(), matrix);
0295 
0296     BeamSpot* aSpot = &(beamSpotInfo->beamSpotsMap_["Transient"]);
0297 
0298     aSpot->setBeamWidthY(spotDB.beamWidthY());
0299     aSpot->setEmittanceX(spotDB.emittanceX());
0300     aSpot->setEmittanceY(spotDB.emittanceY());
0301     aSpot->setbetaStar(spotDB.betaStar());
0302     if (spotDB.beamType() == 2) {
0303       aSpot->setType(reco::BeamSpot::Tracker);
0304     } else {
0305       aSpot->setType(reco::BeamSpot::Fake);
0306     }
0307 
0308     if (writeDIPTxt_) {
0309       std::ofstream outFile;
0310 
0311       std::string tmpname = outputDIPTxt_;
0312       int frun = iLumi.getRun().run();
0313 
0314       char index[15];
0315       if (appendRunTxt_ && writeDIPTxt_) {
0316         sprintf(index, "%s%i", "_Run", frun);
0317         tmpname.insert(outputDIPTxt_.length() - 4, index);
0318       }
0319       //int lastLumiAnalyzed_ = iLumi.id().luminosityBlock();
0320 
0321       if (beamSpotInfo->beamSpotsMap_.find("Transient") != beamSpotInfo->beamSpotsMap_.end()) {
0322         if (beamSpotInfo->beamSpotsMap_.find("HLT") != beamSpotInfo->beamSpotsMap_.end() &&
0323             beamSpotInfo->beamSpotsMap_["Transient"].x0() == beamSpotInfo->beamSpotsMap_["HLT"].x0()) {
0324           // lastLumiAnalyzed_ = lastLumiHLT_;
0325           startTimeStamp_ = startTimeStampHLT_;
0326           stopTimeStamp_ = stopTimeStampHLT_;
0327           lumiRange_ = lumiRangeHLT_;
0328 
0329         } else if (beamSpotInfo->beamSpotsMap_.find("Legacy") != beamSpotInfo->beamSpotsMap_.end() &&
0330                    beamSpotInfo->beamSpotsMap_["Transient"].x0() == beamSpotInfo->beamSpotsMap_["Legacy"].x0()) {
0331           //lastLumiAnalyzed_ = lastLumiLegacy_;
0332           startTimeStamp_ = startTimeStampLegacy_;
0333           stopTimeStamp_ = stopTimeStampLegacy_;
0334           lumiRange_ = lumiRangeLegacy_;
0335         }
0336       }
0337 
0338       outFile.open(tmpname.c_str());
0339 
0340       outFile << "Runnumber " << frun << " bx " << 0 << std::endl;
0341       outFile << "BeginTimeOfFit " << startTimeStamp_ << " " << 0 << std::endl;
0342       outFile << "EndTimeOfFit " << stopTimeStamp_ << " " << 0 << std::endl;
0343       //outFile << "LumiRange " << lumiRange_ << " - " << lastLumiAnalyzed_ << std::endl;
0344       outFile << "LumiRange " << lumiRange_ << std::endl;
0345       outFile << "Type " << aSpot->type() << std::endl;
0346       outFile << "X0 " << aSpot->x0() << std::endl;
0347       outFile << "Y0 " << aSpot->y0() << std::endl;
0348       outFile << "Z0 " << aSpot->z0() << std::endl;
0349       outFile << "sigmaZ0 " << aSpot->sigmaZ() << std::endl;
0350       outFile << "dxdz " << aSpot->dxdz() << std::endl;
0351       outFile << "dydz " << aSpot->dydz() << std::endl;
0352       outFile << "BeamWidthX " << aSpot->BeamWidthX() << std::endl;
0353       outFile << "BeamWidthY " << aSpot->BeamWidthY() << std::endl;
0354       for (int i = 0; i < 6; ++i) {
0355         outFile << "Cov(" << i << ",j) ";
0356         for (int j = 0; j < 7; ++j) {
0357           outFile << aSpot->covariance(i, j) << " ";
0358         }
0359         outFile << std::endl;
0360       }
0361       outFile << "Cov(6,j) 0 0 0 0 0 0 " << aSpot->covariance(6, 6) << std::endl;
0362       outFile << "EmittanceX " << aSpot->emittanceX() << std::endl;
0363       outFile << "EmittanceY " << aSpot->emittanceY() << std::endl;
0364       outFile << "BetaStar " << aSpot->betaStar() << std::endl;
0365 
0366       outFile.close();
0367     }
0368     //LogInfo("OnlineBeamMonitor")
0369     //  << *aSpot << std::endl;
0370   } else {
0371     LogInfo("OnlineBeamMonitor") << "Database BeamSpot is not valid at lumi: " << iLumi.id().luminosityBlock();
0372   }
0373   return beamSpotInfo;
0374 }
0375 
0376 //----------------------------------------------------------------------------------------------------------------------
0377 void OnlineBeamMonitor::globalEndLuminosityBlock(const LuminosityBlock& iLumi, const EventSetup& iSetup) {
0378   processedLumis_.push_back(iLumi.id().luminosityBlock());
0379   auto beamSpotInfo = luminosityBlockCache(iLumi.index());
0380 
0381   //Setting up the choice
0382   if (beamSpotInfo->beamSpotsMap_.find("Transient") != beamSpotInfo->beamSpotsMap_.end()) {
0383     if (beamSpotInfo->beamSpotsMap_.find("HLT") != beamSpotInfo->beamSpotsMap_.end() &&
0384         beamSpotInfo->beamSpotsMap_["Transient"].x0() == beamSpotInfo->beamSpotsMap_["HLT"].x0()) {
0385       bsChoice_->Fill(iLumi.id().luminosityBlock(), 1);
0386       bsChoice_->setBinError(iLumi.id().luminosityBlock(), 0.05);
0387     } else if (beamSpotInfo->beamSpotsMap_.find("Legacy") != beamSpotInfo->beamSpotsMap_.end() &&
0388                beamSpotInfo->beamSpotsMap_["Transient"].x0() == beamSpotInfo->beamSpotsMap_["Legacy"].x0()) {
0389       bsChoice_->Fill(iLumi.id().luminosityBlock(), -1);
0390       bsChoice_->setBinError(iLumi.id().luminosityBlock(), 0.05);
0391     } else {
0392       bsChoice_->Fill(iLumi.id().luminosityBlock(), -10);
0393       bsChoice_->setBinError(iLumi.id().luminosityBlock(), 0.05);
0394     }
0395   } else {
0396     bsChoice_->Fill(iLumi.id().luminosityBlock(), 0);
0397     bsChoice_->setBinError(iLumi.id().luminosityBlock(), 0.05);
0398   }
0399 
0400   //    "PV,BF..."      Value,Error
0401   map<std::string, pair<double, double> > resultsMap;
0402   vector<pair<double, double> > vertexResults;
0403   MonitorElement* histo = nullptr;
0404   for (const auto& itV : varNamesV_) {
0405     resultsMap.clear();
0406     for (const auto& itBS : beamSpotInfo->beamSpotsMap_) {
0407       if (itBS.second.type() == BeamSpot::Tracker) {
0408         if (itV == "x") {
0409           resultsMap[itBS.first] = pair<double, double>(itBS.second.x0(), itBS.second.x0Error());
0410         } else if (itV == "y") {
0411           resultsMap[itBS.first] = pair<double, double>(itBS.second.y0(), itBS.second.y0Error());
0412         } else if (itV == "z") {
0413           resultsMap[itBS.first] = pair<double, double>(itBS.second.z0(), itBS.second.z0Error());
0414         } else if (itV == "sigmaX") {
0415           resultsMap[itBS.first] = pair<double, double>(itBS.second.BeamWidthX(), itBS.second.BeamWidthXError());
0416         } else if (itV == "sigmaY") {
0417           resultsMap[itBS.first] = pair<double, double>(itBS.second.BeamWidthY(), itBS.second.BeamWidthYError());
0418         } else if (itV == "sigmaZ") {
0419           resultsMap[itBS.first] = pair<double, double>(itBS.second.sigmaZ(), itBS.second.sigmaZ0Error());
0420         } else {
0421           LogInfo("OnlineBeamMonitor") << "The histosMap_ has been built with the name " << itV
0422                                        << " that I can't recognize!";
0423         }
0424       }
0425     }
0426 
0427     for (const auto& itM : histoByCategoryNames_) {
0428       if ((histo = histosMap_[itV][itM.first][itM.second]) == nullptr)
0429         continue;
0430       if (itM.second == "Lumibased BeamSpotHLT") {
0431         if (resultsMap.find("HLT") != resultsMap.end()) {
0432           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["HLT"].first);
0433           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["HLT"].second);
0434         }
0435       } else if (itM.second == "Lumibased BeamSpotLegacy") {
0436         if (resultsMap.find("Legacy") != resultsMap.end()) {
0437           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["Legacy"].first);
0438           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["Legacy"].second);
0439         }
0440       } else if (itM.second == "Lumibased BeamSpotTransient") {
0441         if (resultsMap.find("Transient") != resultsMap.end()) {
0442           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["Transient"].first);
0443           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["Transient"].second);
0444         }
0445       } else {
0446         LogInfo("OnlineBeamMonitor") << "The histosMap_ have a histogram named " << itM.second
0447                                      << " that I can't recognize in this loop!";
0448       }
0449     }
0450   }
0451 }
0452 
0453 void OnlineBeamMonitor::dqmEndRun(edm::Run const&, edm::EventSetup const&) {
0454   if (processedLumis_.empty()) {
0455     return;
0456   }
0457 
0458   const double bigNumber = 1000000.;
0459   std::sort(processedLumis_.begin(), processedLumis_.end());
0460   int firstLumi = *processedLumis_.begin();
0461   int lastLumi = *(--processedLumis_.end());
0462   bsChoice_->getTH1()->GetXaxis()->SetRangeUser(firstLumi - 0.5, lastLumi + 0.5);
0463   for (auto& itH : histosMap_) {
0464     for (auto& itHH : itH.second) {
0465       double min = bigNumber;
0466       double max = -bigNumber;
0467       if (itHH.first != "run") {
0468         for (auto& itHHH : itHH.second) {
0469           if (itHHH.second != nullptr) {
0470             for (int bin = 1; bin <= itHHH.second->getTH1()->GetNbinsX(); bin++) {
0471               if (itHHH.second->getTH1()->GetBinError(bin) != 0 || itHHH.second->getTH1()->GetBinContent(bin) != 0) {
0472                 if (itHHH.first == "Lumibased BeamSpotHLT" || itHHH.first == "Lumibased BeamSpotLegacy" ||
0473                     itHHH.first == "Lumibased BeamSpotTransient") {
0474                   if (min > itHHH.second->getTH1()->GetBinContent(bin)) {
0475                     min = itHHH.second->getTH1()->GetBinContent(bin);
0476                   }
0477                   if (max < itHHH.second->getTH1()->GetBinContent(bin)) {
0478                     max = itHHH.second->getTH1()->GetBinContent(bin);
0479                   }
0480                 } else {
0481                   LogInfo("OnlineBeamMonitorClient") << "The histosMap_ have a histogram named " << itHHH.first
0482                                                      << " that I can't recognize in this loop!";
0483                 }
0484               }
0485             }
0486           }
0487         }
0488         for (auto& itHHH : itHH.second) {
0489           if (itHHH.second != nullptr) {
0490             if (itHHH.first == "Lumibased BeamSpotHLT" || itHHH.first == "Lumibased BeamSpotLegacy" ||
0491                 itHHH.first == "Lumibased BeamSpotTransient") {
0492               if ((max == -bigNumber && min == bigNumber) || max - min == 0) {
0493                 itHHH.second->getTH1()->SetMinimum(itHHH.second->getTH1()->GetMinimum() - 0.01);
0494                 itHHH.second->getTH1()->SetMaximum(itHHH.second->getTH1()->GetMaximum() + 0.01);
0495               } else {
0496                 itHHH.second->getTH1()->SetMinimum(min - 0.1 * (max - min));
0497                 itHHH.second->getTH1()->SetMaximum(max + 0.1 * (max - min));
0498               }
0499             } else {
0500               LogInfo("OnlineBeamMonitorClient")
0501                   << "The histosMap_ have a histogram named " << itHHH.first << " that I can't recognize in this loop!";
0502             }
0503             itHHH.second->getTH1()->GetXaxis()->SetRangeUser(firstLumi - 0.5, lastLumi + 0.5);
0504           }
0505         }
0506       }
0507     }
0508   }
0509 }
0510 
0511 DEFINE_FWK_MODULE(OnlineBeamMonitor);