Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-14 11:44:48

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;
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 BeamSpotRcdReader::~BeamSpotRcdReader() = default;
0099 
0100 //
0101 // member functions
0102 //
0103 
0104 void BeamSpotRcdReader::theBSfromDB::init() {
0105   float dummy_float = 9999.0;
0106   int dummy_int = 9999;
0107 
0108   run = dummy_int;
0109   ls = dummy_int;
0110   BSx0_ = dummy_float;
0111   BSy0_ = dummy_float;
0112   BSz0_ = dummy_float;
0113   Beamsigmaz_ = dummy_float;
0114   Beamdxdz_ = dummy_float;
0115   BeamWidthX_ = dummy_float;
0116   BeamWidthY_ = dummy_float;
0117 }
0118 
0119 // ------------ method called for each event  ------------
0120 void BeamSpotRcdReader::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0121   using namespace edm;
0122   std::ostringstream output;
0123 
0124   // initialize the ntuple
0125   theBSfromDB_.init();
0126 
0127   if (watcher_.check(iSetup)) {  // check for new IOV for this run / LS
0128 
0129     output << " for runs: " << iEvent.id().run() << " - " << iEvent.id().luminosityBlock() << std::endl;
0130 
0131     // Get BeamSpot from EventSetup:
0132     const BeamSpotObjects* mybeamspot = &iSetup.getData(beamSpotToken_);
0133 
0134     theBSfromDB_.run = iEvent.id().run();
0135     theBSfromDB_.ls = iEvent.id().luminosityBlock();
0136     theBSfromDB_.BSx0_ = mybeamspot->x();
0137     theBSfromDB_.BSy0_ = mybeamspot->y();
0138     theBSfromDB_.BSz0_ = mybeamspot->z();
0139     theBSfromDB_.Beamsigmaz_ = mybeamspot->sigmaZ();
0140     theBSfromDB_.Beamdxdz_ = mybeamspot->dxdz();
0141     theBSfromDB_.BeamWidthX_ = mybeamspot->beamWidthX();
0142     theBSfromDB_.BeamWidthY_ = mybeamspot->beamWidthY();
0143 
0144     bstree_->Fill();
0145 
0146     output << *mybeamspot << std::endl;
0147   }
0148 
0149   // Final output - either message logger or output file:
0150   if (output_.get())
0151     *output_ << output.str();
0152   else
0153     edm::LogInfo("") << output.str();
0154 }
0155 
0156 // ------------ method called once each job just before starting event loop  ------------
0157 void BeamSpotRcdReader::beginJob() {
0158   bstree_ = tFileService->make<TTree>("BSNtuple", "BeamSpot analyzer ntuple");
0159 
0160   //Tree Branches
0161   bstree_->Branch("run", &theBSfromDB_.run, "run/I");
0162   bstree_->Branch("ls", &theBSfromDB_.ls, "ls/I");
0163   bstree_->Branch("BSx0", &theBSfromDB_.BSx0_, "BSx0/F");
0164   bstree_->Branch("BSy0", &theBSfromDB_.BSy0_, "BSy0/F");
0165   bstree_->Branch("BSz0", &theBSfromDB_.BSz0_, "BSz0/F");
0166   bstree_->Branch("Beamsigmaz", &theBSfromDB_.Beamsigmaz_, "Beamsigmaz/F");
0167   bstree_->Branch("Beamdxdz", &theBSfromDB_.Beamdxdz_, "Beamdxdz/F");
0168   bstree_->Branch("BeamWidthX", &theBSfromDB_.BeamWidthX_, "BeamWidthX/F");
0169   bstree_->Branch("BeamWidthY", &theBSfromDB_.BeamWidthY_, "BeamWidthY/F");
0170 }
0171 
0172 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0173 void BeamSpotRcdReader::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0174   //The following says we do not know what parameters are allowed so do no validation
0175   // Please change this to state exactly what you do use, even if it is no parameters
0176   edm::ParameterSetDescription desc;
0177   desc.setUnknown();
0178   descriptions.addDefault(desc);
0179 }
0180 
0181 //define this as a plug-in
0182 DEFINE_FWK_MODULE(BeamSpotRcdReader);