Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*
0002  * \file BeamSpotProblemMonitor.cc
0003  * \author Sushil S. Chauhan/UC Davis
0004  *
0005  */
0006 
0007 #include "DQM/BeamMonitor/plugins/BeamSpotProblemMonitor.h"
0008 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0009 #include "DataFormats/TrackCandidate/interface/TrackCandidate.h"
0010 #include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 #include "FWCore/Framework/interface/LuminosityBlock.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 
0017 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0018 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
0019 
0020 #include <numeric>
0021 #include <cmath>
0022 
0023 using namespace std;
0024 using namespace edm;
0025 
0026 //
0027 // constructors and destructor
0028 //
0029 BeamSpotProblemMonitor::BeamSpotProblemMonitor(const ParameterSet& ps)
0030     : dcsStatus_{consumes<DcsStatusCollection>(ps.getUntrackedParameter<InputTag>("DCSStatus"))},
0031       scalertag_{consumes<BeamSpotOnlineCollection>(ps.getUntrackedParameter<InputTag>("scalarBSCollection"))},
0032       trkSrc_{consumes<reco::TrackCollection>(ps.getUntrackedParameter<InputTag>("pixelTracks"))},
0033       nTracks_{0},
0034       nCosmicTrk_{ps.getUntrackedParameter<int>("nCosmicTrk")},
0035       fitNLumi_{1},
0036       debug_{ps.getUntrackedParameter<bool>("Debug")},
0037       onlineMode_{ps.getUntrackedParameter<bool>("OnlineMode")},
0038       doTest_{ps.getUntrackedParameter<bool>("doTest")},
0039       alarmONThreshold_{ps.getUntrackedParameter<int>("AlarmONThreshold")},
0040       alarmOFFThreshold_{ps.getUntrackedParameter<int>("AlarmOFFThreshold")},
0041       lastlumi_{0},
0042       nextlumi_{0},
0043       processed_{false},
0044       alarmOn_{false},
0045       beamSpotStatus_{0},
0046       beamSpotFromDB_{0} {
0047   monitorName_ = ps.getUntrackedParameter<string>("monitorName");
0048 
0049   if (not monitorName_.empty())
0050     monitorName_ += "/";
0051 }
0052 
0053 void BeamSpotProblemMonitor::fillDescriptions(ConfigurationDescriptions& oDesc) {
0054   ParameterSetDescription desc;
0055   desc.addUntracked<string>("monitorName", "BeamSpotProblemMonitor");
0056   desc.addUntracked<InputTag>("DCSStatus", edm::InputTag("scalersRawToDigi"));
0057   desc.addUntracked<InputTag>("scalarBSCollection", edm::InputTag("scalersRawToDigi"));
0058   desc.addUntracked<InputTag>("pixelTracks", edm::InputTag("pixelTracks"));
0059   desc.addUntracked<int>("nCosmicTrk", 10);
0060   desc.addUntracked<bool>("Debug", false);
0061   desc.addUntracked<bool>("OnlineMode", true);
0062   desc.addUntracked<bool>("doTest", false);
0063   desc.addUntracked<int>("AlarmONThreshold", 10);
0064   desc.addUntracked<int>("AlarmOFFThreshold", 40);
0065 
0066   oDesc.add("dqmBeamSpotProblemMonitor", desc);
0067 }
0068 
0069 //--------------------------------------------------------
0070 void BeamSpotProblemMonitor::bookHistograms(DQMStore::IBooker& iB, const edm::Run&, const edm::EventSetup&) {
0071   // create and cd into new folder
0072   iB.setCurrentFolder(monitorName_ + "FitFromScalars");
0073 
0074   const string coord{"BeamSpotStatus"};
0075 
0076   string histName(coord + "_lumi");
0077   string histTitle(coord);
0078   const string ytitle("Problem (-1)  /  OK (1)");
0079   const string xtitle("Lumisection");
0080 
0081   beamSpotStatusLumi_ = iB.book1D(histName, histTitle, 40, 0.5, 40.5);
0082   beamSpotStatusLumi_->setAxisTitle(xtitle, 1);
0083   beamSpotStatusLumi_->setAxisTitle(ytitle, 2);
0084 
0085   histName += "_all";
0086   histTitle += " all";
0087   beamSpotStatusLumiAll_ = iB.book1D(histName, histTitle, 40, 0.5, 40.5);
0088   beamSpotStatusLumiAll_->getTH1()->SetCanExtend(TH1::kAllAxes);
0089   beamSpotStatusLumiAll_->setAxisTitle(xtitle, 1);
0090   beamSpotStatusLumiAll_->setAxisTitle(ytitle, 2);
0091 
0092   //NOTE: This in principal should be a Lumi only histogram since it gets reset at every
0093   // dqmBeginLuminosityBlock call. However, it is also filled at that time and the DQMStore
0094   // clears all lumi histograms at postGlobalBeginLuminosityBlock!
0095   beamSpotError_ = iB.book1D("BeamSpotError", "ERROR: Beamspot missing from scalars", 20, 0.5, 20.5);
0096   beamSpotError_->setAxisTitle("# of consecutive LSs with problem", 1);
0097   beamSpotError_->setAxisTitle("Problem with scalar BeamSpot", 2);
0098 }
0099 
0100 //--------------------------------------------------------
0101 void BeamSpotProblemMonitor::dqmBeginLuminosityBlock(const LuminosityBlock& lumiSeg, const EventSetup& context) {
0102   const int nthlumi = lumiSeg.luminosityBlock();
0103 
0104   if (onlineMode_) {
0105     if (nthlumi > nextlumi_) {
0106       fillPlots(lastlumi_, nextlumi_, nthlumi);
0107       nextlumi_ = nthlumi;
0108       edm::LogInfo("BeamSpotProblemMonitor") << "dqmBeginLuminosityBlock:: Next Lumi to Fit: " << nextlumi_ << endl;
0109     }
0110   } else {
0111     if (processed_)
0112       fillPlots(lastlumi_, nextlumi_, nthlumi);
0113     nextlumi_ = nthlumi;
0114     edm::LogInfo("BeamSpotProblemMonitor") << " dqmBeginLuminosityBlock:: Next Lumi to Fit: " << nextlumi_ << endl;
0115   }
0116 
0117   if (processed_)
0118     processed_ = false;
0119   edm::LogInfo("BeamSpotProblemMonitor") << " dqmBeginLuminosityBlock::  Begin of Lumi: " << nthlumi << endl;
0120 }
0121 
0122 // ----------------------------------------------------------
0123 void BeamSpotProblemMonitor::analyze(const Event& iEvent, const EventSetup& iSetup) {
0124   const int nthlumi = iEvent.luminosityBlock();
0125 
0126   if (onlineMode_ && (nthlumi < nextlumi_)) {
0127     edm::LogInfo("BeamSpotProblemMonitor") << "analyze::  Spilt event from previous lumi section!" << std::endl;
0128     return;
0129   }
0130   if (onlineMode_ && (nthlumi > nextlumi_)) {
0131     edm::LogInfo("BeamSpotProblemMonitor") << "analyze::  Spilt event from next lumi section!!!" << std::endl;
0132     return;
0133   }
0134 
0135   beamSpotStatus_ = 0.;
0136 
0137   // Checking TK status
0138   Handle<DcsStatusCollection> dcsStatus;
0139   iEvent.getByToken(dcsStatus_, dcsStatus);
0140   std::array<bool, 6> dcsTk;
0141   for (auto& e : dcsTk) {
0142     e = true;
0143   }
0144   for (auto const& status : *dcsStatus) {
0145     if (!status.ready(DcsStatus::BPIX))
0146       dcsTk[0] = false;
0147     if (!status.ready(DcsStatus::FPIX))
0148       dcsTk[1] = false;
0149     if (!status.ready(DcsStatus::TIBTID))
0150       dcsTk[2] = false;
0151     if (!status.ready(DcsStatus::TOB))
0152       dcsTk[3] = false;
0153     if (!status.ready(DcsStatus::TECp))
0154       dcsTk[4] = false;
0155     if (!status.ready(DcsStatus::TECm))
0156       dcsTk[5] = false;
0157   }
0158 
0159   bool allTkOn = true;
0160   for (auto status : dcsTk) {
0161     if (!status) {
0162       allTkOn = false;
0163       break;
0164     }
0165   }
0166 
0167   //If tracker is ON and collision is going on then must be few track ther
0168   edm::Handle<reco::TrackCollection> trackCollection;
0169   iEvent.getByToken(trkSrc_, trackCollection);
0170   for (auto const& track : *trackCollection) {
0171     if (track.pt() > 1.0)
0172       nTracks_++;
0173     if (nTracks_ > 200)
0174       break;
0175   }
0176 
0177   // get scalar collection and BeamSpot
0178   Handle<BeamSpotOnlineCollection> handleScaler;
0179   iEvent.getByToken(scalertag_, handleScaler);
0180 
0181   // beam spot scalar object
0182   BeamSpotOnline spotOnline;
0183 
0184   bool fallBackToDB = false;
0185   alarmOn_ = false;
0186 
0187   if (!handleScaler->empty()) {
0188     spotOnline = *(handleScaler->begin());
0189 
0190     // check if we have a valid beam spot fit result from online DQM thrugh scalars
0191     if (spotOnline.x() == 0. && spotOnline.y() == 0. && spotOnline.z() == 0. && spotOnline.width_x() == 0. &&
0192         spotOnline.width_y() == 0.) {
0193       fallBackToDB = true;
0194     }
0195   }
0196 
0197   //For testing set it false for every LSs
0198   if (doTest_)
0199     fallBackToDB = true;
0200 
0201   //based on last event of this lumi only as it overwrite it
0202   if (allTkOn && fallBackToDB) {
0203     beamSpotStatus_ = -1.;
0204   }  //i.e,from DB
0205   if (allTkOn && (!fallBackToDB)) {
0206     beamSpotStatus_ = 1.;
0207   }  //i.e,from online DQM
0208 
0209   //when collision at least few tracks should be there otherwise it give false ALARM
0210   if (allTkOn && nTracks_ < nCosmicTrk_)
0211     beamSpotStatus_ = 0.;
0212 
0213   processed_ = true;
0214 }
0215 
0216 //--------------------------------------------------------
0217 void BeamSpotProblemMonitor::fillPlots(int& lastlumi, int& nextlumi, int nthlumi) {
0218   if (onlineMode_ && (nthlumi <= nextlumi))
0219     return;
0220 
0221   int currentlumi = nextlumi;
0222   lastlumi = currentlumi;
0223 
0224   //Chcek status and if lumi are in succession when fall to DB
0225   if (beamSpotStatus_ == -1. && (lastlumi + 1) == nthlumi) {
0226     beamSpotFromDB_++;
0227   } else {
0228     beamSpotFromDB_ = 0;  //if not in succesion or status is ok then set zero
0229   }
0230 
0231   if (beamSpotFromDB_ >= alarmONThreshold_) {
0232     alarmOn_ = true;  //set the audio alarm true after N successive LSs
0233   }
0234 
0235   if (beamSpotFromDB_ > alarmOFFThreshold_) {
0236     alarmOn_ = false;     //set the audio alarm true after 10 successive LSs
0237     beamSpotFromDB_ = 0;  //reset it for new incident
0238   }
0239 
0240   if (onlineMode_) {  // filling LS gap For status plot
0241 
0242     const int countLS_bs = beamSpotStatusLumi_->getTH1()->GetEntries();
0243     int LSgap_bs = currentlumi / fitNLumi_ - countLS_bs;
0244     if (currentlumi % fitNLumi_ == 0)
0245       LSgap_bs--;
0246 
0247     // filling previous fits if LS gap ever exists
0248     for (int ig = 0; ig < LSgap_bs; ig++) {
0249       beamSpotStatusLumi_->ShiftFillLast(0., 0., fitNLumi_);  //x0 , x0err, fitNLumi_;  see DQMCore....
0250     }
0251 
0252     beamSpotStatusLumi_->ShiftFillLast(
0253         beamSpotStatus_,
0254         0.,
0255         fitNLumi_);  //beamSpotStatus_ =>0. (no collision, no tracks); =>1 (OK from scaler), =>-1 (No scalar results)
0256     beamSpotStatusLumiAll_->setBinContent(currentlumi, beamSpotStatus_);
0257 
0258   } else {
0259     beamSpotStatusLumi_->ShiftFillLast(0., 0., fitNLumi_);
0260   }  //onlineMode_
0261 
0262   //Reset it here for next lumi
0263   beamSpotError_->Reset();
0264   if (alarmOn_)
0265     beamSpotError_->Fill(beamSpotFromDB_);
0266 
0267   //Get quality report
0268   const QReport* beamSpotQReport = beamSpotError_->getQReport("BeamSpotOnlineTest");
0269 
0270   if (beamSpotQReport) {
0271     /* S.Dutta : Commenting out these variables are not used and giving error with "-Werror=unused-variable" option
0272     float qtresult = BeamSpotQReport->getQTresult();
0273     int qtstatus   = BeamSpotQReport->getStatus() ; // get QT status value (see table below) */
0274   }
0275 
0276   nTracks_ = 0;
0277 }
0278 
0279 //--------------------------------------------------------
0280 void BeamSpotProblemMonitor::dqmEndLuminosityBlock(const LuminosityBlock& lumiSeg, const EventSetup& iSetup) {
0281   const int nthlumi = lumiSeg.id().luminosityBlock();
0282   edm::LogInfo("BeamSpotProblemMonitor")
0283       << "dqmEndLuminosityBlock:: Lumi of the last event before dqmEndLuminosityBlock: " << nthlumi << endl;
0284 }
0285 //-------------------------------------------------------
0286 
0287 void BeamSpotProblemMonitor::dqmEndRun(const Run& r, const EventSetup& context) {
0288   if (debug_)
0289     edm::LogInfo("BeamSpotProblemMonitor") << "dqmEndRun:: Clearing all the Maps " << endl;
0290   //Reset it end of job
0291   beamSpotError_->Reset();
0292 }
0293 
0294 DEFINE_FWK_MODULE(BeamSpotProblemMonitor);
0295 
0296 // Local Variables:
0297 // show-trailing-whitespace: t
0298 // truncate-lines: t
0299 // End: