Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:42

0001 // -*- C++ -*-
0002 //
0003 // Package:    CondTools/BeamSpot
0004 // Class:      BeamSpotOnlineRecordsReader
0005 //
0006 /**\class BeamSpotOnlineRecordsReader BeamSpotOnlineRecordsReader.cc CondTools/BeamSpot/plugins/BeamSpotOnlineRecordsReader.cc
0007 
0008  Description: EDAnalyzer to read the BeamSpotOnlineHLTObjectsRcd or BeamSpotOnlineLegacyObjectsRcd and dump it into a txt and root file
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Francesco Brivio
0015 //         Created:  Tue, 11 Feb 2020 08:39:14 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 #include <sstream>
0022 #include <fstream>
0023 
0024 // user include files
0025 #include "FWCore/Framework/interface/Frameworkfwd.h"
0026 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0027 
0028 #include "FWCore/Framework/interface/Event.h"
0029 #include "FWCore/Framework/interface/MakerMacros.h"
0030 
0031 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0032 #include "FWCore/Utilities/interface/InputTag.h"
0033 #include "FWCore/Framework/interface/EventSetup.h"
0034 #include "FWCore/Framework/interface/ESHandle.h"
0035 #include "FWCore/Framework/interface/ESWatcher.h"
0036 
0037 #include "CondFormats/DataRecord/interface/BeamSpotOnlineLegacyObjectsRcd.h"
0038 #include "CondFormats/DataRecord/interface/BeamSpotOnlineHLTObjectsRcd.h"
0039 #include "CondFormats/BeamSpotObjects/interface/BeamSpotOnlineObjects.h"
0040 
0041 // For ROOT
0042 #include "FWCore/ServiceRegistry/interface/Service.h"
0043 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0044 #include <TTree.h>
0045 
0046 //
0047 // class declaration
0048 //
0049 
0050 class BeamSpotOnlineRecordsReader : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0051 public:
0052   explicit BeamSpotOnlineRecordsReader(const edm::ParameterSet&);
0053   ~BeamSpotOnlineRecordsReader() override;
0054 
0055   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0056 
0057 private:
0058   void beginJob() override;
0059   void analyze(const edm::Event&, const edm::EventSetup&) override;
0060   template <class Record>
0061   void dump(const edm::Event&, const edm::EventSetup&, const edm::ESGetToken<BeamSpotOnlineObjects, Record>&);
0062 
0063   struct theBSOfromDB {
0064     int ls;
0065     int run;
0066     float BSx0_;
0067     float BSy0_;
0068     float BSz0_;
0069     float Beamsigmaz_;
0070     float Beamdxdz_;
0071     float BeamWidthX_;
0072     float BeamWidthY_;
0073     int lastAnalyzedLumi_;
0074     int lastAnalyzedRun_;
0075     int lastAnalyzedFill_;
0076     void init();
0077   } theBSOfromDB_;
0078 
0079   edm::Service<TFileService> tFileService;
0080   TTree* bstree_;
0081 
0082   const edm::ESGetToken<BeamSpotOnlineObjects, BeamSpotOnlineHLTObjectsRcd> hltToken;
0083   const edm::ESGetToken<BeamSpotOnlineObjects, BeamSpotOnlineLegacyObjectsRcd> legacyToken;
0084 
0085   // ----------member data ---------------------------
0086   bool isHLT_;
0087   edm::ESWatcher<BeamSpotOnlineHLTObjectsRcd> hlt_watcher_;
0088   edm::ESWatcher<BeamSpotOnlineLegacyObjectsRcd> legacy_watcher_;
0089   std::unique_ptr<std::ofstream> output_;
0090 };
0091 
0092 //
0093 // constructors and destructor
0094 //
0095 BeamSpotOnlineRecordsReader::BeamSpotOnlineRecordsReader(const edm::ParameterSet& iConfig)
0096     : bstree_(nullptr), hltToken(esConsumes()), legacyToken(esConsumes()) {
0097   //now do what ever initialization is needed
0098   isHLT_ = iConfig.getParameter<bool>("isHLT");
0099   usesResource("TFileService");
0100   std::string fileName(iConfig.getUntrackedParameter<std::string>("rawFileName"));
0101   if (!fileName.empty()) {
0102     output_ = std::make_unique<std::ofstream>(fileName.c_str());
0103     if (!output_->good()) {
0104       edm::LogError("IOproblem") << "Could not open output file " << fileName << ".";
0105       output_.reset();
0106     }
0107   }
0108 }
0109 
0110 BeamSpotOnlineRecordsReader::~BeamSpotOnlineRecordsReader() = default;
0111 
0112 //
0113 // member functions
0114 //
0115 
0116 void BeamSpotOnlineRecordsReader::theBSOfromDB::init() {
0117   float dummy_float = -999.0;
0118   int dummy_int = -999;
0119 
0120   run = dummy_int;
0121   ls = dummy_int;
0122   BSx0_ = dummy_float;
0123   BSy0_ = dummy_float;
0124   BSz0_ = dummy_float;
0125   Beamsigmaz_ = dummy_float;
0126   Beamdxdz_ = dummy_float;
0127   BeamWidthX_ = dummy_float;
0128   BeamWidthY_ = dummy_float;
0129   lastAnalyzedLumi_ = dummy_int;
0130   lastAnalyzedRun_ = dummy_int;
0131   lastAnalyzedFill_ = dummy_int;
0132 }
0133 
0134 // ------------ method called for each event  ------------
0135 void BeamSpotOnlineRecordsReader::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0136   using namespace edm;
0137 
0138   if (isHLT_) {
0139     if (hlt_watcher_.check(iSetup)) {
0140       dump<BeamSpotOnlineHLTObjectsRcd>(iEvent, iSetup, hltToken);
0141     }  // check for new IOV for this run / LS
0142   } else {
0143     if (legacy_watcher_.check(iSetup)) {
0144       dump<BeamSpotOnlineLegacyObjectsRcd>(iEvent, iSetup, legacyToken);
0145     }  // check for new IOV for this run / LS
0146   }
0147 }
0148 
0149 template <class Record>
0150 void BeamSpotOnlineRecordsReader::dump(const edm::Event& iEvent,
0151                                        const edm::EventSetup& iSetup,
0152                                        const edm::ESGetToken<BeamSpotOnlineObjects, Record>& token) {
0153   std::ostringstream output;
0154   // initialize the ntuple
0155   theBSOfromDB_.init();
0156   output << " for runs: " << iEvent.id().run() << " - " << iEvent.id().luminosityBlock() << std::endl;
0157 
0158   // Get BeamSpot from EventSetup:
0159   const BeamSpotOnlineObjects* mybeamspot = &iSetup.getData(token);
0160 
0161   theBSOfromDB_.run = iEvent.id().run();
0162   theBSOfromDB_.ls = iEvent.id().luminosityBlock();
0163   theBSOfromDB_.BSx0_ = mybeamspot->x();
0164   theBSOfromDB_.BSy0_ = mybeamspot->y();
0165   theBSOfromDB_.BSz0_ = mybeamspot->z();
0166   theBSOfromDB_.Beamsigmaz_ = mybeamspot->sigmaZ();
0167   theBSOfromDB_.Beamdxdz_ = mybeamspot->dxdz();
0168   theBSOfromDB_.BeamWidthX_ = mybeamspot->beamWidthX();
0169   theBSOfromDB_.BeamWidthY_ = mybeamspot->beamWidthY();
0170   theBSOfromDB_.lastAnalyzedLumi_ = mybeamspot->lastAnalyzedLumi();
0171   theBSOfromDB_.lastAnalyzedRun_ = mybeamspot->lastAnalyzedRun();
0172   theBSOfromDB_.lastAnalyzedFill_ = mybeamspot->lastAnalyzedFill();
0173 
0174   bstree_->Fill();
0175 
0176   output << *mybeamspot << std::endl;
0177 
0178   // Final output - either message logger or output file:
0179   if (output_.get()) {
0180     *output_ << output.str();
0181   } else {
0182     edm::LogInfo("BeamSpotOnlineRecordsReader") << output.str();
0183   }
0184 }
0185 
0186 // ------------ method called once each job just before starting event loop  ------------
0187 void BeamSpotOnlineRecordsReader::beginJob() {
0188   bstree_ = tFileService->make<TTree>("BSONtuple", "BeamSpotOnline analyzer ntuple");
0189 
0190   //Tree Branches
0191   bstree_->Branch("run", &theBSOfromDB_.run, "run/I");
0192   bstree_->Branch("ls", &theBSOfromDB_.ls, "ls/I");
0193   bstree_->Branch("BSx0", &theBSOfromDB_.BSx0_, "BSx0/F");
0194   bstree_->Branch("BSy0", &theBSOfromDB_.BSy0_, "BSy0/F");
0195   bstree_->Branch("BSz0", &theBSOfromDB_.BSz0_, "BSz0/F");
0196   bstree_->Branch("Beamsigmaz", &theBSOfromDB_.Beamsigmaz_, "Beamsigmaz/F");
0197   bstree_->Branch("Beamdxdz", &theBSOfromDB_.Beamdxdz_, "Beamdxdz/F");
0198   bstree_->Branch("BeamWidthX", &theBSOfromDB_.BeamWidthX_, "BeamWidthX/F");
0199   bstree_->Branch("BeamWidthY", &theBSOfromDB_.BeamWidthY_, "BeamWidthY/F");
0200   bstree_->Branch("LastAnalyzedLumi", &theBSOfromDB_.lastAnalyzedLumi_, "LastAnalyzedLumi/I");
0201   bstree_->Branch("LastAnalyzedRun", &theBSOfromDB_.lastAnalyzedRun_, "LastAnalyzedRun/I");
0202   bstree_->Branch("LastAnalyzedFill", &theBSOfromDB_.lastAnalyzedFill_, "LastAnalyzedFill/I");
0203 }
0204 
0205 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0206 void BeamSpotOnlineRecordsReader::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0207   edm::ParameterSetDescription desc;
0208   desc.add<bool>("isHLT", true);
0209   desc.addUntracked<std::string>("rawFileName", "");
0210   descriptions.addDefault(desc);
0211 }
0212 
0213 //define this as a plug-in
0214 DEFINE_FWK_MODULE(BeamSpotOnlineRecordsReader);