CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DQM/BeamMonitor/plugins/BeamSpotProblemMonitor.cc

Go to the documentation of this file.
00001 /*
00002  * \file BeamSpotProblemMonitor.cc
00003  * \author Sushil S. Chauhan/UC Davis
00004  *        
00005  * $Date: 2012/12/10 05:07:33 $
00006  * $Revision: 1.3 $
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 // constructors and destructor
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);//40 LS X 23"
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   // create and cd into new folder
00089   dbe_->setCurrentFolder(monitorName_+"FitFromScalars");
00090 
00091   /* S.Dutta : Commenting out these variables are not used and giving error with "-Werror=unused-variable" option
00092   int nbins = alarmOFFThreshold_; 
00093   double hiRange = (alarmOFFThreshold_+0.5);*/
00094 
00095   const int nvar_ = 1;
00096   string coord[nvar_] = {"BeamSpotStatus"};
00097   /* S.Dutta : Commenting out these variables are not used and giving error with "-Werror=unused-variable" option
00098   string label[nvar_] = {"BeamSpotStatus "};*/
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         }//create histo
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     // Checking TK status
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      //If tracker is ON and collision is going on then must be few track ther
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   // get scalar collection and BeamSpot
00234   Handle<BeamSpotOnlineCollection> handleScaler;
00235   iEvent.getByLabel( scalertag_, handleScaler);
00236      
00237    // beam spot scalar object
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     // check if we have a valid beam spot fit result from online DQM thrugh scalars
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    //For testing set it false for every LSs
00261    if(doTest_)fallBackToDB= true;
00262  
00263     //based on last event of this lumi only as it overwrite it
00264     if(AllTkOn && fallBackToDB){BeamSpotStatus_ = -1.;}    //i.e,from DB
00265     if(AllTkOn && (!fallBackToDB)){BeamSpotStatus_ = 1.;}  //i.e,from online DQM
00266 
00267 
00268     //when collision at least few tracks should be there otherwise it give false ALARM  
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     //Chcek status and if lumi are in succession when fall to DB
00288    if(BeamSpotStatus_== -1.  && (lastlumi+1) == nthlumi)
00289                              {BeamSpotFromDB_++;
00290                                  }
00291                                  else{
00292                                         BeamSpotFromDB_=0;} //if not in succesion or status is ok then set zero
00293 
00294 
00295    if(BeamSpotFromDB_ >= alarmONThreshold_ ){ ALARM_ON_ =true; //set the audio alarm true after N successive LSs
00296                                            }
00297 
00298    if(BeamSpotFromDB_ > alarmOFFThreshold_ ){ ALARM_ON_ =false; //set the audio alarm true after 10 successive LSs
00299                                                BeamSpotFromDB_=0; //reset it for new incident
00300                                              }
00301 
00302 
00303 
00304  if (onlineMode_) 
00305  { // filling LS gap For status plot
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     // filling previous fits if LS gap ever exists
00312     for (int ig = 0; ig < LSgap_bs; ig++) {
00313       hs["BeamSpotStatus_lumi"]->ShiftFillLast( 0., 0., fitNLumi_ );//x0 , x0err, fitNLumi_;  see DQMCore....
00314      }
00315 
00316      hs["BeamSpotStatus_lumi"]->ShiftFillLast( BeamSpotStatus_, 0. , fitNLumi_ ); //BeamSpotStatus_ =>0. (no collision, no tracks); =>1 (OK from scaler), =>-1 (No scalar results) 
00317      hs["BeamSpotStatus_lumi_all"]->setBinContent( currentlumi, BeamSpotStatus_);
00318 
00319 
00320  }//onlineMode_
00321  else { 
00322       hs["BeamSpotStatus_lumi"]->ShiftFillLast( 0., 0., fitNLumi_ );
00323      }
00324 
00325       //Reset it here for next lumi
00326       BeamSpotError->Reset();
00327       if(ALARM_ON_)BeamSpotError->Fill(BeamSpotFromDB_);
00328 
00329 
00330     //Get quality report
00331      MonitorElement* myQReport = dbe_->get(monitorName_+"FitFromScalars/BeamSpotError");
00332 
00333      const QReport * BeamSpotQReport = myQReport->getQReport("BeamSpotOnlineTest");  
00334 
00335     if(BeamSpotQReport){  
00336                           /* S.Dutta : Commenting out these variables are not used and giving error with "-Werror=unused-variable" option
00337                            float qtresult = BeamSpotQReport->getQTresult();
00338                            int qtstatus   = BeamSpotQReport->getStatus() ; // get QT status value (see table below) */
00339                            std::string qtmessage = BeamSpotQReport->getMessage() ; // get the whole QT result message
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      //Reset it end of job
00362      BeamSpotError->Reset();
00363 }
00364 
00365 //--------------------------------------------------------
00366 void BeamSpotProblemMonitor::endJob(const LuminosityBlock& lumiSeg,
00367                          const EventSetup& iSetup){
00368   if (!onlineMode_) endLuminosityBlock(lumiSeg, iSetup);
00369      //Reset it end of job
00370      BeamSpotError->Reset();
00371 }
00372 
00373 DEFINE_FWK_MODULE(BeamSpotProblemMonitor);