CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OnlineBeamMonitor.cc
Go to the documentation of this file.
1 /*
2  * \file OnlineBeamMonitor.cc
3  * \author Lorenzo Uplegger/FNAL
4  * modified by Simone Gennai INFN/Bicocca
5  */
6 
21 #include <memory>
22 
23 #include <numeric>
24 
25 using namespace std;
26 using namespace edm;
27 using namespace reco;
28 
29 //----------------------------------------------------------------------------------------------------------------------
31  : monitorName_(ps.getUntrackedParameter<string>("MonitorName")),
32  bsTransientToken_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
33  bsHLTToken_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
34  bsLegacyToken_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
35  numberOfValuesToSave_(0) {
36  if (!monitorName_.empty())
37  monitorName_ = monitorName_ + "/";
38 
39  processedLumis_.clear();
40 
41  varNamesV_.push_back("x");
42  varNamesV_.push_back("y");
43  varNamesV_.push_back("z");
44  varNamesV_.push_back("sigmaX");
45  varNamesV_.push_back("sigmaY");
46  varNamesV_.push_back("sigmaZ");
47 
48  //histoByCategoryNames_.insert(pair<string, string>("run", "Coordinate"));
49  //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex fit-DataBase"));
50  //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex fit-BeamFit"));
51  //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex fit-Scalers"));
52  //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex-DataBase"));
53  //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex-BeamFit"));
54  //histoByCategoryNames_.insert(pair<string, string>("run", "PrimaryVertex-Scalers"));
55 
56  histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased BeamSpotHLT"));
57  histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased BeamSpotLegacy"));
58  histoByCategoryNames_.insert(pair<string, string>("lumi", "Lumibased BeamSpotTransient"));
59 
60  for (const auto& itV : varNamesV_) {
61  for (const auto& itM : histoByCategoryNames_) {
62  histosMap_[itV][itM.first][itM.second] = nullptr;
63  }
64  }
65 }
66 
69  ps.addUntracked<std::string>("MonitorName", "YourSubsystemName");
70  iDesc.addWithDefaultLabel(ps);
71 }
72 
73 //----------------------------------------------------------------------------------------------------------------------
75  edm::Run const& iRun,
76  edm::EventSetup const& iSetup) {
77  string name;
78  string title;
79  int firstLumi = 1;
80  int lastLumi = 3000;
81  for (auto& itM : histosMap_) {
82  //Making histos per Lumi
83  // x,y,z,sigmaX,sigmaY,sigmaZ
84  for (auto& itMM : itM.second) {
85  if (itMM.first != "run") {
86  for (auto& itMMM : itMM.second) {
87  name = string("h") + itM.first + itMMM.first;
88  title = itM.first + "_{0} " + itMMM.first;
89  if (itMM.first == "lumi") {
90  ibooker.setCurrentFolder(monitorName_ + "Debug");
91  itMMM.second = ibooker.book1D(name, title, lastLumi - firstLumi + 1, firstLumi - 0.5, lastLumi + 0.5);
92  itMMM.second->setEfficiencyFlag();
93  } else {
94  LogInfo("OnlineBeamMonitorClient") << "Unrecognized category " << itMM.first;
95  }
96  if (itMMM.second != nullptr) {
97  if (itMMM.first.find('-') != string::npos) {
98  itMMM.second->setAxisTitle(string("#Delta ") + itM.first + "_{0} (cm)", 2);
99  } else {
100  itMMM.second->setAxisTitle(itM.first + "_{0} (cm)", 2);
101  }
102  itMMM.second->setAxisTitle("Lumisection", 1);
103  }
104  }
105  }
106  }
107  }
108 
109  // create and cd into new folder
110  ibooker.setCurrentFolder(monitorName_ + "Validation");
111  //Book histograms
112  bsChoice_ = ibooker.bookProfile("bsChoice",
113  "BS Choice (+1): HLT - (-1): Legacy - (-10): Fake BS - (0): No Transient ",
114  lastLumi - firstLumi + 1,
115  firstLumi - 0.5,
116  lastLumi + 0.5,
117  100,
118  -10,
119  1,
120  "");
121  bsChoice_->setAxisTitle("Lumisection", 1);
122  bsChoice_->setAxisTitle("Choice", 2);
123 }
124 
125 //----------------------------------------------------------------------------------------------------------------------
126 std::shared_ptr<onlinebeammonitor::NoCache> OnlineBeamMonitor::globalBeginLuminosityBlock(
127  const LuminosityBlock& iLumi, const EventSetup& iSetup) const {
128  // Always create a beamspot group for each lumi weather we have results or not! Each Beamspot will be of unknown type!
129 
130  processedLumis_.push_back(iLumi.id().luminosityBlock());
131  //Read BeamSpot from DB
133  ESHandle<BeamSpotOnlineObjects> bsLegacyHandle;
134  ESHandle<BeamSpotObjects> bsTransientHandle;
135 
136  if (auto bsHLTHandle = iSetup.getHandle(bsHLTToken_)) {
137  auto const& spotDB = *bsHLTHandle;
138 
139  // translate from BeamSpotObjects to reco::BeamSpot
140  BeamSpot::Point apoint(spotDB.GetX(), spotDB.GetY(), spotDB.GetZ());
141 
143  for (int i = 0; i < 7; ++i) {
144  for (int j = 0; j < 7; ++j) {
145  matrix(i, j) = spotDB.GetCovariance(i, j);
146  }
147  }
148 
149  beamSpotsMap_["HLT"] =
150  BeamSpot(apoint, spotDB.GetSigmaZ(), spotDB.Getdxdz(), spotDB.Getdydz(), spotDB.GetBeamWidthX(), matrix);
151 
152  BeamSpot* aSpot = &(beamSpotsMap_["HLT"]);
153 
154  aSpot->setBeamWidthY(spotDB.GetBeamWidthY());
155  aSpot->setEmittanceX(spotDB.GetEmittanceX());
156  aSpot->setEmittanceY(spotDB.GetEmittanceY());
157  aSpot->setbetaStar(spotDB.GetBetaStar());
158 
159  if (spotDB.GetBeamType() == 2) {
160  aSpot->setType(reco::BeamSpot::Tracker);
161  } else {
162  aSpot->setType(reco::BeamSpot::Fake);
163  }
164  //LogInfo("OnlineBeamMonitor")
165  // << *aSpot << std::endl;
166  } else {
167  LogInfo("OnlineBeamMonitor") << "Database BeamSpot is not valid at lumi: " << iLumi.id().luminosityBlock();
168  }
169  if (auto bsLegacyHandle = iSetup.getHandle(bsLegacyToken_)) {
170  auto const& spotDB = *bsLegacyHandle;
171  // translate from BeamSpotObjects to reco::BeamSpot
172  BeamSpot::Point apoint(spotDB.GetX(), spotDB.GetY(), spotDB.GetZ());
173 
175  for (int i = 0; i < 7; ++i) {
176  for (int j = 0; j < 7; ++j) {
177  matrix(i, j) = spotDB.GetCovariance(i, j);
178  }
179  }
180 
181  beamSpotsMap_["Legacy"] =
182  BeamSpot(apoint, spotDB.GetSigmaZ(), spotDB.Getdxdz(), spotDB.Getdydz(), spotDB.GetBeamWidthX(), matrix);
183 
184  BeamSpot* aSpot = &(beamSpotsMap_["Legacy"]);
185 
186  aSpot->setBeamWidthY(spotDB.GetBeamWidthY());
187  aSpot->setEmittanceX(spotDB.GetEmittanceX());
188  aSpot->setEmittanceY(spotDB.GetEmittanceY());
189  aSpot->setbetaStar(spotDB.GetBetaStar());
190 
191  if (spotDB.GetBeamType() == 2) {
192  aSpot->setType(reco::BeamSpot::Tracker);
193  } else {
194  aSpot->setType(reco::BeamSpot::Fake);
195  }
196  //LogInfo("OnlineBeamMonitor")
197  // << *aSpot << std::endl;
198  } else {
199  LogInfo("OnlineBeamMonitor") << "Database BeamSpot is not valid at lumi: " << iLumi.id().luminosityBlock();
200  }
201  if (auto bsTransientHandle = iSetup.getHandle(bsTransientToken_)) {
202  auto const& spotDB = *bsTransientHandle;
203 
204  // translate from BeamSpotObjects to reco::BeamSpot
205  BeamSpot::Point apoint(spotDB.GetX(), spotDB.GetY(), spotDB.GetZ());
206 
208  for (int i = 0; i < 7; ++i) {
209  for (int j = 0; j < 7; ++j) {
210  matrix(i, j) = spotDB.GetCovariance(i, j);
211  }
212  }
213 
214  beamSpotsMap_["Transient"] =
215  BeamSpot(apoint, spotDB.GetSigmaZ(), spotDB.Getdxdz(), spotDB.Getdydz(), spotDB.GetBeamWidthX(), matrix);
216 
217  BeamSpot* aSpot = &(beamSpotsMap_["Transient"]);
218 
219  aSpot->setBeamWidthY(spotDB.GetBeamWidthY());
220  aSpot->setEmittanceX(spotDB.GetEmittanceX());
221  aSpot->setEmittanceY(spotDB.GetEmittanceY());
222  aSpot->setbetaStar(spotDB.GetBetaStar());
223 
224  if (spotDB.GetBeamType() == 2) {
225  aSpot->setType(reco::BeamSpot::Tracker);
226  } else {
227  aSpot->setType(reco::BeamSpot::Fake);
228  }
229  //LogInfo("OnlineBeamMonitor")
230  // << *aSpot << std::endl;
231  } else {
232  LogInfo("OnlineBeamMonitor") << "Database BeamSpot is not valid at lumi: " << iLumi.id().luminosityBlock();
233  }
234  return nullptr;
235 }
236 
237 //----------------------------------------------------------------------------------------------------------------------
239  //Setting up the choice
240  if (beamSpotsMap_.find("Transient") != beamSpotsMap_.end()) {
241  if (beamSpotsMap_.find("HLT") != beamSpotsMap_.end() &&
242  beamSpotsMap_["Transient"].x0() == beamSpotsMap_["HLT"].x0()) {
243  bsChoice_->Fill(iLumi.id().luminosityBlock(), 1);
244  bsChoice_->setBinError(iLumi.id().luminosityBlock(), 0.05);
245  } else if (beamSpotsMap_.find("Legacy") != beamSpotsMap_.end() &&
246  beamSpotsMap_["Transient"].x0() == beamSpotsMap_["Legacy"].x0()) {
247  bsChoice_->Fill(iLumi.id().luminosityBlock(), -1);
248  bsChoice_->setBinError(iLumi.id().luminosityBlock(), 0.05);
249  } else {
250  bsChoice_->Fill(iLumi.id().luminosityBlock(), -10);
251  bsChoice_->setBinError(iLumi.id().luminosityBlock(), 0.05);
252  }
253  } else {
254  bsChoice_->Fill(iLumi.id().luminosityBlock(), 0);
255  bsChoice_->setBinError(iLumi.id().luminosityBlock(), 0.05);
256  }
257 
258  // "PV,BF..." Value,Error
259  map<std::string, pair<double, double> > resultsMap;
260  vector<pair<double, double> > vertexResults;
261  MonitorElement* histo = nullptr;
262  for (const auto& itV : varNamesV_) {
263  resultsMap.clear();
264  for (const auto& itBS : beamSpotsMap_) {
265  if (itBS.second.type() == BeamSpot::Tracker) {
266  if (itV == "x") {
267  resultsMap[itBS.first] = pair<double, double>(itBS.second.x0(), itBS.second.x0Error());
268  } else if (itV == "y") {
269  resultsMap[itBS.first] = pair<double, double>(itBS.second.y0(), itBS.second.y0Error());
270  } else if (itV == "z") {
271  resultsMap[itBS.first] = pair<double, double>(itBS.second.z0(), itBS.second.z0Error());
272  } else if (itV == "sigmaX") {
273  resultsMap[itBS.first] = pair<double, double>(itBS.second.BeamWidthX(), itBS.second.BeamWidthXError());
274  } else if (itV == "sigmaY") {
275  resultsMap[itBS.first] = pair<double, double>(itBS.second.BeamWidthY(), itBS.second.BeamWidthYError());
276  } else if (itV == "sigmaZ") {
277  resultsMap[itBS.first] = pair<double, double>(itBS.second.sigmaZ(), itBS.second.sigmaZ0Error());
278  } else {
279  LogInfo("OnlineBeamMonitor") << "The histosMap_ has been built with the name " << itV
280  << " that I can't recognize!";
281  }
282  }
283  }
284 
285  for (const auto& itM : histoByCategoryNames_) {
286  if ((histo = histosMap_[itV][itM.first][itM.second]) == nullptr)
287  continue;
288  if (itM.second == "Lumibased BeamSpotHLT") {
289  if (resultsMap.find("HLT") != resultsMap.end()) {
290  histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["HLT"].first);
291  histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["HLT"].second);
292  }
293  } else if (itM.second == "Lumibased BeamSpotLegacy") {
294  if (resultsMap.find("Legacy") != resultsMap.end()) {
295  histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["Legacy"].first);
296  histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["Legacy"].second);
297  }
298  } else if (itM.second == "Lumibased BeamSpotTransient") {
299  if (resultsMap.find("Transient") != resultsMap.end()) {
300  histo->setBinContent(iLumi.id().luminosityBlock(), resultsMap["Transient"].first);
301  histo->setBinError(iLumi.id().luminosityBlock(), resultsMap["Transient"].second);
302  }
303  } else {
304  LogInfo("OnlineBeamMonitor") << "The histosMap_ have a histogram named " << itM.second
305  << " that I can't recognize in this loop!";
306  }
307  }
308  }
309 }
310 
312  if (processedLumis_.empty()) {
313  return;
314  }
315 
316  const double bigNumber = 1000000.;
317  std::sort(processedLumis_.begin(), processedLumis_.end());
318  int firstLumi = *processedLumis_.begin();
319  int lastLumi = *(--processedLumis_.end());
320  bsChoice_->getTH1()->GetXaxis()->SetRangeUser(firstLumi - 0.5, lastLumi + 0.5);
321  for (auto& itH : histosMap_) {
322  for (auto& itHH : itH.second) {
323  double min = bigNumber;
324  double max = -bigNumber;
325  if (itHH.first != "run") {
326  for (auto& itHHH : itHH.second) {
327  if (itHHH.second != nullptr) {
328  for (int bin = 1; bin <= itHHH.second->getTH1()->GetNbinsX(); bin++) {
329  if (itHHH.second->getTH1()->GetBinError(bin) != 0 || itHHH.second->getTH1()->GetBinContent(bin) != 0) {
330  if (itHHH.first == "Lumibased BeamSpotHLT" || itHHH.first == "Lumibased BeamSpotLegacy" ||
331  itHHH.first == "Lumibased BeamSpotTransient") {
332  if (min > itHHH.second->getTH1()->GetBinContent(bin)) {
333  min = itHHH.second->getTH1()->GetBinContent(bin);
334  }
335  if (max < itHHH.second->getTH1()->GetBinContent(bin)) {
336  max = itHHH.second->getTH1()->GetBinContent(bin);
337  }
338  } else {
339  LogInfo("OnlineBeamMonitorClient") << "The histosMap_ have a histogram named " << itHHH.first
340  << " that I can't recognize in this loop!";
341  }
342  }
343  }
344  }
345  }
346  for (auto& itHHH : itHH.second) {
347  if (itHHH.second != nullptr) {
348  if (itHHH.first == "Lumibased BeamSpotHLT" || itHHH.first == "Lumibased BeamSpotLegacy" ||
349  itHHH.first == "Lumibased BeamSpotTransient") {
350  if ((max == -bigNumber && min == bigNumber) || max - min == 0) {
351  itHHH.second->getTH1()->SetMinimum(itHHH.second->getTH1()->GetMinimum() - 0.01);
352  itHHH.second->getTH1()->SetMaximum(itHHH.second->getTH1()->GetMaximum() + 0.01);
353  } else {
354  itHHH.second->getTH1()->SetMinimum(min - 0.1 * (max - min));
355  itHHH.second->getTH1()->SetMaximum(max + 0.1 * (max - min));
356  }
357  } else {
358  LogInfo("OnlineBeamMonitorClient")
359  << "The histosMap_ have a histogram named " << itHHH.first << " that I can't recognize in this loop!";
360  }
361  itHHH.second->getTH1()->GetXaxis()->SetRangeUser(firstLumi - 0.5, lastLumi + 0.5);
362  }
363  }
364  }
365  }
366  }
367 }
LuminosityBlockID id() const
math::Error< dimension >::type CovarianceMatrix
Definition: BeamSpot.h:29
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
std::multimap< std::string, std::string > histoByCategoryNames_
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::ESGetToken< BeamSpotOnlineObjects, BeamSpotOnlineLegacyObjectsRcd > bsLegacyToken_
void dqmEndRun(edm::Run const &, edm::EventSetup const &) override
math::XYZPoint Point
point in the space
Definition: BeamSpot.h:27
BeamSpotContainer beamSpotsMap_
void Fill(long long x)
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
std::vector< std::string > varNamesV_
void setBeamWidthY(double v)
Definition: BeamSpot.h:105
MonitorElement * bookProfile(TString const &name, TString const &title, int nchX, double lowX, double highX, int, double lowY, double highY, char const *option="s", FUNC onbooking=NOOP())
Definition: DQMStore.h:322
OnlineBeamMonitor(const edm::ParameterSet &)
Transition
Definition: Transition.h:12
T min(T a, T b)
Definition: MathUtil.h:58
void globalEndLuminosityBlock(const edm::LuminosityBlock &iLumi, const edm::EventSetup &iSetup) override
std::vector< int > processedLumis_
Log< level::Info, false > LogInfo
std::string monitorName_
virtual void setBinContent(int binx, double content)
set content of bin (1-D)
LuminosityBlockNumber_t luminosityBlock() const
MonitorElement * bsChoice_
edm::ESGetToken< BeamSpotOnlineObjects, BeamSpotOnlineHLTObjectsRcd > bsHLTToken_
static void fillDescriptions(edm::ConfigurationDescriptions &)
HistosContainer histosMap_
edm::ESGetToken< BeamSpotObjects, BeamSpotTransientObjectsRcd > bsTransientToken_
virtual void setBinError(int binx, double error)
set uncertainty on content of bin (1-D)
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:157
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
std::shared_ptr< onlinebeammonitor::NoCache > globalBeginLuminosityBlock(const edm::LuminosityBlock &iLumi, const edm::EventSetup &iSetup) const override
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
Definition: Run.h:45
virtual void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)