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;
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 
99 
100 //
101 // member functions
102 //
103 
105  float dummy_float = 9999.0;
106  int dummy_int = 9999;
107 
108  run = dummy_int;
109  ls = dummy_int;
110  BSx0_ = dummy_float;
111  BSy0_ = dummy_float;
112  BSz0_ = dummy_float;
113  Beamsigmaz_ = dummy_float;
114  Beamdxdz_ = dummy_float;
115  BeamWidthX_ = dummy_float;
116  BeamWidthY_ = dummy_float;
117 }
118 
119 // ------------ method called for each event ------------
121  using namespace edm;
122  std::ostringstream output;
123 
124  // initialize the ntuple
125  theBSfromDB_.init();
126 
127  if (watcher_.check(iSetup)) { // check for new IOV for this run / LS
128 
129  output << " for runs: " << iEvent.id().run() << " - " << iEvent.id().luminosityBlock() << std::endl;
130 
131  // Get BeamSpot from EventSetup:
132  const BeamSpotObjects* mybeamspot = &iSetup.getData(beamSpotToken_);
133 
134  theBSfromDB_.run = iEvent.id().run();
135  theBSfromDB_.ls = iEvent.id().luminosityBlock();
136  theBSfromDB_.BSx0_ = mybeamspot->x();
137  theBSfromDB_.BSy0_ = mybeamspot->y();
138  theBSfromDB_.BSz0_ = mybeamspot->z();
139  theBSfromDB_.Beamsigmaz_ = mybeamspot->sigmaZ();
140  theBSfromDB_.Beamdxdz_ = mybeamspot->dxdz();
141  theBSfromDB_.BeamWidthX_ = mybeamspot->beamWidthX();
142  theBSfromDB_.BeamWidthY_ = mybeamspot->beamWidthY();
143 
144  bstree_->Fill();
145 
146  output << *mybeamspot << std::endl;
147  }
148 
149  // Final output - either message logger or output file:
150  if (output_.get())
151  *output_ << output.str();
152  else
153  edm::LogInfo("") << output.str();
154 }
155 
156 // ------------ method called once each job just before starting event loop ------------
158  bstree_ = tFileService->make<TTree>("BSNtuple", "BeamSpot analyzer ntuple");
159 
160  //Tree Branches
161  bstree_->Branch("run", &theBSfromDB_.run, "run/I");
162  bstree_->Branch("ls", &theBSfromDB_.ls, "ls/I");
163  bstree_->Branch("BSx0", &theBSfromDB_.BSx0_, "BSx0/F");
164  bstree_->Branch("BSy0", &theBSfromDB_.BSy0_, "BSy0/F");
165  bstree_->Branch("BSz0", &theBSfromDB_.BSz0_, "BSz0/F");
166  bstree_->Branch("Beamsigmaz", &theBSfromDB_.Beamsigmaz_, "Beamsigmaz/F");
167  bstree_->Branch("Beamdxdz", &theBSfromDB_.Beamdxdz_, "Beamdxdz/F");
168  bstree_->Branch("BeamWidthX", &theBSfromDB_.BeamWidthX_, "BeamWidthX/F");
169  bstree_->Branch("BeamWidthY", &theBSfromDB_.BeamWidthY_, "BeamWidthY/F");
170 }
171 
172 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
174  //The following says we do not know what parameters are allowed so do no validation
175  // Please change this to state exactly what you do use, even if it is no parameters
177  desc.setUnknown();
178  descriptions.addDefault(desc);
179 }
180 
181 //define this as a plug-in
void beginJob() override
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 addDefault(ParameterSetDescription const &psetDescription)
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
double sigmaZ() const
get sigma Z, RMS bunch length
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
HLT enums.
~BeamSpotRcdReader() override
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