CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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->GetX();
137  theBSfromDB_.BSy0_ = mybeamspot->GetY();
138  theBSfromDB_.BSz0_ = mybeamspot->GetZ();
139  theBSfromDB_.Beamsigmaz_ = mybeamspot->GetSigmaZ();
140  theBSfromDB_.Beamdxdz_ = mybeamspot->Getdxdz();
141  theBSfromDB_.BeamWidthX_ = mybeamspot->GetBeamWidthX();
142  theBSfromDB_.BeamWidthY_ = mybeamspot->GetBeamWidthY();
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
RunNumber_t run() const
Definition: EventID.h:38
void beginJob() override
T getUntrackedParameter(std::string const &, T const &) const
double GetY() const
get Y beam position
edm::ESWatcher< BeamSpotObjectsRcd > watcher_
struct BeamSpotRcdReader::theBSfromDB theBSfromDB_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
double GetSigmaZ() const
get sigma Z, RMS bunch length
double GetBeamWidthX() const
get average transverse beam width
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
std::unique_ptr< std::ofstream > output_
Log< level::Error, false > LogError
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:39
double GetBeamWidthY() const
get average transverse beam width
const edm::ESGetToken< BeamSpotObjects, BeamSpotObjectsRcd > beamSpotToken_
bool getData(T &iHolder) const
Definition: EventSetup.h:128
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 GetZ() const
get Z beam position
edm::Service< TFileService > tFileService
double Getdxdz() const
get dxdz slope, crossing angle in XZ
Log< level::Info, false > LogInfo
double GetX() const
get X beam position
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
edm::EventID id() const
Definition: EventBase.h:59
~BeamSpotRcdReader() override
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283