CMS 3D CMS Logo

BeamSpotRcdReader.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondTools/BeamSpot
4 // Class: BeamSpotRcdReader
5 //
13 //
14 // Original Author: Marco Musich
15 // Created: Tue, 18 Oct 2016 11:00:44 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
25 
31 
35 
36 // For ROOT
39 #include <TTree.h>
40 
41 #include <sstream>
42 #include <fstream>
43 
44 //
45 // class declaration
46 //
47 
48 class BeamSpotRcdReader : public edm::one::EDAnalyzer<edm::one::SharedResources> {
49 public:
50  explicit BeamSpotRcdReader(const edm::ParameterSet&);
51  ~BeamSpotRcdReader() override = default;
52 
53  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
54 
55 private:
56  void beginJob() override;
57  void analyze(const edm::Event&, const edm::EventSetup&) override;
58 
59  struct theBSfromDB {
60  int run;
61  int ls;
62  float BSx0_;
63  float BSy0_;
64  float BSz0_;
65  float Beamsigmaz_;
66  float Beamdxdz_;
67  float BeamWidthX_;
68  float BeamWidthY_;
69  void init();
70  } theBSfromDB_;
71 
74  TTree* bstree_;
75 
76  // ----------member data ---------------------------
78  std::unique_ptr<std::ofstream> output_;
79 };
80 
81 //
82 // constructors and destructor
83 //
85  : beamSpotToken_(esConsumes()), bstree_(nullptr) {
86  //now do what ever initialization is needed
87  usesResource("TFileService");
88  std::string fileName(iConfig.getUntrackedParameter<std::string>("rawFileName"));
89  if (!fileName.empty()) {
90  output_ = std::make_unique<std::ofstream>(fileName.c_str());
91  if (!output_->good()) {
92  edm::LogError("IOproblem") << "Could not open output file " << fileName << ".";
93  output_.reset();
94  }
95  }
96 }
97 
98 //
99 // member functions
100 //
101 
103  float dummy_float = 9999.0;
104  int dummy_int = 9999;
105 
106  run = dummy_int;
107  ls = dummy_int;
108  BSx0_ = dummy_float;
109  BSy0_ = dummy_float;
110  BSz0_ = dummy_float;
111  Beamsigmaz_ = dummy_float;
112  Beamdxdz_ = dummy_float;
113  BeamWidthX_ = dummy_float;
114  BeamWidthY_ = dummy_float;
115 }
116 
117 // ------------ method called for each event ------------
119  using namespace edm;
120  std::ostringstream output;
121 
122  // initialize the ntuple
123  theBSfromDB_.init();
124 
125  if (watcher_.check(iSetup)) { // check for new IOV for this run / LS
126 
127  output << " for runs: " << iEvent.id().run() << " - " << iEvent.id().luminosityBlock() << std::endl;
128 
129  // Get BeamSpot from EventSetup:
130  const BeamSpotObjects* mybeamspot = &iSetup.getData(beamSpotToken_);
131 
132  theBSfromDB_.run = iEvent.id().run();
133  theBSfromDB_.ls = iEvent.id().luminosityBlock();
134  theBSfromDB_.BSx0_ = mybeamspot->x();
135  theBSfromDB_.BSy0_ = mybeamspot->y();
136  theBSfromDB_.BSz0_ = mybeamspot->z();
137  theBSfromDB_.Beamsigmaz_ = mybeamspot->sigmaZ();
138  theBSfromDB_.Beamdxdz_ = mybeamspot->dxdz();
139  theBSfromDB_.BeamWidthX_ = mybeamspot->beamWidthX();
140  theBSfromDB_.BeamWidthY_ = mybeamspot->beamWidthY();
141 
142  bstree_->Fill();
143 
144  output << *mybeamspot << std::endl;
145  }
146 
147  // Final output - either message logger or output file:
148  if (output_.get())
149  *output_ << output.str();
150  else
151  edm::LogInfo("BeamSpotRcdReader") << output.str();
152 }
153 
154 // ------------ method called once each job just before starting event loop ------------
156  bstree_ = tFileService->make<TTree>("BSNtuple", "BeamSpot analyzer ntuple");
157 
158  //Tree Branches
159  bstree_->Branch("run", &theBSfromDB_.run, "run/I");
160  bstree_->Branch("ls", &theBSfromDB_.ls, "ls/I");
161  bstree_->Branch("BSx0", &theBSfromDB_.BSx0_, "BSx0/F");
162  bstree_->Branch("BSy0", &theBSfromDB_.BSy0_, "BSy0/F");
163  bstree_->Branch("BSz0", &theBSfromDB_.BSz0_, "BSz0/F");
164  bstree_->Branch("Beamsigmaz", &theBSfromDB_.Beamsigmaz_, "Beamsigmaz/F");
165  bstree_->Branch("Beamdxdz", &theBSfromDB_.Beamdxdz_, "Beamdxdz/F");
166  bstree_->Branch("BeamWidthX", &theBSfromDB_.BeamWidthX_, "BeamWidthX/F");
167  bstree_->Branch("BeamWidthY", &theBSfromDB_.BeamWidthY_, "BeamWidthY/F");
168 }
169 
170 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
173  desc.addUntracked<std::string>("rawFileName", {});
174  descriptions.addWithDefaultLabel(desc);
175 }
176 
177 //define this as a plug-in
void beginJob() override
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
double z() const
get Z beam position
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
edm::ESWatcher< BeamSpotObjectsRcd > watcher_
struct BeamSpotRcdReader::theBSfromDB theBSfromDB_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
double beamWidthX() const
get average transverse beam width
std::unique_ptr< std::ofstream > output_
Log< level::Error, false > LogError
const edm::ESGetToken< BeamSpotObjects, BeamSpotObjectsRcd > beamSpotToken_
T getUntrackedParameter(std::string const &, T const &) const
int iEvent
Definition: GenABIO.cc:224
void analyze(const edm::Event &, const edm::EventSetup &) override
BeamSpotRcdReader(const edm::ParameterSet &)
double x() const
get X beam position
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::Service< TFileService > tFileService
double beamWidthY() const
get average transverse beam width
Log< level::Info, false > LogInfo
double y() const
get Y beam position
~BeamSpotRcdReader() override=default
double sigmaZ() const
get sigma Z, RMS bunch length
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
HLT enums.
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
Definition: output.py:1
double dxdz() const
get dxdz slope, crossing angle in XZ