00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "DQM/BeamMonitor/plugins/BeamSpotProblemMonitor.h"
00013 #include "DQMServices/Core/interface/QReport.h"
00014 #include "FWCore/ServiceRegistry/interface/Service.h"
00015 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00016 #include "DataFormats/VertexReco/interface/Vertex.h"
00017 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00018 #include "DataFormats/TrackCandidate/interface/TrackCandidate.h"
00019 #include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
00020 #include "DataFormats/TrackReco/interface/Track.h"
00021 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00022 #include "DataFormats/Common/interface/View.h"
00023 #include "RecoVertex/BeamSpotProducer/interface/BSFitter.h"
00024 #include "FWCore/Framework/interface/MakerMacros.h"
00025 #include "FWCore/Framework/interface/LuminosityBlock.h"
00026 #include "FWCore/Framework/interface/Run.h"
00027 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00028 #include "DataFormats/Common/interface/TriggerResults.h"
00029 #include "FWCore/Common/interface/TriggerNames.h"
00030 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
00031
00032 #include "RecoVertex/BeamSpotProducer/interface/BeamSpotOnlineProducer.h"
00033 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00034 #include "DataFormats/Scalers/interface/BeamSpotOnline.h"
00035 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
00036 #include "DataFormats/Scalers/interface/DcsStatus.h"
00037 #include "DQMServices/Core/interface/QReport.h"
00038
00039 #include <numeric>
00040 #include <math.h>
00041 #include <TMath.h>
00042 #include <iostream>
00043 #include <TStyle.h>
00044
00045 using namespace std;
00046 using namespace edm;
00047
00048
00049 #define buffTime (23)
00050
00051
00052
00053
00054 BeamSpotProblemMonitor::BeamSpotProblemMonitor( const ParameterSet& ps ) :
00055 Ntracks_(0), fitNLumi_(0), ALARM_ON_(false), BeamSpotStatus_(0), BeamSpotFromDB_(0){
00056
00057 parameters_ = ps;
00058 monitorName_ = parameters_.getUntrackedParameter<string>("monitorName","YourSubsystemName");
00059 trkSrc_ = parameters_.getUntrackedParameter<InputTag>("pixelTracks");
00060 nCosmicTrk_ = parameters_.getUntrackedParameter<int>("nCosmicTrk");
00061 scalertag_ = parameters_.getUntrackedParameter<InputTag>("scalarBSCollection");
00062 intervalInSec_ = parameters_.getUntrackedParameter<int>("timeInterval",920);
00063 debug_ = parameters_.getUntrackedParameter<bool>("Debug");
00064 onlineMode_ = parameters_.getUntrackedParameter<bool>("OnlineMode");
00065 alarmONThreshold_ = parameters_.getUntrackedParameter<int>("AlarmONThreshold");
00066 alarmOFFThreshold_= parameters_.getUntrackedParameter<int>("AlarmOFFThreshold");
00067 doTest_ =parameters_.getUntrackedParameter<bool>("doTest");
00068
00069 dbe_ = Service<DQMStore>().operator->();
00070 if (monitorName_ != "" ) monitorName_ = monitorName_+"/" ;
00071
00072
00073 if (fitNLumi_ <= 0) fitNLumi_ = 1;
00074 lastlumi_ = 0;
00075 nextlumi_ = 0;
00076 processed_ = false;
00077 }
00078
00079
00080 BeamSpotProblemMonitor::~BeamSpotProblemMonitor() {
00081 }
00082
00083
00084
00085 void BeamSpotProblemMonitor::beginJob() {
00086
00087
00088
00089 dbe_->setCurrentFolder(monitorName_+"FitFromScalars");
00090
00091
00092
00093
00094
00095 const int nvar_ = 1;
00096 string coord[nvar_] = {"BeamSpotStatus"};
00097
00098
00099
00100 for (int i = 0; i < 1; i++) {
00101 dbe_->setCurrentFolder(monitorName_+"FitFromScalars");
00102 for (int ic=0; ic<nvar_; ++ic) {
00103 TString histName(coord[ic]);
00104 TString histTitle(coord[ic]);
00105 string ytitle("Problem (-1) / OK (1)");
00106 string xtitle("");
00107 string options("E1");
00108 bool createHisto = true;
00109 switch(i){
00110
00111 case 0:
00112 histName += "_lumi";
00113 xtitle = "Lumisection";
00114
00115 if (createHisto) {
00116 hs[histName] = dbe_->book1D(histName,histTitle,40,0.5,40.5);
00117 hs[histName]->setAxisTitle(xtitle,1);
00118 hs[histName]->setAxisTitle(ytitle,2);
00119
00120 histName += "_all";
00121 histTitle += " all";
00122 hs[histName] = dbe_->book1D(histName,histTitle,40,0.5,40.5);
00123 hs[histName]->getTH1()->SetBit(TH1::kCanRebin);
00124 hs[histName]->setAxisTitle(xtitle,1);
00125 hs[histName]->setAxisTitle(ytitle,2);
00126
00127 }
00128 }
00129 }
00130 }
00131
00132 BeamSpotError = dbe_->book1D("BeamSpotError","ERROR: Beamspot missing from scalars",20,0.5,20.5);
00133 BeamSpotError->setAxisTitle("# of consecutive LSs with problem",1);
00134 BeamSpotError->setAxisTitle("Problem with scalar BeamSpot",2);
00135
00136
00137 dbe_->setCurrentFolder(monitorName_+"FitFromScalars");
00138
00139
00140
00141 }
00142
00143
00144 void BeamSpotProblemMonitor::beginRun(const edm::Run& r, const EventSetup& context) {
00145
00146
00147 if (debug_) {
00148 edm::LogInfo("BeamSpotProblemMonitor") << "TimeOffset = ";
00149 }
00150
00151 }
00152
00153
00154 void BeamSpotProblemMonitor::beginLuminosityBlock(const LuminosityBlock& lumiSeg,
00155 const EventSetup& context) {
00156 int nthlumi = lumiSeg.luminosityBlock();
00157
00158
00159 if (onlineMode_) {
00160 if (nthlumi > nextlumi_) {
00161 FillPlots(lumiSeg,lastlumi_,nextlumi_,nthlumi);
00162 nextlumi_ = nthlumi;
00163 edm::LogInfo("BeamSpotProblemMonitor") << "beginLuminosityBlock:: Next Lumi to Fit: " << nextlumi_ << endl;
00164 }
00165 }
00166 else{
00167 if (processed_) FillPlots(lumiSeg,lastlumi_,nextlumi_,nthlumi);
00168 nextlumi_ = nthlumi;
00169 edm::LogInfo("BeamSpotProblemMonitor") << " beginLuminosityBlock:: Next Lumi to Fit: " << nextlumi_ << endl;
00170 }
00171
00172 if (processed_) processed_ = false;
00173 edm::LogInfo("BeamSpotProblemMonitor") << " beginLuminosityBlock:: Begin of Lumi: " << nthlumi << endl;
00174 }
00175
00176
00177
00178
00179
00180 void BeamSpotProblemMonitor::analyze(const Event& iEvent,
00181 const EventSetup& iSetup ) {
00182 const int nthlumi = iEvent.luminosityBlock();
00183
00184
00185 if (onlineMode_ && (nthlumi < nextlumi_)) {
00186 edm::LogInfo("BeamSpotProblemMonitor") << "analyze:: Spilt event from previous lumi section!" << std::endl;
00187 return;
00188 }
00189 if (onlineMode_ && (nthlumi > nextlumi_)) {
00190 edm::LogInfo("BeamSpotProblemMonitor") << "analyze:: Spilt event from next lumi section!!!" << std::endl;
00191 return;
00192 }
00193
00194 BeamSpotStatus_ = 0.;
00195
00196
00197
00198 Handle<DcsStatusCollection> dcsStatus;
00199 iEvent.getByLabel("scalersRawToDigi", dcsStatus);
00200 for (int i=0;i<6;i++) dcsTk[i]=true;
00201 for (DcsStatusCollection::const_iterator dcsStatusItr = dcsStatus->begin();
00202 dcsStatusItr != dcsStatus->end(); ++dcsStatusItr) {
00203 if (!dcsStatusItr->ready(DcsStatus::BPIX)) dcsTk[0]=false;
00204 if (!dcsStatusItr->ready(DcsStatus::FPIX)) dcsTk[1]=false;
00205 if (!dcsStatusItr->ready(DcsStatus::TIBTID)) dcsTk[2]=false;
00206 if (!dcsStatusItr->ready(DcsStatus::TOB)) dcsTk[3]=false;
00207 if (!dcsStatusItr->ready(DcsStatus::TECp)) dcsTk[4]=false;
00208 if (!dcsStatusItr->ready(DcsStatus::TECm)) dcsTk[5]=false;
00209 }
00210
00211 bool AllTkOn = true;
00212 for (int i=0; i<5; i++)
00213 {
00214 if (!dcsTk[i]) {
00215 AllTkOn = false;
00216
00217 }
00218 }
00219
00220
00221
00222 edm::Handle<reco::TrackCollection> TrackCollection;
00223 iEvent.getByLabel(trkSrc_, TrackCollection);
00224 const reco::TrackCollection *tracks = TrackCollection.product();
00225 for ( reco::TrackCollection::const_iterator track = tracks->begin();track != tracks->end();++track )
00226 {
00227 if(track->pt() > 1.0)Ntracks_++;
00228 if(Ntracks_> 200) break;
00229 }
00230
00231
00232
00233
00234 Handle<BeamSpotOnlineCollection> handleScaler;
00235 iEvent.getByLabel( scalertag_, handleScaler);
00236
00237
00238 BeamSpotOnline spotOnline;
00239
00240 bool fallBackToDB=false;
00241 ALARM_ON_ = false;
00242
00243
00244 if (handleScaler->size()!=0)
00245 {
00246 spotOnline = * ( handleScaler->begin() );
00247
00248
00249 if ( spotOnline.x() == 0. &&
00250 spotOnline.y() == 0. &&
00251 spotOnline.z() == 0. &&
00252 spotOnline.width_x() == 0. &&
00253 spotOnline.width_y() == 0. )
00254 {
00255 fallBackToDB=true;
00256 }
00257 }
00258
00259
00260
00261 if(doTest_)fallBackToDB= true;
00262
00263
00264 if(AllTkOn && fallBackToDB){BeamSpotStatus_ = -1.;}
00265 if(AllTkOn && (!fallBackToDB)){BeamSpotStatus_ = 1.;}
00266
00267
00268
00269 if(AllTkOn && Ntracks_ < nCosmicTrk_)BeamSpotStatus_ = 0.;
00270
00271
00272 dbe_->setCurrentFolder(monitorName_+"FitFromScalars/");
00273
00274
00275 processed_ = true;
00276
00277 }
00278
00279
00280 void BeamSpotProblemMonitor::FillPlots(const LuminosityBlock& lumiSeg,int &lastlumi,int &nextlumi,int &nthlumi){
00281
00282 if (onlineMode_ && (nthlumi <= nextlumi)) return;
00283
00284 int currentlumi = nextlumi;
00285 lastlumi = currentlumi;
00286
00287
00288 if(BeamSpotStatus_== -1. && (lastlumi+1) == nthlumi)
00289 {BeamSpotFromDB_++;
00290 }
00291 else{
00292 BeamSpotFromDB_=0;}
00293
00294
00295 if(BeamSpotFromDB_ >= alarmONThreshold_ ){ ALARM_ON_ =true;
00296 }
00297
00298 if(BeamSpotFromDB_ > alarmOFFThreshold_ ){ ALARM_ON_ =false;
00299 BeamSpotFromDB_=0;
00300 }
00301
00302
00303
00304 if (onlineMode_)
00305 {
00306 const int countLS_bs = hs["BeamSpotStatus_lumi"]->getTH1()->GetEntries();
00307 int LSgap_bs = currentlumi/fitNLumi_ - countLS_bs;
00308 if (currentlumi%fitNLumi_ == 0)LSgap_bs--;
00309
00310
00311
00312 for (int ig = 0; ig < LSgap_bs; ig++) {
00313 hs["BeamSpotStatus_lumi"]->ShiftFillLast( 0., 0., fitNLumi_ );
00314 }
00315
00316 hs["BeamSpotStatus_lumi"]->ShiftFillLast( BeamSpotStatus_, 0. , fitNLumi_ );
00317 hs["BeamSpotStatus_lumi_all"]->setBinContent( currentlumi, BeamSpotStatus_);
00318
00319
00320 }
00321 else {
00322 hs["BeamSpotStatus_lumi"]->ShiftFillLast( 0., 0., fitNLumi_ );
00323 }
00324
00325
00326 BeamSpotError->Reset();
00327 if(ALARM_ON_)BeamSpotError->Fill(BeamSpotFromDB_);
00328
00329
00330
00331 MonitorElement* myQReport = dbe_->get(monitorName_+"FitFromScalars/BeamSpotError");
00332
00333 const QReport * BeamSpotQReport = myQReport->getQReport("BeamSpotOnlineTest");
00334
00335 if(BeamSpotQReport){
00336
00337
00338
00339 std::string qtmessage = BeamSpotQReport->getMessage() ;
00340 }
00341
00342
00343 Ntracks_= 0;
00344
00345 }
00346
00347
00348 void BeamSpotProblemMonitor::endLuminosityBlock(const LuminosityBlock& lumiSeg,
00349 const EventSetup& iSetup) {
00350 int nthlumi = lumiSeg.id().luminosityBlock();
00351 edm::LogInfo("BeamSpotProblemMonitor") << "endLuminosityBlock:: Lumi of the last event before endLuminosityBlock: " << nthlumi << endl;
00352
00353 if (onlineMode_ && nthlumi < nextlumi_) return;
00354
00355 }
00356
00357
00358 void BeamSpotProblemMonitor::endRun(const Run& r, const EventSetup& context){
00359
00360 if(debug_)edm::LogInfo("BeamSpotProblemMonitor") << "endRun:: Clearing all the Maps "<<endl;
00361
00362 BeamSpotError->Reset();
00363 }
00364
00365
00366 void BeamSpotProblemMonitor::endJob(const LuminosityBlock& lumiSeg,
00367 const EventSetup& iSetup){
00368 if (!onlineMode_) endLuminosityBlock(lumiSeg, iSetup);
00369
00370 BeamSpotError->Reset();
00371 }
00372
00373 DEFINE_FWK_MODULE(BeamSpotProblemMonitor);