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