CMS 3D CMS Logo

BeamProfile2DBReader.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondTools/BeamProfile2DBReader
4 // Class: BeamProfile2DBReader
5 //
13 //
14 // Original Author: Francesco Brivio
15 // Created: 11 June 2023
16 //
17 
18 // system include files
19 #include <fstream>
20 #include <memory>
21 #include <sstream>
22 
23 // user include files
34 
35 // For ROOT
38 #include <TTree.h>
39 
40 //
41 // class declaration
42 //
43 
44 class BeamProfile2DBReader : public edm::one::EDAnalyzer<edm::one::SharedResources> {
45 public:
46  explicit BeamProfile2DBReader(const edm::ParameterSet&);
47  ~BeamProfile2DBReader() override = default;
48 
49  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
50 
51 private:
52  void beginJob() override;
53  void analyze(const edm::Event&, const edm::EventSetup&) override;
54 
55  struct TheBSfromDB {
56  int run;
57  int ls;
58  double fX0, fY0, fZ0;
59  double fSigmaZ;
61  double fPhi, fAlpha;
62  double fTimeOffset;
63  void init();
64  } theBSfromDB_;
65 
68  TTree* bstree_;
69 
70  // ----------member data ---------------------------
72  std::unique_ptr<std::ofstream> output_;
73 };
74 
75 //
76 // constructors and destructor
77 //
79  : beamSpotToken_(esConsumes()), bstree_(nullptr) {
80  //now do what ever initialization is needed
81  usesResource("TFileService");
82  std::string fileName(iConfig.getUntrackedParameter<std::string>("rawFileName"));
83  if (!fileName.empty()) {
84  output_ = std::make_unique<std::ofstream>(fileName.c_str());
85  if (!output_->good()) {
86  edm::LogError("IOproblem") << "Could not open output file " << fileName << ".";
87  output_.reset();
88  }
89  }
90 }
91 
92 //
93 // member functions
94 //
95 
97  float dummy_double = 0.0;
98  int dummy_int = 0;
99 
100  run = dummy_int;
101  ls = dummy_int;
102  fX0 = dummy_double;
103  fY0 = dummy_double;
104  fZ0 = dummy_double;
105  fSigmaZ = dummy_double;
106  fbetastar = dummy_double;
107  femittance = dummy_double;
108  fPhi = dummy_double;
109  fAlpha = dummy_double;
110  fTimeOffset = dummy_double;
111 }
112 
113 // ------------ method called for each event ------------
115  using namespace edm;
116  std::ostringstream output;
117 
118  // initialize the ntuple
119  theBSfromDB_.init();
120 
121  if (watcher_.check(iSetup)) { // check for new IOV for this run / LS
122 
123  output << " for runs: " << iEvent.id().run() << " - " << iEvent.id().luminosityBlock() << std::endl;
124 
125  // Get SimBeamSpot from EventSetup:
126  const SimBeamSpotObjects* mybeamspot = &iSetup.getData(beamSpotToken_);
127 
128  theBSfromDB_.run = iEvent.id().run();
129  theBSfromDB_.ls = iEvent.id().luminosityBlock();
130  theBSfromDB_.fX0 = mybeamspot->x();
131  theBSfromDB_.fY0 = mybeamspot->y();
132  theBSfromDB_.fZ0 = mybeamspot->z();
133  theBSfromDB_.fSigmaZ = mybeamspot->sigmaZ();
134  theBSfromDB_.fbetastar = mybeamspot->betaStar();
135  theBSfromDB_.femittance = mybeamspot->emittance();
136  theBSfromDB_.fPhi = mybeamspot->phi();
137  theBSfromDB_.fAlpha = mybeamspot->alpha();
138  theBSfromDB_.fTimeOffset = mybeamspot->timeOffset();
139  bstree_->Fill();
140  output << *mybeamspot << std::endl;
141  }
142 
143  // Final output - either message logger or output file:
144  if (output_.get())
145  *output_ << output.str();
146  else
147  edm::LogInfo("BeamProfile2DBReader") << output.str();
148 }
149 
150 // ------------ method called once each job just before starting event loop ------------
152  bstree_ = tFileService->make<TTree>("BSNtuple", "SimBeamSpot analyzer ntuple");
153 
154  //Tree Branches
155  bstree_->Branch("run", &theBSfromDB_.run, "run/I");
156  bstree_->Branch("ls", &theBSfromDB_.ls, "ls/I");
157  bstree_->Branch("BSx0", &theBSfromDB_.fX0, "BSx0/F");
158  bstree_->Branch("BSy0", &theBSfromDB_.fY0, "BSy0/F");
159  bstree_->Branch("BSz0", &theBSfromDB_.fZ0, "BSz0/F");
160  bstree_->Branch("Beamsigmaz", &theBSfromDB_.fSigmaZ, "Beamsigmaz/F");
161  bstree_->Branch("BetaStar", &theBSfromDB_.fbetastar, "BetaStar/F");
162  bstree_->Branch("Emittance", &theBSfromDB_.femittance, "Emittance/F");
163  bstree_->Branch("Phi", &theBSfromDB_.fPhi, "Phi/F");
164  bstree_->Branch("Alpha", &theBSfromDB_.fAlpha, "Alpha/F");
165  bstree_->Branch("TimeOffset", &theBSfromDB_.fTimeOffset, "TimeOffset/F");
166 }
167 
168 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
171  desc.addUntracked<std::string>("rawFileName", {});
172  descriptions.addWithDefaultLabel(desc);
173 }
174 
175 //define this as a plug-in
double sigmaZ() const
get sigmaZ
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
struct BeamProfile2DBReader::TheBSfromDB theBSfromDB_
void analyze(const edm::Event &, const edm::EventSetup &) override
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
BeamProfile2DBReader(const edm::ParameterSet &)
edm::Service< TFileService > tFileService
Log< level::Error, false > LogError
double alpha() const
get Alpha
std::unique_ptr< std::ofstream > output_
T getUntrackedParameter(std::string const &, T const &) const
int iEvent
Definition: GenABIO.cc:224
edm::ESWatcher< SimBeamSpotObjectsRcd > watcher_
const edm::ESGetToken< SimBeamSpotObjects, SimBeamSpotObjectsRcd > beamSpotToken_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Log< level::Info, false > LogInfo
double timeOffset() const
get TimeOffset
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
double z() const
get Z position
double emittance() const
get Emittance
HLT enums.
double phi() const
get Phi
double betaStar() const
get BetaStar
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
double x() const
get X position
double y() const
get Y position
~BeamProfile2DBReader() override=default