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/BeamProfile2DBReader
0004 // Class:      BeamProfile2DBReader
0005 //
0006 /**\class BeamProfile2DBReader BeamProfile2DBReader.cc CondTools/BeamSpot/plugins/BeamProfile2DBReader.cc
0007 
0008  Description: simple emd::one::EDAnalyzer to retrieve and ntuplize SimBeamSpot data from the conditions database
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Francesco Brivio
0015 //         Created:  11 June 2023
0016 //
0017 
0018 // system include files
0019 #include <fstream>
0020 #include <memory>
0021 #include <sstream>
0022 
0023 // user include files
0024 #include "CondFormats/BeamSpotObjects/interface/SimBeamSpotObjects.h"
0025 #include "CondFormats/DataRecord/interface/SimBeamSpotObjectsRcd.h"
0026 #include "FWCore/Framework/interface/ESHandle.h"
0027 #include "FWCore/Framework/interface/ESWatcher.h"
0028 #include "FWCore/Framework/interface/Event.h"
0029 #include "FWCore/Framework/interface/EventSetup.h"
0030 #include "FWCore/Framework/interface/Frameworkfwd.h"
0031 #include "FWCore/Framework/interface/MakerMacros.h"
0032 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0034 
0035 // For ROOT
0036 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0037 #include "FWCore/ServiceRegistry/interface/Service.h"
0038 #include <TTree.h>
0039 
0040 //
0041 // class declaration
0042 //
0043 
0044 class BeamProfile2DBReader : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0045 public:
0046   explicit BeamProfile2DBReader(const edm::ParameterSet&);
0047   ~BeamProfile2DBReader() override = default;
0048 
0049   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0050 
0051 private:
0052   void beginJob() override;
0053   void analyze(const edm::Event&, const edm::EventSetup&) override;
0054 
0055   struct TheBSfromDB {
0056     int run;
0057     int ls;
0058     double fX0, fY0, fZ0;
0059     double fMeanX, fMeanY, fMeanZ;
0060     double fSigmaX, fSigmaY, fSigmaZ;
0061     double fbetastar, femittance;
0062     double fPhi, fAlpha;
0063     double fTimeOffset;
0064     void init();
0065   } theBSfromDB_;
0066 
0067   const edm::ESGetToken<SimBeamSpotObjects, SimBeamSpotObjectsRcd> beamSpotToken_;
0068   edm::Service<TFileService> tFileService;
0069   TTree* bstree_;
0070 
0071   // ----------member data ---------------------------
0072   edm::ESWatcher<SimBeamSpotObjectsRcd> watcher_;
0073   std::unique_ptr<std::ofstream> output_;
0074 };
0075 
0076 //
0077 // constructors and destructor
0078 //
0079 BeamProfile2DBReader::BeamProfile2DBReader(const edm::ParameterSet& iConfig)
0080     : beamSpotToken_(esConsumes()), bstree_(nullptr) {
0081   //now do what ever initialization is needed
0082   usesResource("TFileService");
0083   std::string fileName(iConfig.getUntrackedParameter<std::string>("rawFileName"));
0084   if (!fileName.empty()) {
0085     output_ = std::make_unique<std::ofstream>(fileName.c_str());
0086     if (!output_->good()) {
0087       edm::LogError("IOproblem") << "Could not open output file " << fileName << ".";
0088       output_.reset();
0089     }
0090   }
0091 }
0092 
0093 //
0094 // member functions
0095 //
0096 
0097 void BeamProfile2DBReader::TheBSfromDB::init() {
0098   float dummy_double = 0.0;
0099   int dummy_int = 0;
0100 
0101   run = dummy_int;
0102   ls = dummy_int;
0103   fX0 = dummy_double;
0104   fY0 = dummy_double;
0105   fZ0 = dummy_double;
0106   fMeanX = dummy_double;
0107   fMeanY = dummy_double;
0108   fMeanZ = dummy_double;
0109   fSigmaX = dummy_double;
0110   fSigmaY = dummy_double;
0111   fSigmaZ = dummy_double;
0112   fbetastar = dummy_double;
0113   femittance = dummy_double;
0114   fPhi = dummy_double;
0115   fAlpha = dummy_double;
0116   fTimeOffset = dummy_double;
0117 }
0118 
0119 // ------------ method called for each event  ------------
0120 void BeamProfile2DBReader::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 SimBeamSpot from EventSetup:
0132     const SimBeamSpotObjects* mybeamspot = &iSetup.getData(beamSpotToken_);
0133 
0134     theBSfromDB_.run = iEvent.id().run();
0135     theBSfromDB_.ls = iEvent.id().luminosityBlock();
0136     theBSfromDB_.fX0 = mybeamspot->x();
0137     theBSfromDB_.fY0 = mybeamspot->y();
0138     theBSfromDB_.fZ0 = mybeamspot->z();
0139     theBSfromDB_.fMeanX = mybeamspot->meanX();
0140     theBSfromDB_.fMeanY = mybeamspot->meanY();
0141     theBSfromDB_.fMeanZ = mybeamspot->meanZ();
0142     theBSfromDB_.fSigmaX = mybeamspot->sigmaX();
0143     theBSfromDB_.fSigmaY = mybeamspot->sigmaY();
0144     theBSfromDB_.fSigmaZ = mybeamspot->sigmaZ();
0145     theBSfromDB_.fbetastar = mybeamspot->betaStar();
0146     theBSfromDB_.femittance = mybeamspot->emittance();
0147     theBSfromDB_.fPhi = mybeamspot->phi();
0148     theBSfromDB_.fAlpha = mybeamspot->alpha();
0149     theBSfromDB_.fTimeOffset = mybeamspot->timeOffset();
0150     bstree_->Fill();
0151     output << *mybeamspot << std::endl;
0152   }
0153 
0154   // Final output - either message logger or output file:
0155   if (output_.get())
0156     *output_ << output.str();
0157   else
0158     edm::LogInfo("BeamProfile2DBReader") << output.str();
0159 }
0160 
0161 // ------------ method called once each job just before starting event loop  ------------
0162 void BeamProfile2DBReader::beginJob() {
0163   bstree_ = tFileService->make<TTree>("BSNtuple", "SimBeamSpot analyzer ntuple");
0164 
0165   //Tree Branches
0166   bstree_->Branch("run", &theBSfromDB_.run, "run/I");
0167   bstree_->Branch("ls", &theBSfromDB_.ls, "ls/I");
0168   bstree_->Branch("BSx0", &theBSfromDB_.fX0, "BSx0/F");
0169   bstree_->Branch("BSy0", &theBSfromDB_.fY0, "BSy0/F");
0170   bstree_->Branch("BSz0", &theBSfromDB_.fZ0, "BSz0/F");
0171   bstree_->Branch("BSmeanX", &theBSfromDB_.fMeanX, "BSmeanX/F");
0172   bstree_->Branch("BSmeanY", &theBSfromDB_.fMeanY, "BSmeanY/F");
0173   bstree_->Branch("BSmeanZ", &theBSfromDB_.fMeanZ, "BSmeanZ/F");
0174   bstree_->Branch("Beamsigmax", &theBSfromDB_.fSigmaX, "Beamsigmax/F");
0175   bstree_->Branch("Beamsigmay", &theBSfromDB_.fSigmaY, "Beamsigmay/F");
0176   bstree_->Branch("Beamsigmaz", &theBSfromDB_.fSigmaZ, "Beamsigmaz/F");
0177   bstree_->Branch("BetaStar", &theBSfromDB_.fbetastar, "BetaStar/F");
0178   bstree_->Branch("Emittance", &theBSfromDB_.femittance, "Emittance/F");
0179   bstree_->Branch("Phi", &theBSfromDB_.fPhi, "Phi/F");
0180   bstree_->Branch("Alpha", &theBSfromDB_.fAlpha, "Alpha/F");
0181   bstree_->Branch("TimeOffset", &theBSfromDB_.fTimeOffset, "TimeOffset/F");
0182 }
0183 
0184 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0185 void BeamProfile2DBReader::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0186   edm::ParameterSetDescription desc;
0187   desc.addUntracked<std::string>("rawFileName", {});
0188   descriptions.addWithDefaultLabel(desc);
0189 }
0190 
0191 //define this as a plug-in
0192 DEFINE_FWK_MODULE(BeamProfile2DBReader);