Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:41:36

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 < 7; ++i) {
0249       for (int j = 0; j < 7; ++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     beamSpotInfo->vertices_.push_back(*PVCollection.product());
0300   }
0301 
0302   if (beamSpotInfo->beamSpotMap_.find("SC") == beamSpotInfo->beamSpotMap_.end()) {
0303     //BeamSpot from file for this stream is = to the scalar BeamSpot
0304     if (Handle<BeamSpot> recoBeamSpotHandle = iEvent.getHandle(scalerLabel_)) {
0305       beamSpotInfo->beamSpotMap_["SC"] = *recoBeamSpotHandle;
0306       if (beamSpotInfo->beamSpotMap_["SC"].BeamWidthX() != 0) {
0307         beamSpotInfo->beamSpotMap_["SC"].setType(reco::BeamSpot::Tracker);
0308       } else {
0309         beamSpotInfo->beamSpotMap_["SC"].setType(reco::BeamSpot::Fake);
0310       }
0311     } else {
0312       LogWarning("AlcaBeamMonitor") << "No BeamSpot from scalers is available";
0313       return;
0314     }
0315   }
0316 }
0317 
0318 //----------------------------------------------------------------------------------------------------------------------
0319 void AlcaBeamMonitor::globalEndLuminosityBlock(const LuminosityBlock& iLumi, const EventSetup& iSetup) {
0320   processedLumis_.push_back(iLumi.id().luminosityBlock());
0321 
0322   auto beamSpotInfo = luminosityBlockCache(iLumi.index());
0323   if (theBeamFitter_->runPVandTrkFitter()) {
0324     beamSpotInfo->beamSpotMap_["BF"] = theBeamFitter_->getBeamSpot();
0325   }
0326   theBeamFitter_->resetTrkVector();
0327   theBeamFitter_->resetLSRange();
0328   theBeamFitter_->resetRefTime();
0329   theBeamFitter_->resetPVFitter();
0330 
0331   if (thePVFitter_->runFitter()) {
0332     beamSpotInfo->beamSpotMap_["PV"] = thePVFitter_->getBeamSpot();
0333   }
0334   thePVFitter_->resetAll();
0335 
0336   //    "PV,BF..."      Value,Error
0337   map<std::string, pair<double, double> > resultsMap;
0338   vector<pair<double, double> > vertexResults;
0339   MonitorElement* histo = nullptr;
0340   for (vector<string>::iterator itV = varNamesV_.begin(); itV != varNamesV_.end(); itV++) {
0341     resultsMap.clear();
0342     for (BeamSpotContainer::iterator itBS = beamSpotInfo->beamSpotMap_.begin();
0343          itBS != beamSpotInfo->beamSpotMap_.end();
0344          itBS++) {
0345       if (itBS->second.type() == BeamSpot::Tracker) {
0346         if (*itV == "x") {
0347           resultsMap[itBS->first] = pair<double, double>(itBS->second.x0(), itBS->second.x0Error());
0348         } else if (*itV == "y") {
0349           resultsMap[itBS->first] = pair<double, double>(itBS->second.y0(), itBS->second.y0Error());
0350         } else if (*itV == "z") {
0351           resultsMap[itBS->first] = pair<double, double>(itBS->second.z0(), itBS->second.z0Error());
0352         } else if (*itV == "sigmaX") {
0353           resultsMap[itBS->first] = pair<double, double>(itBS->second.BeamWidthX(), itBS->second.BeamWidthXError());
0354         } else if (*itV == "sigmaY") {
0355           resultsMap[itBS->first] = pair<double, double>(itBS->second.BeamWidthY(), itBS->second.BeamWidthYError());
0356         } else if (*itV == "sigmaZ") {
0357           resultsMap[itBS->first] = pair<double, double>(itBS->second.sigmaZ(), itBS->second.sigmaZ0Error());
0358         } else {
0359           LogInfo("AlcaBeamMonitor") << "The histosMap_ has been built with the name " << *itV
0360                                      << " that I can't recognize!";
0361           //assert(0);
0362         }
0363       }
0364     }
0365     vertexResults.clear();
0366     for (vector<VertexCollection>::iterator itPV = beamSpotInfo->vertices_.begin();
0367          itPV != beamSpotInfo->vertices_.end();
0368          itPV++) {
0369       if (!itPV->empty()) {
0370         for (VertexCollection::const_iterator pv = itPV->begin(); pv != itPV->end(); pv++) {
0371           if (pv->isFake() || pv->tracksSize() < 10)
0372             continue;
0373           if (*itV == "x") {
0374             vertexResults.push_back(pair<double, double>(pv->x(), pv->xError()));
0375           } else if (*itV == "y") {
0376             vertexResults.push_back(pair<double, double>(pv->y(), pv->yError()));
0377           } else if (*itV == "z") {
0378             vertexResults.push_back(pair<double, double>(pv->z(), pv->zError()));
0379           } else if (*itV != "sigmaX" && *itV != "sigmaY" && *itV != "sigmaZ") {
0380             LogInfo("AlcaBeamMonitor") << "The histosMap_ has been built with the name " << *itV
0381                                        << " that I can't recognize!";
0382             //assert(0);
0383           }
0384         }
0385       }
0386     }
0387 
0388     for (multimap<string, string>::iterator itM = histoByCategoryNames_.begin(); itM != histoByCategoryNames_.end();
0389          itM++) {
0390       if ((histo = histosMap_[*itV][itM->first][itM->second]) == nullptr)
0391         continue;
0392       if (itM->second == "Coordinate") {
0393         if (beamSpotInfo->beamSpotMap_.find("DB") != beamSpotInfo->beamSpotMap_.end()) {
0394           histo->Fill(resultsMap["DB"].first);
0395         }
0396       } else if (itM->second == "PrimaryVertex fit-DataBase") {
0397         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("DB") != resultsMap.end()) {
0398           histo->Fill(resultsMap["PV"].first - resultsMap["DB"].first);
0399         }
0400       } else if (itM->second == "PrimaryVertex fit-BeamFit") {
0401         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("BF") != resultsMap.end()) {
0402           histo->Fill(resultsMap["PV"].first - resultsMap["BF"].first);
0403         }
0404       } else if (itM->second == "PrimaryVertex fit-Online") {
0405         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("SC") != resultsMap.end()) {
0406           histo->Fill(resultsMap["PV"].first - resultsMap["SC"].first);
0407         }
0408       } else if (itM->second == "PrimaryVertex-DataBase") {
0409         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("DB") != resultsMap.end()) {
0410           for (vector<pair<double, double> >::iterator itPV = vertexResults.begin(); itPV != vertexResults.end();
0411                itPV++) {
0412             histo->Fill(itPV->first - resultsMap["DB"].first);
0413           }
0414         }
0415       } else if (itM->second == "PrimaryVertex-BeamFit") {
0416         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("BF") != resultsMap.end()) {
0417           for (vector<pair<double, double> >::iterator itPV = vertexResults.begin(); itPV != vertexResults.end();
0418                itPV++) {
0419             histo->Fill(itPV->first - resultsMap["BF"].first);
0420           }
0421         }
0422       } else if (itM->second == "PrimaryVertex-Online") {
0423         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("SC") != resultsMap.end()) {
0424           for (vector<pair<double, double> >::iterator itPV = vertexResults.begin(); itPV != vertexResults.end();
0425                itPV++) {
0426             histo->Fill(itPV->first - resultsMap["SC"].first);
0427           }
0428         }
0429       } else if (itM->second == "Lumibased BeamSpotFit") {
0430         if (resultsMap.find("BF") != resultsMap.end()) {
0431           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["BF"].first);
0432           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["BF"].second);
0433         }
0434       } else if (itM->second == "Lumibased PrimaryVertex") {
0435         if (resultsMap.find("PV") != resultsMap.end()) {
0436           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["PV"].first);
0437           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["PV"].second);
0438         }
0439       } else if (itM->second == "Lumibased DataBase") {
0440         if (resultsMap.find("DB") != resultsMap.end()) {
0441           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["DB"].first);
0442           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["DB"].second);
0443         }
0444       } else if (itM->second == "Lumibased Online") {
0445         if (resultsMap.find("SC") != resultsMap.end()) {
0446           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["SC"].first);
0447           histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["SC"].second);
0448         }
0449       } else if (itM->second == "Lumibased PrimaryVertex-DataBase fit") {
0450         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("DB") != resultsMap.end()) {
0451           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["PV"].first - resultsMap["DB"].first);
0452           histo->setBinError(iLumi.id().luminosityBlock(),
0453                              std::sqrt(std::pow(resultsMap["PV"].second, 2) + std::pow(resultsMap["DB"].second, 2)));
0454         }
0455       } else if (itM->second == "Lumibased PrimaryVertex-Online fit") {
0456         if (resultsMap.find("PV") != resultsMap.end() && resultsMap.find("SC") != resultsMap.end()) {
0457           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["PV"].first - resultsMap["SC"].first);
0458           histo->setBinError(iLumi.id().luminosityBlock(),
0459                              std::sqrt(std::pow(resultsMap["PV"].second, 2) + std::pow(resultsMap["SC"].second, 2)));
0460         }
0461       } else if (itM->second == "Lumibased Online-DataBase fit") {
0462         if (resultsMap.find("SC") != resultsMap.end() && resultsMap.find("DB") != resultsMap.end()) {
0463           histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["SC"].first - resultsMap["DB"].first);
0464           histo->setBinError(iLumi.id().luminosityBlock(),
0465                              std::sqrt(std::pow(resultsMap["SC"].second, 2) + std::pow(resultsMap["DB"].second, 2)));
0466         }
0467       } else if (itM->second == "Lumibased PrimaryVertex-DataBase") {
0468         if (resultsMap.find("DB") != resultsMap.end() && !vertexResults.empty()) {
0469           for (vector<pair<double, double> >::iterator itPV = vertexResults.begin(); itPV != vertexResults.end();
0470                itPV++) {
0471             histo->setBinContent(iLumi.id().luminosityBlock(), (*itPV).first - resultsMap["DB"].first);
0472             histo->setBinError(iLumi.id().luminosityBlock(),
0473                                std::sqrt(std::pow((*itPV).second, 2) + std::pow(resultsMap["DB"].second, 2)));
0474           }
0475         }
0476       } else if (itM->second == "Lumibased PrimaryVertex-Online") {
0477         if (resultsMap.find("SC") != resultsMap.end() && !vertexResults.empty()) {
0478           for (vector<pair<double, double> >::iterator itPV = vertexResults.begin(); itPV != vertexResults.end();
0479                itPV++) {
0480             histo->setBinContent(iLumi.id().luminosityBlock(), (*itPV).first - resultsMap["SC"].first);
0481             histo->setBinError(iLumi.id().luminosityBlock(),
0482                                std::sqrt(std::pow((*itPV).second, 2) + std::pow(resultsMap["SC"].second, 2)));
0483           }
0484         }
0485       }
0486       //      else if(itM->second == "Lumibased Online-DataBase"){
0487       //      if(resultsMap.find("SC") != resultsMap.end() && resultsMap.find("DB") != resultsMap.end()){
0488       //        itHHH->second->Fill(bin,resultsMap["SC"].first-resultsMap["DB"].first);
0489       //      }
0490       //    }
0491       else {
0492         LogInfo("AlcaBeamMonitor") << "The histosMap_ have a histogram named " << itM->second
0493                                    << " that I can't recognize in this loop!";
0494         //assert(0);
0495       }
0496     }
0497   }
0498 }
0499 
0500 void AlcaBeamMonitor::dqmEndRun(edm::Run const&, edm::EventSetup const&) {
0501   if (processedLumis_.empty()) {
0502     return;
0503   }
0504 
0505   const double bigNumber = 1000000.;
0506   std::sort(processedLumis_.begin(), processedLumis_.end());
0507   int firstLumi = *processedLumis_.begin();
0508   int lastLumi = *(--processedLumis_.end());
0509 
0510   for (HistosContainer::iterator itH = histosMap_.begin(); itH != histosMap_.end(); itH++) {
0511     for (map<string, map<string, MonitorElement*> >::iterator itHH = itH->second.begin(); itHH != itH->second.end();
0512          itHH++) {
0513       double min = bigNumber;
0514       double max = -bigNumber;
0515       double minDelta = bigNumber;
0516       double maxDelta = -bigNumber;
0517       //      double minDeltaProf = bigNumber;
0518       //      double maxDeltaProf = -bigNumber;
0519       if (itHH->first != "run") {
0520         for (map<string, MonitorElement*>::iterator itHHH = itHH->second.begin(); itHHH != itHH->second.end();
0521              itHHH++) {
0522           if (itHHH->second != nullptr) {
0523             for (int bin = 1; bin <= itHHH->second->getTH1()->GetNbinsX(); bin++) {
0524               if (itHHH->second->getTH1()->GetBinError(bin) != 0 || itHHH->second->getTH1()->GetBinContent(bin) != 0) {
0525                 if (itHHH->first == "Lumibased BeamSpotFit" || itHHH->first == "Lumibased PrimaryVertex" ||
0526                     itHHH->first == "Lumibased DataBase" || itHHH->first == "Lumibased Online") {
0527                   if (min > itHHH->second->getTH1()->GetBinContent(bin)) {
0528                     min = itHHH->second->getTH1()->GetBinContent(bin);
0529                   }
0530                   if (max < itHHH->second->getTH1()->GetBinContent(bin)) {
0531                     max = itHHH->second->getTH1()->GetBinContent(bin);
0532                   }
0533                 } else if (itHHH->first == "Lumibased PrimaryVertex-DataBase fit" ||
0534                            itHHH->first == "Lumibased PrimaryVertex-Online fit" ||
0535                            itHHH->first == "Lumibased Online-DataBase fit" ||
0536                            itHHH->first == "Lumibased PrimaryVertex-DataBase" ||
0537                            itHHH->first == "Lumibased PrimaryVertex-Online") {
0538                   if (minDelta > itHHH->second->getTH1()->GetBinContent(bin)) {
0539                     minDelta = itHHH->second->getTH1()->GetBinContent(bin);
0540                   }
0541                   if (maxDelta < itHHH->second->getTH1()->GetBinContent(bin)) {
0542                     maxDelta = itHHH->second->getTH1()->GetBinContent(bin);
0543                   }
0544                 } else {
0545                   LogInfo("AlcaBeamMonitorClient") << "The histosMap_ have a histogram named " << itHHH->first
0546                                                    << " that I can't recognize in this loop!";
0547                   // assert(0);
0548                 }
0549               }
0550             }
0551           }
0552         }
0553         for (map<string, MonitorElement*>::iterator itHHH = itHH->second.begin(); itHHH != itHH->second.end();
0554              itHHH++) {
0555           if (itHHH->second != nullptr) {
0556             if (itHHH->first == "Lumibased BeamSpotFit" || itHHH->first == "Lumibased PrimaryVertex" ||
0557                 itHHH->first == "Lumibased DataBase" || itHHH->first == "Lumibased Online") {
0558               if ((max == -bigNumber && min == bigNumber) || max - min == 0) {
0559                 itHHH->second->getTH1()->SetMinimum(itHHH->second->getTH1()->GetMinimum() - 0.01);
0560                 itHHH->second->getTH1()->SetMaximum(itHHH->second->getTH1()->GetMaximum() + 0.01);
0561               } else {
0562                 itHHH->second->getTH1()->SetMinimum(min - 0.1 * (max - min));
0563                 itHHH->second->getTH1()->SetMaximum(max + 0.1 * (max - min));
0564               }
0565             } else if (itHHH->first == "Lumibased PrimaryVertex-DataBase fit" ||
0566                        itHHH->first == "Lumibased PrimaryVertex-Online fit" ||
0567                        itHHH->first == "Lumibased Online-DataBase fit" ||
0568                        itHHH->first == "Lumibased PrimaryVertex-DataBase" ||
0569                        itHHH->first == "Lumibased PrimaryVertex-Online") {
0570               if ((maxDelta == -bigNumber && minDelta == bigNumber) || maxDelta - minDelta == 0) {
0571                 itHHH->second->getTH1()->SetMinimum(itHHH->second->getTH1()->GetMinimum() - 0.01);
0572                 itHHH->second->getTH1()->SetMaximum(itHHH->second->getTH1()->GetMaximum() + 0.01);
0573               } else {
0574                 itHHH->second->getTH1()->SetMinimum(minDelta - 2 * (maxDelta - minDelta));
0575                 itHHH->second->getTH1()->SetMaximum(maxDelta + 2 * (maxDelta - minDelta));
0576               }
0577             } else {
0578               LogInfo("AlcaBeamMonitorClient") << "The histosMap_ have a histogram named " << itHHH->first
0579                                                << " that I can't recognize in this loop!";
0580             }
0581             itHHH->second->getTH1()->GetXaxis()->SetRangeUser(firstLumi - 0.5, lastLumi + 0.5);
0582           }
0583         }
0584       }
0585     }
0586   }
0587 }
0588 DEFINE_FWK_MODULE(AlcaBeamMonitor);