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 
56  private:
57  void beginJob() override;
58  void analyze(const edm::Event&, const edm::EventSetup&) override;
59  void endJob() override;
60 
61  struct theBSfromDB
62  {
63  int run;
64  int ls;
65  float BSx0_;
66  float BSy0_;
67  float BSz0_;
68  float Beamsigmaz_;
69  float Beamdxdz_;
70  float BeamWidthX_;
71  float BeamWidthY_;
72  void init();
73  } theBSfromDB_;
74 
76  TTree * bstree_;
77 
78  // ----------member data ---------------------------
80  std::unique_ptr<std::ofstream> output_;
81 };
82 
83 //
84 // constants, enums and typedefs
85 //
86 
87 //
88 // static data member definitions
89 //
90 
91 //
92 // constructors and destructor
93 //
96 {
97  //now do what ever initialization is needed
98  usesResource("TFileService");
99  std::string fileName(iConfig.getUntrackedParameter<std::string>("rawFileName"));
100  if (!fileName.empty()) {
101  output_.reset(new std::ofstream(fileName.c_str()));
102  if (!output_->good()) {
103  edm::LogError("IOproblem") << "Could not open output file " << fileName << ".";
104  output_.reset();
105  }
106  }
107 }
108 
109 
111 {
112 
113  // do anything here that needs to be done at desctruction time
114  // (e.g. close files, deallocate resources etc.)
115 
116 }
117 
118 
119 //
120 // member functions
121 //
122 
123 void
125 {
126 
127  float dummy_float = 9999.0;
128  int dummy_int = 9999;
129 
130  run = dummy_int;
131  ls = dummy_int;
132  BSx0_ = dummy_float;
133  BSy0_ = dummy_float;
134  BSz0_ = dummy_float;
135  Beamsigmaz_ = dummy_float;
136  Beamdxdz_ = dummy_float;
137  BeamWidthX_ = dummy_float;
138  BeamWidthY_ = dummy_float;
139 
140 }
141 
142 
143 // ------------ method called for each event ------------
144 void
146 {
147  using namespace edm;
148  std::ostringstream output;
149 
150  // initialize the ntuple
151  theBSfromDB_.init();
152 
153  if (watcher_.check(iSetup)) { // check for new IOV for this run / LS
154 
155  output << " for runs: " << iEvent.id().run() << " - " << iEvent.id().luminosityBlock() << std::endl;
156 
157  // Get BeamSpot from EventSetup:
159  iSetup.get<BeamSpotObjectsRcd>().get(beamhandle);
160  const BeamSpotObjects *mybeamspot = beamhandle.product();
161 
162  theBSfromDB_.run = iEvent.id().run();
163  theBSfromDB_.ls = iEvent.id().luminosityBlock();
164  theBSfromDB_.BSx0_ = mybeamspot->GetX();
165  theBSfromDB_.BSy0_ = mybeamspot->GetY();
166  theBSfromDB_.BSz0_ = mybeamspot->GetZ();
167  theBSfromDB_.Beamsigmaz_ = mybeamspot->GetSigmaZ();
168  theBSfromDB_.Beamdxdz_ = mybeamspot->Getdxdz();
169  theBSfromDB_.BeamWidthX_ = mybeamspot->GetBeamWidthX();
170  theBSfromDB_.BeamWidthY_ = mybeamspot->GetBeamWidthY();
171 
172  bstree_->Fill();
173 
174  output << *mybeamspot << std::endl;
175 
176  }
177 
178  // Final output - either message logger or output file:
179  if (output_.get()) *output_ << output.str();
180  else edm::LogInfo("") << output.str();
181 }
182 
183 
184 // ------------ method called once each job just before starting event loop ------------
185 void
187 {
188  bstree_ = tFileService->make<TTree>("BSNtuple","BeamSpot analyzer ntuple");
189 
190  //Tree Branches
191  bstree_->Branch("run",&theBSfromDB_.run,"run/I");
192  bstree_->Branch("ls",&theBSfromDB_.ls,"ls/I");
193  bstree_->Branch("BSx0",&theBSfromDB_.BSx0_,"BSx0/F");
194  bstree_->Branch("BSy0",&theBSfromDB_.BSy0_,"BSy0/F");
195  bstree_->Branch("BSz0",&theBSfromDB_.BSz0_,"BSz0/F");
196  bstree_->Branch("Beamsigmaz",&theBSfromDB_.Beamsigmaz_ ,"Beamsigmaz/F");
197  bstree_->Branch("Beamdxdz",&theBSfromDB_.Beamdxdz_,"Beamdxdz/F");
198  bstree_->Branch("BeamWidthX",&theBSfromDB_.BeamWidthX_,"BeamWidthX/F");
199  bstree_->Branch("BeamWidthY",&theBSfromDB_.BeamWidthY_,"BeamWidthY/F");
200 
201 }
202 
203 // ------------ method called once each job just after ending the event loop ------------
204 void
206 {
207 }
208 
209 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
210 void
212  //The following says we do not know what parameters are allowed so do no validation
213  // Please change this to state exactly what you do use, even if it is no parameters
215  desc.setUnknown();
216  descriptions.addDefault(desc);
217 }
218 
219 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:39
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_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
#define nullptr
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_
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:40
double GetBeamWidthY() const
get average transverse beam width
void endJob() override
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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
def ls(path, rec=False)
Definition: eostools.py:349
double GetX() const
get X beam position
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:52
edm::EventID id() const
Definition: EventBase.h:59
HLT enums.
T get() const
Definition: EventSetup.h:71
~BeamSpotRcdReader() override
T const * product() const
Definition: ESHandle.h:86