CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/RecoVertex/BeamSpotProducer/plugins/BeamSpotOnlineProducer.cc

Go to the documentation of this file.
00001 
00002 #include "RecoVertex/BeamSpotProducer/interface/BeamSpotOnlineProducer.h"
00003 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00004 #include "DataFormats/Scalers/interface/BeamSpotOnline.h"
00005 
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007 #include "FWCore/Framework/interface/EventSetup.h"
00008 #include "FWCore/Framework/interface/IOVSyncValue.h"
00009 #include "CondFormats/DataRecord/interface/BeamSpotObjectsRcd.h"
00010 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
00011 
00012 #include "FWCore/Framework/interface/MakerMacros.h"
00013 
00014 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerEvmReadoutRecord.h"
00015 
00016 using namespace edm;
00017 
00018 
00019 BeamSpotOnlineProducer::BeamSpotOnlineProducer(const ParameterSet& iconf)
00020 {
00021   
00022   if (iconf.exists("label")){
00023     edm::LogError("Deprecation")<<"please migrate to using src = cms.InputTag instead of label = cms.InputTag";
00024     scalertag_ = iconf.getParameter<InputTag>("label");
00025   }
00026   else
00027     scalertag_ = iconf.getParameter<InputTag>("src");
00028 
00029   changeFrame_ = iconf.getParameter<bool>("changeToCMSCoordinates");
00030 
00031   theMaxR2 = iconf.getParameter<double>("maxRadius");
00032   theMaxR2*=theMaxR2;
00033   theMaxZ = iconf.getParameter<double>("maxZ");
00034 
00035   theSetSigmaZ = iconf.getParameter<double>("setSigmaZ");
00036   
00037   thel1GtEvmReadoutRecordTag = iconf.getParameter<InputTag>("gtEvmLabel");
00038   
00039   produces<reco::BeamSpot>();
00040 
00041 } 
00042 
00043 BeamSpotOnlineProducer::~BeamSpotOnlineProducer() {}
00044 
00045 void
00046 BeamSpotOnlineProducer::produce(Event& iEvent, const EventSetup& iSetup)
00047 {
00048   //shout MODE only in stable beam
00049   bool shoutMODE=false;
00050   edm::Handle<L1GlobalTriggerEvmReadoutRecord> gtEvmReadoutRecord;
00051   if (iEvent.getByLabel(thel1GtEvmReadoutRecordTag, gtEvmReadoutRecord)){
00052     const boost::uint16_t beamModeValue = (gtEvmReadoutRecord->gtfeWord()).beamMode();
00053     if (beamModeValue == 11) shoutMODE=true;
00054   }
00055   else{
00056     shoutMODE=true;
00057   }
00058 
00059   // get scalar collection
00060   Handle<BeamSpotOnlineCollection> handleScaler;
00061   iEvent.getByLabel( scalertag_, handleScaler);
00062 
00063   // beam spot scalar object
00064   BeamSpotOnline spotOnline;
00065 
00066   // product is a reco::BeamSpot object
00067   std::auto_ptr<reco::BeamSpot> result(new reco::BeamSpot);
00068   
00069   reco::BeamSpot aSpot;
00070 
00071   bool fallBackToDB=false;
00072   if (handleScaler->size()!=0){
00073     // get one element
00074     spotOnline = * ( handleScaler->begin() );
00075     
00076     // in case we need to switch to LHC reference frame
00077     // ignore for the moment rotations, and translations
00078     double f = 1.;
00079     if (changeFrame_) f = -1.;
00080     
00081     reco::BeamSpot::Point apoint( f* spotOnline.x(), spotOnline.y(), f* spotOnline.z() );
00082     
00083     reco::BeamSpot::CovarianceMatrix matrix;
00084     matrix(0,0) = spotOnline.err_x()*spotOnline.err_x();
00085     matrix(1,1) = spotOnline.err_y()*spotOnline.err_y();
00086     matrix(2,2) = spotOnline.err_z()*spotOnline.err_z();
00087     matrix(3,3) = spotOnline.err_sigma_z()*spotOnline.err_sigma_z();
00088     
00089     double sigmaZ = spotOnline.sigma_z();
00090     if (theSetSigmaZ>0)
00091       sigmaZ = theSetSigmaZ;
00092     
00093     aSpot = reco::BeamSpot( apoint,
00094                             sigmaZ,
00095                           spotOnline.dxdz(),
00096                             f* spotOnline.dydz(),
00097                             spotOnline.width_x(),
00098                             matrix);
00099     
00100     aSpot.setBeamWidthY( spotOnline.width_y() );
00101     aSpot.setEmittanceX( 0. );
00102     aSpot.setEmittanceY( 0. );
00103     aSpot.setbetaStar( 0.) ;
00104     aSpot.setType( reco::BeamSpot::LHC ); // flag value from scalars
00105     
00106     // check if we have a valid beam spot fit result from online DQM
00107     if ( spotOnline.x() == 0 &&
00108          spotOnline.y() == 0 &&
00109          spotOnline.z() == 0 &&
00110          spotOnline.width_x() == 0 &&
00111          spotOnline.width_y() == 0 ) 
00112       {
00113         if (shoutMODE){
00114           edm::LogWarning("BeamSpotFromDB") 
00115           << "Online Beam Spot producer falls back to DB value because the scaler values are zero ";
00116         }
00117         fallBackToDB=true;
00118       }
00119     double r2=spotOnline.x()*spotOnline.x() + spotOnline.y()*spotOnline.y();
00120     if (fabs(spotOnline.z())>=theMaxZ || r2>=theMaxR2){
00121       if (shoutMODE){
00122         edm::LogError("BeamSpotFromDB") 
00123           << "Online Beam Spot producer falls back to DB value because the scaler values are too big to be true :"
00124           <<spotOnline.x()<<" "<<spotOnline.y()<<" "<<spotOnline.z();
00125       }
00126       fallBackToDB=true;
00127     }
00128   }
00129   else{
00130     //empty online beamspot collection: FED data was empty
00131     //the error should probably have been send at unpacker level
00132     fallBackToDB=true;
00133   }
00134       
00135   if (fallBackToDB){
00136 
00137     edm::ESHandle< BeamSpotObjects > beamhandle;
00138     iSetup.get<BeamSpotObjectsRcd>().get(beamhandle);
00139     const BeamSpotObjects *spotDB = beamhandle.product();
00140 
00141     // translate from BeamSpotObjects to reco::BeamSpot
00142     reco::BeamSpot::Point apoint( spotDB->GetX(), spotDB->GetY(), spotDB->GetZ() );
00143   
00144     reco::BeamSpot::CovarianceMatrix matrix;
00145     for ( int i=0; i<7; ++i ) {
00146       for ( int j=0; j<7; ++j ) {
00147         matrix(i,j) = spotDB->GetCovariance(i,j);
00148       }
00149     }
00150   
00151     // this assume beam width same in x and y
00152     aSpot = reco::BeamSpot( apoint,
00153                             spotDB->GetSigmaZ(),
00154                             spotDB->Getdxdz(),
00155                             spotDB->Getdydz(),
00156                             spotDB->GetBeamWidthX(),
00157                             matrix );
00158     aSpot.setBeamWidthY( spotDB->GetBeamWidthY() );
00159     aSpot.setEmittanceX( spotDB->GetEmittanceX() );
00160     aSpot.setEmittanceY( spotDB->GetEmittanceY() );
00161     aSpot.setbetaStar( spotDB->GetBetaStar() );
00162     aSpot.setType( reco::BeamSpot::Tracker );
00163 
00164   }
00165   
00166   *result = aSpot;
00167 
00168   iEvent.put(result);
00169 
00170 }
00171 
00172 DEFINE_FWK_MODULE(BeamSpotOnlineProducer);