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:      BeamSpotRcdReader
0005 //
0006 /**\class BeamSpotRcdReader BeamSpotRcdReader.cc CondTools/BeamSpot/plugins/BeamSpotRcdReader.cc
0007 
0008  Description: simple emd::one::EDAnalyzer to retrieve and ntuplize BeamSpot data from the conditions database
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Marco Musich
0015 //         Created:  Tue, 18 Oct 2016 11:00:44 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0025 
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028 #include "FWCore/Framework/interface/EventSetup.h"
0029 #include "FWCore/Framework/interface/ESHandle.h"
0030 #include "FWCore/Framework/interface/ESWatcher.h"
0031 
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 #include "CondFormats/DataRecord/interface/BeamSpotObjectsRcd.h"
0034 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
0035 
0036 // For ROOT
0037 #include "FWCore/ServiceRegistry/interface/Service.h"
0038 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0039 #include <TTree.h>
0040 
0041 #include <sstream>
0042 #include <fstream>
0043 
0044 //
0045 // class declaration
0046 //
0047 
0048 class BeamSpotRcdReader : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0049 public:
0050   explicit BeamSpotRcdReader(const edm::ParameterSet&);
0051   ~BeamSpotRcdReader() override = default;
0052 
0053   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0054 
0055 private:
0056   void beginJob() override;
0057   void analyze(const edm::Event&, const edm::EventSetup&) override;
0058 
0059   struct theBSfromDB {
0060     int run;
0061     int ls;
0062     float BSx0_;
0063     float BSy0_;
0064     float BSz0_;
0065     float Beamsigmaz_;
0066     float Beamdxdz_;
0067     float BeamWidthX_;
0068     float BeamWidthY_;
0069     void init();
0070   } theBSfromDB_;
0071 
0072   const edm::ESGetToken<BeamSpotObjects, BeamSpotObjectsRcd> beamSpotToken_;
0073   edm::Service<TFileService> tFileService;
0074   TTree* bstree_;
0075 
0076   // ----------member data ---------------------------
0077   edm::ESWatcher<BeamSpotObjectsRcd> watcher_;
0078   std::unique_ptr<std::ofstream> output_;
0079 };
0080 
0081 //
0082 // constructors and destructor
0083 //
0084 BeamSpotRcdReader::BeamSpotRcdReader(const edm::ParameterSet& iConfig)
0085     : beamSpotToken_(esConsumes()), bstree_(nullptr) {
0086   //now do what ever initialization is needed
0087   usesResource("TFileService");
0088   std::string fileName(iConfig.getUntrackedParameter<std::string>("rawFileName"));
0089   if (!fileName.empty()) {
0090     output_ = std::make_unique<std::ofstream>(fileName.c_str());
0091     if (!output_->good()) {
0092       edm::LogError("IOproblem") << "Could not open output file " << fileName << ".";
0093       output_.reset();
0094     }
0095   }
0096 }
0097 
0098 //
0099 // member functions
0100 //
0101 
0102 void BeamSpotRcdReader::theBSfromDB::init() {
0103   float dummy_float = 9999.0;
0104   int dummy_int = 9999;
0105 
0106   run = dummy_int;
0107   ls = dummy_int;
0108   BSx0_ = dummy_float;
0109   BSy0_ = dummy_float;
0110   BSz0_ = dummy_float;
0111   Beamsigmaz_ = dummy_float;
0112   Beamdxdz_ = dummy_float;
0113   BeamWidthX_ = dummy_float;
0114   BeamWidthY_ = dummy_float;
0115 }
0116 
0117 // ------------ method called for each event  ------------
0118 void BeamSpotRcdReader::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0119   using namespace edm;
0120   std::ostringstream output;
0121 
0122   // initialize the ntuple
0123   theBSfromDB_.init();
0124 
0125   if (watcher_.check(iSetup)) {  // check for new IOV for this run / LS
0126 
0127     output << " for runs: " << iEvent.id().run() << " - " << iEvent.id().luminosityBlock() << std::endl;
0128 
0129     // Get BeamSpot from EventSetup:
0130     const BeamSpotObjects* mybeamspot = &iSetup.getData(beamSpotToken_);
0131 
0132     theBSfromDB_.run = iEvent.id().run();
0133     theBSfromDB_.ls = iEvent.id().luminosityBlock();
0134     theBSfromDB_.BSx0_ = mybeamspot->x();
0135     theBSfromDB_.BSy0_ = mybeamspot->y();
0136     theBSfromDB_.BSz0_ = mybeamspot->z();
0137     theBSfromDB_.Beamsigmaz_ = mybeamspot->sigmaZ();
0138     theBSfromDB_.Beamdxdz_ = mybeamspot->dxdz();
0139     theBSfromDB_.BeamWidthX_ = mybeamspot->beamWidthX();
0140     theBSfromDB_.BeamWidthY_ = mybeamspot->beamWidthY();
0141 
0142     bstree_->Fill();
0143 
0144     output << *mybeamspot << std::endl;
0145   }
0146 
0147   // Final output - either message logger or output file:
0148   if (output_.get())
0149     *output_ << output.str();
0150   else
0151     edm::LogInfo("BeamSpotRcdReader") << output.str();
0152 }
0153 
0154 // ------------ method called once each job just before starting event loop  ------------
0155 void BeamSpotRcdReader::beginJob() {
0156   bstree_ = tFileService->make<TTree>("BSNtuple", "BeamSpot analyzer ntuple");
0157 
0158   //Tree Branches
0159   bstree_->Branch("run", &theBSfromDB_.run, "run/I");
0160   bstree_->Branch("ls", &theBSfromDB_.ls, "ls/I");
0161   bstree_->Branch("BSx0", &theBSfromDB_.BSx0_, "BSx0/F");
0162   bstree_->Branch("BSy0", &theBSfromDB_.BSy0_, "BSy0/F");
0163   bstree_->Branch("BSz0", &theBSfromDB_.BSz0_, "BSz0/F");
0164   bstree_->Branch("Beamsigmaz", &theBSfromDB_.Beamsigmaz_, "Beamsigmaz/F");
0165   bstree_->Branch("Beamdxdz", &theBSfromDB_.Beamdxdz_, "Beamdxdz/F");
0166   bstree_->Branch("BeamWidthX", &theBSfromDB_.BeamWidthX_, "BeamWidthX/F");
0167   bstree_->Branch("BeamWidthY", &theBSfromDB_.BeamWidthY_, "BeamWidthY/F");
0168 }
0169 
0170 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0171 void BeamSpotRcdReader::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0172   edm::ParameterSetDescription desc;
0173   desc.addUntracked<std::string>("rawFileName", {});
0174   descriptions.addWithDefaultLabel(desc);
0175 }
0176 
0177 //define this as a plug-in
0178 DEFINE_FWK_MODULE(BeamSpotRcdReader);