Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-22 05:03:06

0001 /*
0002  * \file AlcaBeamMonitor.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 "DataFormats/BeamSpot/interface/BeamSpot.h"
0012 //#include "DataFormats/Scalers/interface/BeamSpotOnline.h"
0013 #include "DQM/BeamMonitor/plugins/AlcaBeamMonitor.h"
0014 #include "DataFormats/Common/interface/Handle.h"
0015 #include "DataFormats/Common/interface/View.h"
0016 #include "FWCore/Framework/interface/ConsumesCollector.h"
0017 #include "FWCore/Framework/interface/EventSetup.h"
0018 #include "FWCore/Framework/interface/LuminosityBlock.h"
0019 #include "FWCore/Framework/interface/Run.h"
0020 #include "RecoVertex/BeamSpotProducer/interface/BeamFitter.h"
0021 #include "RecoVertex/BeamSpotProducer/interface/PVFitter.h"
0022 #include <memory>
0023 
0024 #include <numeric>
0025 
0026 using namespace std;
0027 using namespace edm;
0028 using namespace reco;
0029 
0030 //----------------------------------------------------------------------------------------------------------------------
0031 AlcaBeamMonitor::AlcaBeamMonitor(const ParameterSet& ps)
0032     : monitorName_(ps.getUntrackedParameter<string>("MonitorName")),
0033       primaryVertexLabel_(consumes<VertexCollection>(ps.getUntrackedParameter<InputTag>("PrimaryVertexLabel"))),
0034       trackLabel_(consumes<reco::TrackCollection>(ps.getUntrackedParameter<InputTag>("TrackLabel"))),
0035       scalerLabel_(consumes<BeamSpot>(ps.getUntrackedParameter<InputTag>("ScalerLabel"))),
0036       beamSpotToken_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
0037       perLSsaving_(ps.getUntrackedParameter<bool>("perLSsaving", false)),
0038       numberOfValuesToSave_(0) {
0039   if (!monitorName_.empty())
0040     monitorName_ = monitorName_ + "/";
0041 
0042   theBeamFitter_ = std::make_unique<BeamFitter>(ps, consumesCollector());
0043   theBeamFitter_->resetTrkVector();
0044   theBeamFitter_->resetLSRange();
0045   theBeamFitter_->resetRefTime();
0046   theBeamFitter_->resetPVFitter();
0047 
0048   thePVFitter_ = std::make_unique<PVFitter>(ps, consumesCollector());
0049 
0050   processedLumis_.clear();
0051 
0052   varNamesV_.push_back("x");
0053   varNamesV_.push_back("y");
0054   varNamesV_.push_back("z");
0055   varNamesV_.push_back("sigmaX");
0056   varNamesV_.push_back("sigmaY");
0057   varNamesV_.push_back("sigmaZ");
0058 
0059   if (!perLSsaving_) {
0060     histoByCategoryNames_.insert(pair<string, string>("run", "Coordinate"));
0061     histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex fit-DataBase"));
0062     histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex fit-BeamFit"));
0063     histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex fit-Online"));
0064     histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex-DataBase"));
0065     histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex-BeamFit"));
0066     histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex-Online"));
0067 
0068     histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased BeamSpotFit"));
0069     histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased PrimaryVertex"));
0070     histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased DataBase"));
0071     histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased Online"));
0072     histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased PrimaryVertex-DataBase fit"));
0073     histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased PrimaryVertex-Online fit"));
0074     histoByCategoryNames_.insert(pair<string, string>("validation", "Lumibased Online-DataBase fit"));
0075     histoByCategoryNames_.insert(pair<string, string>("validation", "Lumibased PrimaryVertex-DataBase"));
0076     histoByCategoryNames_.insert(pair<string, string>("validation", "Lumibased PrimaryVertex-Online"));
0077   }
0078 
0079   for (vector<string>::iterator itV = varNamesV_.begin(); itV != varNamesV_.end(); itV++) {
0080     for (multimap<string, string>::iterator itM = histoByCategoryNames_.begin(); itM != histoByCategoryNames_.end();
0081          itM++) {
0082       histosMap_[*itV][itM->first][itM->second] = nullptr;
0083     }
0084   }
0085 }
0086 
0087 void AlcaBeamMonitor::fillDescriptions(edm::ConfigurationDescriptions& iDesc) {
0088   edm::ParameterSetDescription ps;
0089 
0090   ps.addUntracked<std::string>("MonitorName", "YourSubsystemName");
0091   ps.addUntracked<edm::InputTag>("PrimaryVertexLabel");
0092   ps.addUntracked<edm::InputTag>("TrackLabel");
0093   ps.addUntracked<edm::InputTag>("ScalerLabel");
0094   ps.addUntracked<bool>("perLSsaving");
0095 
0096   BeamFitter::fillDescription(ps);
0097   PVFitter::fillDescription(ps);
0098 
0099   iDesc.addDefault(ps);
0100 }
0101 
0102 //----------------------------------------------------------------------------------------------------------------------
0103 void AlcaBeamMonitor::bookHistograms(DQMStore::IBooker& ibooker, edm::Run const& iRun, edm::EventSetup const& iSetup) {
0104   string name;
0105   string title;
0106   int firstLumi = 1;
0107   int lastLumi = 3000;
0108   for (HistosContainer::iterator itM = histosMap_.begin(); itM != histosMap_.end(); itM++) {
0109     ibooker.setCurrentFolder(monitorName_ + "Debug");
0110     for (map<string, MonitorElement*>::iterator itMM = itM->second["run"].begin(); itMM != itM->second["run"].end();
0111          itMM++) {
0112       name = string("h") + itM->first + itMM->first;
0113       title = itM->first + "_{0} " + itMM->first;
0114       if (itM->first == "x" || itM->first == "y") {
0115         if (itMM->first == "Coordinate") {
0116           itMM->second = ibooker.book1D(name, title, 1001, -0.2525, 0.2525);
0117         } else if (itMM->first == "PrimaryVertex fit-DataBase" || itMM->first == "PrimaryVertex fit-BeamFit" ||
0118                    itMM->first == "PrimaryVertex fit-Online" || itMM->first == "PrimaryVertex-DataBase" ||
0119                    itMM->first == "PrimaryVertex-BeamFit" || itMM->first == "PrimaryVertex-Online") {
0120           itMM->second = ibooker.book1D(name, title, 1001, -0.02525, 0.02525);
0121         } else {
0122           //assert(0);
0123         }
0124       } else if (itM->first == "z") {
0125         if (itMM->first == "Coordinate") {
0126           itMM->second = ibooker.book1D(name, title, 101, -5.05, 5.05);
0127         } else if (itMM->first == "PrimaryVertex fit-DataBase" || itMM->first == "PrimaryVertex fit-BeamFit" ||
0128                    itMM->first == "PrimaryVertex fit-Online") {
0129           itMM->second = ibooker.book1D(name, title, 101, -0.505, 0.505);
0130         } else if (itMM->first == "PrimaryVertex-DataBase" || itMM->first == "PrimaryVertex-BeamFit" ||
0131                    itMM->first == "PrimaryVertex-Online") {
0132           itMM->second = ibooker.book1D(name, title, 1001, -5.005, 5.005);
0133         } else {
0134           //assert(0);
0135         }
0136       } else if (itM->first == "sigmaX" || itM->first == "sigmaY") {
0137         if (itMM->first == "Coordinate") {
0138           itMM->second = ibooker.book1D(name, title, 100, 0, 0.015);
0139         } else if (itMM->first == "PrimaryVertex fit-DataBase" || itMM->first == "PrimaryVertex fit-BeamFit" ||
0140                    itMM->first == "PrimaryVertex fit-Online" || itMM->first == "PrimaryVertex-DataBase" ||
0141                    itMM->first == "PrimaryVertex-BeamFit" || itMM->first == "PrimaryVertex-Online") {
0142           itMM->second = nullptr;
0143         } else {
0144           //assert(0);
0145         }
0146       } else if (itM->first == "sigmaZ") {
0147         if (itMM->first == "Coordinate") {
0148           itMM->second = ibooker.book1D(name, title, 110, 0, 11);
0149         } else if (itMM->first == "PrimaryVertex fit-DataBase" || itMM->first == "PrimaryVertex fit-BeamFit" ||
0150                    itMM->first == "PrimaryVertex fit-Online" || itMM->first == "PrimaryVertex-DataBase" ||
0151                    itMM->first == "PrimaryVertex-BeamFit" || itMM->first == "PrimaryVertex-Online") {
0152           itMM->second = ibooker.book1D(name, title, 101, -5.05, 5.05);
0153         } else {
0154           //assert(0);
0155         }
0156       } else {
0157         //assert(0);
0158       }
0159       if (itMM->second != nullptr) {
0160         if (itMM->first == "Coordinate") {
0161           itMM->second->setAxisTitle(itM->first + "_{0} (cm)", 1);
0162         } else if (itMM->first == "PrimaryVertex fit-DataBase" || itMM->first == "PrimaryVertex fit-BeamFit" ||
0163                    itMM->first == "PrimaryVertex fit-Online" || itMM->first == "PrimaryVertex-DataBase" ||
0164                    itMM->first == "PrimaryVertex-BeamFit" || itMM->first == "PrimaryVertex-Online") {
0165           itMM->second->setAxisTitle(itMM->first + " " + itM->first + "_{0} (cm)", 1);
0166         }
0167         itMM->second->setAxisTitle("Entries", 2);
0168       }
0169     }
0170 
0171     //Making histos per Lumi
0172     // x,y,z,sigmaX,sigmaY,sigmaZ
0173     for (map<string, map<string, MonitorElement*> >::iterator itMM = itM->second.begin(); itMM != itM->second.end();
0174          itMM++) {
0175       if (itMM->first != "run") {
0176         for (map<string, MonitorElement*>::iterator itMMM = itMM->second.begin(); itMMM != itMM->second.end();
0177              itMMM++) {
0178           name = string("h") + itM->first + itMMM->first;
0179           title = itM->first + "_{0} " + itMMM->first;
0180           if (itMM->first == "lumi") {
0181             ibooker.setCurrentFolder(monitorName_ + "Debug");
0182             itMMM->second = ibooker.book1D(name, title, lastLumi - firstLumi + 1, firstLumi - 0.5, lastLumi + 0.5);
0183             itMMM->second->setEfficiencyFlag();
0184           } else if (itMM->first == "validation" && itMMM->first == "Lumibased Online-DataBase fit") {
0185             ibooker.setCurrentFolder(monitorName_ + "Validation");
0186             itMMM->second = ibooker.book1D(name, title, lastLumi - firstLumi + 1, firstLumi - 0.5, lastLumi + 0.5);
0187             itMMM->second->setEfficiencyFlag();
0188           } else if (itMM->first == "validation" && itMMM->first != "Lumibased Online-DataBase fit" &&
0189                      (itM->first == "x" || itM->first == "y" || itM->first == "z")) {
0190             ibooker.setCurrentFolder(monitorName_ + "Validation");
0191             itMMM->second = ibooker.book1D(name, title, lastLumi - firstLumi + 1, firstLumi - 0.5, lastLumi + 0.5);
0192             itMMM->second->setEfficiencyFlag();
0193           } else if (itMM->first == "validation" &&
0194                      (itM->first == "sigmaX" || itM->first == "sigmaY" || itM->first == "sigmaZ")) {
0195             ibooker.setCurrentFolder(monitorName_ + "Validation");
0196             itMMM->second = nullptr;
0197           } else {
0198             LogInfo("AlcaBeamMonitorClient") << "Unrecognized category " << itMM->first;
0199             // assert(0);
0200           }
0201           if (itMMM->second != nullptr) {
0202             if (itMMM->first.find('-') != string::npos) {
0203               itMMM->second->setAxisTitle(string("#Delta ") + itM->first + "_{0} (cm)", 2);
0204             } else {
0205               itMMM->second->setAxisTitle(itM->first + "_{0} (cm)", 2);
0206             }
0207             itMMM->second->setAxisTitle("Lumisection", 1);
0208           }
0209         }
0210       }
0211     }
0212   }
0213 
0214   // create and cd into new folder
0215   ibooker.setCurrentFolder(monitorName_ + "Validation");
0216   //Book histograms
0217   hD0Phi0_ = ibooker.bookProfile("hD0Phi0", "d_{0} vs. #phi_{0} (All Tracks)", 63, -3.15, 3.15, 100, -0.5, 0.5, "");
0218   hD0Phi0_->setAxisTitle("#phi_{0} (rad)", 1);
0219   hD0Phi0_->setAxisTitle("d_{0} (cm)", 2);
0220 
0221   ibooker.setCurrentFolder(monitorName_ + "Debug");
0222   hDxyBS_ = ibooker.book1D("hDxyBS", "dxy_{0} w.r.t. Beam spot (All Tracks)", 100, -0.1, 0.1);
0223   hDxyBS_->setAxisTitle("dxy_{0} w.r.t. Beam spot (cm)", 1);
0224 }
0225 
0226 //----------------------------------------------------------------------------------------------------------------------
0227 std::shared_ptr<alcabeammonitor::BeamSpotInfo> AlcaBeamMonitor::globalBeginLuminosityBlock(
0228     const LuminosityBlock& iLumi, const EventSetup& iSetup) const {
0229   // Always create a beamspot group for each lumi weather we have results or not! Each Beamspot will be of unknown type!
0230 
0231   auto beamSpotInfo = std::make_shared<alcabeammonitor::BeamSpotInfo>();
0232 
0233   //Read BeamSpot from DB
0234   ESHandle<BeamSpotObjects> bsDBHandle;
0235   try {
0236     bsDBHandle = iSetup.getHandle(beamSpotToken_);
0237   } catch (cms::Exception& exception) {
0238     LogError("AlcaBeamMonitor") << exception.what();
0239     return nullptr;
0240   }
0241   if (bsDBHandle.isValid()) {  // check the product
0242     const BeamSpotObjects* spotDB = bsDBHandle.product();
0243 
0244     // translate from BeamSpotObjects to reco::BeamSpot
0245     BeamSpot::Point apoint(spotDB->x(), spotDB->y(), spotDB->z());
0246 
0247     BeamSpot::CovarianceMatrix matrix;
0248     for (int i = 0; i < reco::BeamSpot::dimension; ++i) {
0249       for (int j = 0; j < reco::BeamSpot::dimension; ++j) {
0250         matrix(i, j) = spotDB->covariance(i, j);
0251       }
0252     }
0253 
0254     beamSpotInfo->beamSpotMap_["DB"] =
0255         BeamSpot(apoint, spotDB->sigmaZ(), spotDB->dxdz(), spotDB->dydz(), spotDB->beamWidthX(), matrix);
0256 
0257     BeamSpot* aSpot = &(beamSpotInfo->beamSpotMap_["DB"]);
0258 
0259     aSpot->setBeamWidthY(spotDB->beamWidthY());
0260     aSpot->setEmittanceX(spotDB->emittanceX());
0261     aSpot->setEmittanceY(spotDB->emittanceY());
0262     aSpot->setbetaStar(spotDB->betaStar());
0263 
0264     if (spotDB->beamType() == 2) {
0265       aSpot->setType(reco::BeamSpot::Tracker);
0266     } else {
0267       aSpot->setType(reco::BeamSpot::Fake);
0268     }
0269     //LogInfo("AlcaBeamMonitor")
0270     //  << *aSpot << std::endl;
0271   } else {
0272     LogInfo("AlcaBeamMonitor") << "Database BeamSpot is not valid at lumi: " << iLumi.id().luminosityBlock();
0273   }
0274   return beamSpotInfo;
0275 }
0276 
0277 //----------------------------------------------------------------------------------------------------------------------
0278 void AlcaBeamMonitor::analyze(const Event& iEvent, const EventSetup& iSetup) {
0279   //------ BeamFitter
0280   theBeamFitter_->readEvent(iEvent);
0281   //------ PVFitter
0282   thePVFitter_->readEvent(iEvent);
0283 
0284   auto beamSpotInfo = luminosityBlockCache(iEvent.getLuminosityBlock().index());
0285   if (beamSpotInfo->beamSpotMap_.find("DB") != beamSpotInfo->beamSpotMap_.end()) {
0286     //------ Tracks
0287     Handle<reco::TrackCollection> TrackCollection;
0288     iEvent.getByToken(trackLabel_, TrackCollection);
0289     const reco::TrackCollection* tracks = TrackCollection.product();
0290     for (reco::TrackCollection::const_iterator track = tracks->begin(); track != tracks->end(); ++track) {
0291       hD0Phi0_->Fill(track->phi(), -1 * track->dxy());
0292       hDxyBS_->Fill(-1 * track->dxy(beamSpotInfo->beamSpotMap_["DB"].position()));
0293     }
0294   }
0295 
0296   //------ Primary Vertices
0297   Handle<VertexCollection> PVCollection;
0298   if (iEvent.getByToken(primaryVertexLabel_, PVCollection)) {
0299     std::vector<alcabeammonitor::pvPosAndErr> vertices;
0300     vertices.reserve(PVCollection->size());
0301     for (const auto& pv : *PVCollection.product()) {
0302       if (pv.isFake() || pv.tracksSize() < 10)
0303         continue;
0304       vertices.emplace_back(pv);
0305     }
0306     vertices.shrink_to_fit();
0307     beamSpotInfo->vertices_.emplace_back(std::move(vertices));
0308   }
0309 
0310   if (beamSpotInfo->beamSpotMap_.find("SC") == beamSpotInfo->beamSpotMap_.end()) {
0311     //BeamSpot from file for this stream is = to the scalar BeamSpot
0312     if (Handle<BeamSpot> recoBeamSpotHandle = iEvent.getHandle(scalerLabel_)) {
0313       beamSpotInfo->beamSpotMap_["SC"] = *recoBeamSpotHandle;
0314       if (beamSpotInfo->beamSpotMap_["SC"].BeamWidthX() != 0) {
0315         beamSpotInfo->beamSpotMap_["SC"].setType(reco::BeamSpot::Tracker);
0316       } else {
0317         beamSpotInfo->beamSpotMap_["SC"].setType(reco::BeamSpot::Fake);
0318       }
0319     } else {
0320       LogWarning("AlcaBeamMonitor") << "No BeamSpot from scalers is available";
0321       return;
0322     }
0323   }
0324 }
0325 
0326 //----------------------------------------------------------------------------------------------------------------------
0327 void AlcaBeamMonitor::globalEndLuminosityBlock(const LuminosityBlock& iLumi, const EventSetup& iSetup) {
0328   processedLumis_.push_back(iLumi.id().luminosityBlock());
0329 
0330   auto beamSpotInfo = luminosityBlockCache(iLumi.index());
0331   if (theBeamFitter_->runPVandTrkFitter()) {
0332     beamSpotInfo->beamSpotMap_["BF"] = theBeamFitter_->getBeamSpot();
0333   }
0334   theBeamFitter_->resetTrkVector();
0335   theBeamFitter_->resetLSRange();
0336   theBeamFitter_->resetRefTime();
0337   theBeamFitter_->resetPVFitter();
0338 
0339   if (thePVFitter_->runFitter()) {
0340     beamSpotInfo->beamSpotMap_["PV"] = thePVFitter_->getBeamSpot();
0341   }
0342   thePVFitter_->resetAll();
0343 
0344   //    "PV,BF..."      Value,Error
0345   map<std::string, pair<double, double> > resultsMap;
0346   vector<pair<double, double> > vertexResults;
0347   MonitorElement* histo = nullptr;
0348   for (vector<string>::iterator itV = varNamesV_.begin(); itV != varNamesV_.end(); itV++) {
0349     resultsMap.clear();
0350     for (BeamSpotContainer::iterator itBS = beamSpotInfo->beamSpotMap_.begin();
0351          itBS != beamSpotInfo->beamSpotMap_.end();
0352          itBS++) {
0353       if (itBS->second.type() == BeamSpot::Tracker) {
0354         if (*itV == "x") {
0355           resultsMap[itBS->first] = pair<double, double>(itBS->second.x0(), itBS->second.x0Error());
0356         } else if (*itV == "y") {
0357           resultsMap[itBS->first] = pair<double, double>(itBS->second.y0(), itBS->second.y0Error());
0358         } else if (*itV == "z") {
0359           resultsMap[itBS->first] = pair<double, double>(itBS->second.z0(), itBS->second.z0Error());
0360         } else if (*itV == "sigmaX") {
0361           resultsMap[itBS->first] = pair<double, double>(itBS->second.BeamWidthX(), itBS->second.BeamWidthXError());
0362         } else if (*itV == "sigmaY") {
0363           resultsMap[itBS->first] = pair<double, double>(itBS->second.BeamWidthY(), itBS->second.BeamWidthYError());
0364         } else if (*itV == "sigmaZ") {
0365           resultsMap[itBS->first] = pair<double, double>(itBS->second.sigmaZ(), itBS->second.sigmaZ0Error());
0366         } else {
0367           LogInfo("AlcaBeamMonitor") << "The histosMap_ has been built with the name " << *itV
0368                                      << " that I can't recognize!";
0369           //assert(0);
0370         }
0371       }
0372     }
0373     vertexResults.clear();
0374 
0375     for (const auto& itPV : beamSpotInfo->vertices_) {
0376       for (const auto& pv : itPV) {
0377         if (*itV == "x") {
0378           vertexResults.push_back(pv.xWithError());
0379         } else if (*itV == "y") {
0380           vertexResults.push_back(pv.yWithError());
0381         } else if (*itV == "z") {
0382           vertexResults.push_back(pv.zWithError());
0383         } else if (*itV != "sigmaX" && *itV != "sigmaY" && *itV != "sigmaZ") {
0384           LogInfo("AlcaBeamMonitor") << "The histosMap_ has been built with the name " << *itV
0385                                      << " that I can't recognize!";
0386           //assert(0);
0387         }
0388       }
0389     }
0390 
0391     for (multimap<string, string>::iterator itM = histoByCategoryNames_.begin(); itM != histoByCategoryNames_.end();
0392          itM++) {
0393       if ((histo = histosMap_[*itV][itM->first][itM->second]) == nullptr)
0394         continue;
0395       if (itM->second == "Coordinate") {
0396         if (beamSpotInfo->beamSpotMap_.find("DB") != beamSpotInfo->beamSpotMap_.end()) {
0397           histo->Fill(resultsMap["DB"].first);
0398         }
0399       } else if (itM->second == "PrimaryVertex fit-DataBase") {
0400         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("DB") != resultsMap.end()) {
0401           histo->Fill(resultsMap["PV"].first - resultsMap["DB"].first);
0402         }
0403       } else if (itM->second == "PrimaryVertex fit-BeamFit") {
0404         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("BF") != resultsMap.end()) {
0405           histo->Fill(resultsMap["PV"].first - resultsMap["BF"].first);
0406         }
0407       } else if (itM->second == "PrimaryVertex fit-Online") {
0408         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("SC") != resultsMap.end()) {
0409           histo->Fill(resultsMap["PV"].first - resultsMap["SC"].first);
0410         }
0411       } else if (itM->second == "PrimaryVertex-DataBase") {
0412         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("DB") != resultsMap.end()) {
0413           for (vector<pair<double, double> >::iterator itPV = vertexResults.begin(); itPV != vertexResults.end();
0414                itPV++) {
0415             histo->Fill(itPV->first - resultsMap["DB"].first);
0416           }
0417         }
0418       } else if (itM->second == "PrimaryVertex-BeamFit") {
0419         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("BF") != resultsMap.end()) {
0420           for (vector<pair<double, double> >::iterator itPV = vertexResults.begin(); itPV != vertexResults.end();
0421                itPV++) {
0422             histo->Fill(itPV->first - resultsMap["BF"].first);
0423           }
0424         }
0425       } else if (itM->second == "PrimaryVertex-Online") {
0426         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("SC") != resultsMap.end()) {
0427           for (vector<pair<double, double> >::iterator itPV = vertexResults.begin(); itPV != vertexResults.end();
0428                itPV++) {
0429             histo->Fill(itPV->first - resultsMap["SC"].first);
0430           }
0431         }
0432       } else if (itM->second == "Lumibased BeamSpotFit") {
0433         if (resultsMap.find("BF") != resultsMap.end()) {
0434           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["BF"].first);
0435           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["BF"].second);
0436         }
0437       } else if (itM->second == "Lumibased PrimaryVertex") {
0438         if (resultsMap.find("PV") != resultsMap.end()) {
0439           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["PV"].first);
0440           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["PV"].second);
0441         }
0442       } else if (itM->second == "Lumibased DataBase") {
0443         if (resultsMap.find("DB") != resultsMap.end()) {
0444           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["DB"].first);
0445           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["DB"].second);
0446         }
0447       } else if (itM->second == "Lumibased Online") {
0448         if (resultsMap.find("SC") != resultsMap.end()) {
0449           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["SC"].first);
0450           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["SC"].second);
0451         }
0452       } else if (itM->second == "Lumibased PrimaryVertex-DataBase fit") {
0453         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("DB") != resultsMap.end()) {
0454           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["PV"].first - resultsMap["DB"].first);
0455           histo->setBinError(iLumi.id().luminosityBlock(),
0456                              std::sqrt(std::pow(resultsMap["PV"].second, 2) + std::pow(resultsMap["DB"].second, 2)));
0457         }
0458       } else if (itM->second == "Lumibased PrimaryVertex-Online fit") {
0459         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("SC") != resultsMap.end()) {
0460           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["PV"].first - resultsMap["SC"].first);
0461           histo->setBinError(iLumi.id().luminosityBlock(),
0462                              std::sqrt(std::pow(resultsMap["PV"].second, 2) + std::pow(resultsMap["SC"].second, 2)));
0463         }
0464       } else if (itM->second == "Lumibased Online-DataBase fit") {
0465         if (resultsMap.find("SC") != resultsMap.end() && resultsMap.find("DB") != resultsMap.end()) {
0466           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["SC"].first - resultsMap["DB"].first);
0467           histo->setBinError(iLumi.id().luminosityBlock(),
0468                              std::sqrt(std::pow(resultsMap["SC"].second, 2) + std::pow(resultsMap["DB"].second, 2)));
0469         }
0470       } else if (itM->second == "Lumibased PrimaryVertex-DataBase") {
0471         if (resultsMap.find("DB") != resultsMap.end() && !vertexResults.empty()) {
0472           for (vector<pair<double, double> >::iterator itPV = vertexResults.begin(); itPV != vertexResults.end();
0473                itPV++) {
0474             histo->setBinContent(iLumi.id().luminosityBlock(), (*itPV).first - resultsMap["DB"].first);
0475             histo->setBinError(iLumi.id().luminosityBlock(),
0476                                std::sqrt(std::pow((*itPV).second, 2) + std::pow(resultsMap["DB"].second, 2)));
0477           }
0478         }
0479       } else if (itM->second == "Lumibased PrimaryVertex-Online") {
0480         if (resultsMap.find("SC") != resultsMap.end() && !vertexResults.empty()) {
0481           for (vector<pair<double, double> >::iterator itPV = vertexResults.begin(); itPV != vertexResults.end();
0482                itPV++) {
0483             histo->setBinContent(iLumi.id().luminosityBlock(), (*itPV).first - resultsMap["SC"].first);
0484             histo->setBinError(iLumi.id().luminosityBlock(),
0485                                std::sqrt(std::pow((*itPV).second, 2) + std::pow(resultsMap["SC"].second, 2)));
0486           }
0487         }
0488       }
0489       //      else if(itM->second == "Lumibased Online-DataBase"){
0490       //      if(resultsMap.find("SC") != resultsMap.end() && resultsMap.find("DB") != resultsMap.end()){
0491       //        itHHH->second->Fill(bin,resultsMap["SC"].first-resultsMap["DB"].first);
0492       //      }
0493       //    }
0494       else {
0495         LogInfo("AlcaBeamMonitor") << "The histosMap_ have a histogram named " << itM->second
0496                                    << " that I can't recognize in this loop!";
0497         //assert(0);
0498       }
0499     }
0500   }
0501 }
0502 
0503 void AlcaBeamMonitor::dqmEndRun(edm::Run const&, edm::EventSetup const&) {
0504   if (processedLumis_.empty()) {
0505     return;
0506   }
0507 
0508   const double bigNumber = 1000000.;
0509   std::sort(processedLumis_.begin(), processedLumis_.end());
0510   int firstLumi = *processedLumis_.begin();
0511   int lastLumi = *(--processedLumis_.end());
0512 
0513   for (HistosContainer::iterator itH = histosMap_.begin(); itH != histosMap_.end(); itH++) {
0514     for (map<string, map<string, MonitorElement*> >::iterator itHH = itH->second.begin(); itHH != itH->second.end();
0515          itHH++) {
0516       double min = bigNumber;
0517       double max = -bigNumber;
0518       double minDelta = bigNumber;
0519       double maxDelta = -bigNumber;
0520       //      double minDeltaProf = bigNumber;
0521       //      double maxDeltaProf = -bigNumber;
0522       if (itHH->first != "run") {
0523         for (map<string, MonitorElement*>::iterator itHHH = itHH->second.begin(); itHHH != itHH->second.end();
0524              itHHH++) {
0525           if (itHHH->second != nullptr) {
0526             for (int bin = 1; bin <= itHHH->second->getTH1()->GetNbinsX(); bin++) {
0527               if (itHHH->second->getTH1()->GetBinError(bin) != 0 || itHHH->second->getTH1()->GetBinContent(bin) != 0) {
0528                 if (itHHH->first == "Lumibased BeamSpotFit" || itHHH->first == "Lumibased PrimaryVertex" ||
0529                     itHHH->first == "Lumibased DataBase" || itHHH->first == "Lumibased Online") {
0530                   if (min > itHHH->second->getTH1()->GetBinContent(bin)) {
0531                     min = itHHH->second->getTH1()->GetBinContent(bin);
0532                   }
0533                   if (max < itHHH->second->getTH1()->GetBinContent(bin)) {
0534                     max = itHHH->second->getTH1()->GetBinContent(bin);
0535                   }
0536                 } else if (itHHH->first == "Lumibased PrimaryVertex-DataBase fit" ||
0537                            itHHH->first == "Lumibased PrimaryVertex-Online fit" ||
0538                            itHHH->first == "Lumibased Online-DataBase fit" ||
0539                            itHHH->first == "Lumibased PrimaryVertex-DataBase" ||
0540                            itHHH->first == "Lumibased PrimaryVertex-Online") {
0541                   if (minDelta > itHHH->second->getTH1()->GetBinContent(bin)) {
0542                     minDelta = itHHH->second->getTH1()->GetBinContent(bin);
0543                   }
0544                   if (maxDelta < itHHH->second->getTH1()->GetBinContent(bin)) {
0545                     maxDelta = itHHH->second->getTH1()->GetBinContent(bin);
0546                   }
0547                 } else {
0548                   LogInfo("AlcaBeamMonitorClient") << "The histosMap_ have a histogram named " << itHHH->first
0549                                                    << " that I can't recognize in this loop!";
0550                   // assert(0);
0551                 }
0552               }
0553             }
0554           }
0555         }
0556         for (map<string, MonitorElement*>::iterator itHHH = itHH->second.begin(); itHHH != itHH->second.end();
0557              itHHH++) {
0558           if (itHHH->second != nullptr) {
0559             if (itHHH->first == "Lumibased BeamSpotFit" || itHHH->first == "Lumibased PrimaryVertex" ||
0560                 itHHH->first == "Lumibased DataBase" || itHHH->first == "Lumibased Online") {
0561               if ((max == -bigNumber && min == bigNumber) || max - min == 0) {
0562                 itHHH->second->getTH1()->SetMinimum(itHHH->second->getTH1()->GetMinimum() - 0.01);
0563                 itHHH->second->getTH1()->SetMaximum(itHHH->second->getTH1()->GetMaximum() + 0.01);
0564               } else {
0565                 itHHH->second->getTH1()->SetMinimum(min - 0.1 * (max - min));
0566                 itHHH->second->getTH1()->SetMaximum(max + 0.1 * (max - min));
0567               }
0568             } else if (itHHH->first == "Lumibased PrimaryVertex-DataBase fit" ||
0569                        itHHH->first == "Lumibased PrimaryVertex-Online fit" ||
0570                        itHHH->first == "Lumibased Online-DataBase fit" ||
0571                        itHHH->first == "Lumibased PrimaryVertex-DataBase" ||
0572                        itHHH->first == "Lumibased PrimaryVertex-Online") {
0573               if ((maxDelta == -bigNumber && minDelta == bigNumber) || maxDelta - minDelta == 0) {
0574                 itHHH->second->getTH1()->SetMinimum(itHHH->second->getTH1()->GetMinimum() - 0.01);
0575                 itHHH->second->getTH1()->SetMaximum(itHHH->second->getTH1()->GetMaximum() + 0.01);
0576               } else {
0577                 itHHH->second->getTH1()->SetMinimum(minDelta - 2 * (maxDelta - minDelta));
0578                 itHHH->second->getTH1()->SetMaximum(maxDelta + 2 * (maxDelta - minDelta));
0579               }
0580             } else {
0581               LogInfo("AlcaBeamMonitorClient") << "The histosMap_ have a histogram named " << itHHH->first
0582                                                << " that I can't recognize in this loop!";
0583             }
0584             itHHH->second->getTH1()->GetXaxis()->SetRangeUser(firstLumi - 0.5, lastLumi + 0.5);
0585           }
0586         }
0587       }
0588     }
0589   }
0590 }
0591 DEFINE_FWK_MODULE(AlcaBeamMonitor);