CMS 3D CMS Logo

AlcaBeamMonitorClient.cc
Go to the documentation of this file.
1 /*
2  * \file AlcaBeamMonitorClient.cc
3  * \author Lorenzo Uplegger/FNAL
4  *
5  */
6 
8 #include <numeric>
17 //#include <iostream>
18 
19 using namespace std;
20 using namespace edm;
21 // using namespace reco;
22 
23 //----------------------------------------------------------------------------------------------------------------------
25  : parameters_(ps),
26  monitorName_(parameters_.getUntrackedParameter<string>(
27  "MonitorName", "YourSubsystemName")),
28  numberOfValuesToSave_(0) {
30 
31  if (!monitorName_.empty()) monitorName_ = monitorName_ + "/";
32 
33  varNamesV_.push_back("x");
34  varNamesV_.push_back("y");
35  varNamesV_.push_back("z");
36  varNamesV_.push_back("sigmaX");
37  varNamesV_.push_back("sigmaY");
38  varNamesV_.push_back("sigmaZ");
39 
40  histoByCategoryNames_.insert(
41  pair<string, string>("lumi", "Lumibased BeamSpotFit"));
42  histoByCategoryNames_.insert(
43  pair<string, string>("lumi", "Lumibased PrimaryVertex"));
44  histoByCategoryNames_.insert(
45  pair<string, string>("lumi", "Lumibased DataBase"));
46  histoByCategoryNames_.insert(
47  pair<string, string>("lumi", "Lumibased Scalers"));
48  histoByCategoryNames_.insert(
49  pair<string, string>("lumi", "Lumibased PrimaryVertex-DataBase fit"));
50  histoByCategoryNames_.insert(
51  pair<string, string>("lumi", "Lumibased PrimaryVertex-Scalers fit"));
52  histoByCategoryNames_.insert(
53  pair<string, string>("validation", "Lumibased Scalers-DataBase fit"));
54  histoByCategoryNames_.insert(
55  pair<string, string>("validation", "Lumibased PrimaryVertex-DataBase"));
56  histoByCategoryNames_.insert(
57  pair<string, string>("validation", "Lumibased PrimaryVertex-Scalers"));
58 
59  for (vector<string>::iterator itV = varNamesV_.begin();
60  itV != varNamesV_.end(); itV++) {
61  for (multimap<string, string>::iterator itM = histoByCategoryNames_.begin();
62  itM != histoByCategoryNames_.end(); itM++) {
63  histosMap_[*itV][itM->first][itM->second] = nullptr;
64  positionsMap_[*itV][itM->first][itM->second] =
65  3 * numberOfValuesToSave_; // value, error, ok
67  }
68  }
69 }
70 
72 
73 //----------------------------------------------------------------------------------------------------------------------
75 
76 //----------------------------------------------------------------------------------------------------------------------
78  const EventSetup& context) {
79  for (HistosContainer::iterator itM = histosMap_.begin();
80  itM != histosMap_.end(); itM++) {
81  for (map<string, map<string, MonitorElement*> >::iterator itMM =
82  itM->second.begin();
83  itMM != itM->second.end(); itMM++) {
84  if (itMM->first != "run") {
85  for (map<string, MonitorElement*>::iterator itMMM =
86  itMM->second.begin();
87  itMMM != itMM->second.end(); itMMM++) {
88  if (itMMM->second != nullptr) {
89  if (itMM->first == "lumi") {
90  dbe_->removeElement(monitorName_ + "Debug",
91  itMMM->second->getName());
92  } else if (itMM->first == "validation") {
93  dbe_->removeElement(monitorName_ + "Validation",
94  itMMM->second->getName());
95  } else {
96  LogInfo("AlcaBeamMonitorClient") << "Unrecognized category "
97  << itMM->first;
98  // assert(0);
99  }
100  itMMM->second = nullptr;
101  }
102  }
103  }
104  }
105  }
106 }
107 
108 //----------------------------------------------------------------------------------------------------------------------
109 
110 //----------------------------------------------------------------------------------------------------------------------
112  const EventSetup& iSetup) {}
113 
114 //----------------------------------------------------------------------------------------------------------------------
116  const EventSetup& iSetup) {
117  MonitorElement* tmp = nullptr;
118  tmp = dbe_->get(monitorName_ + "Service/hHistoLumiValues");
119  if (!tmp) {
120  return;
121  }
122  valuesMap_[iLumi.id().luminosityBlock()] = vector<double>();
123  for (int i = 0; i < 3 * numberOfValuesToSave_; i++) {
124  valuesMap_[iLumi.id().luminosityBlock()].push_back(
125  tmp->getTProfile()->GetBinContent(i + 1));
126  }
127 }
128 
129 //----------------------------------------------------------------------------------------------------------------------
130 void AlcaBeamMonitorClient::endRun(const Run& iRun, const EventSetup& context) {
131  // use this in case any LS is missing.
132  if (valuesMap_.empty()) {
133  LogInfo("AlcaBeamMonitorClient")
134  << "The histogram "
135  << monitorName_ +
136  "Service/hHistoLumiValues which contains all the values has not "
137  "been found in any lumi!";
138  return;
139  }
140  int lastLumi = (--valuesMap_.end())->first;
141  int firstLumi = valuesMap_.begin()->first;
142 
143  // create and cd into new folder
144  dbe_->setCurrentFolder(monitorName_ + "Validation");
145 
146  LogInfo("AlcaBeamMonitorClient") << "End of run " << iRun.id().run() << "("
147  << firstLumi << "-" << lastLumi << ")";
148 
149  string name;
150  string title;
151  // x,y,z,sigmaX,sigmaY,sigmaZ
152  for (HistosContainer::iterator itM = histosMap_.begin();
153  itM != histosMap_.end(); itM++) {
154  for (map<string, map<string, MonitorElement*> >::iterator itMM =
155  itM->second.begin();
156  itMM != itM->second.end(); itMM++) {
157  if (itMM->first != "run") {
158  for (map<string, MonitorElement*>::iterator itMMM =
159  itMM->second.begin();
160  itMMM != itMM->second.end(); itMMM++) {
161  name = string("h") + itM->first + itMMM->first;
162  title = itM->first + "_{0} " + itMMM->first;
163  if (itMM->first == "lumi") {
164  dbe_->setCurrentFolder(monitorName_ + "Debug");
165  itMMM->second = dbe_->book1D(name, title, lastLumi - firstLumi + 1,
166  firstLumi - 0.5, lastLumi + 0.5);
167  itMMM->second->setEfficiencyFlag();
168  } else if (itMM->first == "validation" &&
169  itMMM->first == "Lumibased Scalers-DataBase fit") {
170  dbe_->setCurrentFolder(monitorName_ + "Validation");
171  itMMM->second = dbe_->book1D(name, title, lastLumi - firstLumi + 1,
172  firstLumi - 0.5, lastLumi + 0.5);
173  itMMM->second->setEfficiencyFlag();
174  } else if (itMM->first == "validation" &&
175  itMMM->first != "Lumibased Scalers-DataBase fit" &&
176  (itM->first == "x" || itM->first == "y" ||
177  itM->first == "z")) {
178  dbe_->setCurrentFolder(monitorName_ + "Validation");
179  itMMM->second = dbe_->book1D(name, title, lastLumi - firstLumi + 1,
180  firstLumi - 0.5, lastLumi + 0.5);
181  itMMM->second->setEfficiencyFlag();
182  }
183  else if (itMM->first == "validation" &&
184  (itM->first == "sigmaX" || itM->first == "sigmaY" ||
185  itM->first == "sigmaZ")) {
186  dbe_->setCurrentFolder(monitorName_ + "Validation");
187  itMMM->second = nullptr;
188  } else {
189  LogInfo("AlcaBeamMonitorClient") << "Unrecognized category "
190  << itMM->first;
191  // assert(0);
192  }
193  if (itMMM->second != nullptr) {
194  if (itMMM->first.find('-') != string::npos) {
195  itMMM->second->setAxisTitle(
196  string("#Delta ") + itM->first + "_{0} (cm)", 2);
197  } else {
198  itMMM->second->setAxisTitle(itM->first + "_{0} (cm)", 2);
199  }
200  itMMM->second->setAxisTitle("Lumisection", 1);
201  }
202  }
203  }
204  }
205  }
206 
207  unsigned int bin = 0;
208  for (HistosContainer::iterator itH = histosMap_.begin();
209  itH != histosMap_.end(); itH++) {
210  for (map<string, map<string, MonitorElement*> >::iterator itHH =
211  itH->second.begin();
212  itHH != itH->second.end(); itHH++) {
213  for (map<string, MonitorElement*>::iterator itHHH = itHH->second.begin();
214  itHHH != itHH->second.end(); itHHH++) {
215  for (map<LuminosityBlockNumber_t, vector<double> >::iterator itVal =
216  valuesMap_.begin();
217  itVal != valuesMap_.end(); itVal++) {
218  if (itHHH->second != nullptr) {
219  // cout << positionsMap_[itH->first][itHH->first][itHHH->first]
220  //<< endl;
221  if (itVal->second[positionsMap_[itH->first][itHH->first]
222  [itHHH->first] +
223  2] == 1) {
224  bin = itHHH->second->getTH1()->FindBin(itVal->first);
225  itHHH->second->setBinContent(
226  bin, itVal->second[positionsMap_[itH->first][itHH->first]
227  [itHHH->first]]);
228  itHHH->second->setBinError(
229  bin, itVal->second[positionsMap_[itH->first][itHH->first]
230  [itHHH->first] +
231  1]);
232  }
233  }
234  }
235  }
236  }
237  }
238 
239 
240  const double bigNumber = 1000000.;
241  for (HistosContainer::iterator itH = histosMap_.begin();
242  itH != histosMap_.end(); itH++) {
243  for (map<string, map<string, MonitorElement*> >::iterator itHH =
244  itH->second.begin();
245  itHH != itH->second.end(); itHH++) {
246  double min = bigNumber;
247  double max = -bigNumber;
248  double minDelta = bigNumber;
249  double maxDelta = -bigNumber;
250  // double minDeltaProf = bigNumber;
251  // double maxDeltaProf = -bigNumber;
252  if (itHH->first != "run") {
253  for (map<string, MonitorElement*>::iterator itHHH =
254  itHH->second.begin();
255  itHHH != itHH->second.end(); itHHH++) {
256  if (itHHH->second != nullptr) {
257  for (int bin = 1; bin <= itHHH->second->getTH1()->GetNbinsX();
258  bin++) {
259  if (itHHH->second->getTH1()->GetBinError(bin) != 0 ||
260  itHHH->second->getTH1()->GetBinContent(bin) != 0) {
261  if (itHHH->first == "Lumibased BeamSpotFit" ||
262  itHHH->first == "Lumibased PrimaryVertex" ||
263  itHHH->first == "Lumibased DataBase" ||
264  itHHH->first == "Lumibased Scalers") {
265  if (min > itHHH->second->getTH1()->GetBinContent(bin)) {
266  min = itHHH->second->getTH1()->GetBinContent(bin);
267  }
268  if (max < itHHH->second->getTH1()->GetBinContent(bin)) {
269  max = itHHH->second->getTH1()->GetBinContent(bin);
270  }
271  } else if (itHHH->first ==
272  "Lumibased PrimaryVertex-DataBase fit" ||
273  itHHH->first ==
274  "Lumibased PrimaryVertex-Scalers fit" ||
275  itHHH->first == "Lumibased Scalers-DataBase fit" ||
276  itHHH->first == "Lumibased PrimaryVertex-DataBase" ||
277  itHHH->first == "Lumibased PrimaryVertex-Scalers") {
278  if (minDelta > itHHH->second->getTH1()->GetBinContent(bin)) {
279  minDelta = itHHH->second->getTH1()->GetBinContent(bin);
280  }
281  if (maxDelta < itHHH->second->getTH1()->GetBinContent(bin)) {
282  maxDelta = itHHH->second->getTH1()->GetBinContent(bin);
283  }
284  }
285  else {
286  LogInfo("AlcaBeamMonitorClient")
287  << "The histosMap_ have a histogram named "
288  << itHHH->first
289  << " that I can't recognize in this loop!";
290  // assert(0);
291  }
292  }
293  }
294  }
295  }
296  for (map<string, MonitorElement*>::iterator itHHH =
297  itHH->second.begin();
298  itHHH != itHH->second.end(); itHHH++) {
299  if (itHHH->second != nullptr) {
300  if (itHHH->first == "Lumibased BeamSpotFit" ||
301  itHHH->first == "Lumibased PrimaryVertex" ||
302  itHHH->first == "Lumibased DataBase" ||
303  itHHH->first == "Lumibased Scalers") {
304  if ((max == -bigNumber && min == bigNumber) || max - min == 0) {
305  itHHH->second->getTH1()->SetMinimum(
306  itHHH->second->getTH1()->GetMinimum() - 0.01);
307  itHHH->second->getTH1()->SetMaximum(
308  itHHH->second->getTH1()->GetMaximum() + 0.01);
309  } else {
310  itHHH->second->getTH1()->SetMinimum(min - 0.1 * (max - min));
311  itHHH->second->getTH1()->SetMaximum(max + 0.1 * (max - min));
312  }
313  } else if (itHHH->first == "Lumibased PrimaryVertex-DataBase fit" ||
314  itHHH->first == "Lumibased PrimaryVertex-Scalers fit" ||
315  itHHH->first == "Lumibased Scalers-DataBase fit" ||
316  itHHH->first == "Lumibased PrimaryVertex-DataBase" ||
317  itHHH->first == "Lumibased PrimaryVertex-Scalers") {
318  if ((maxDelta == -bigNumber && minDelta == bigNumber) ||
319  maxDelta - minDelta == 0) {
320  itHHH->second->getTH1()->SetMinimum(
321  itHHH->second->getTH1()->GetMinimum() - 0.01);
322  itHHH->second->getTH1()->SetMaximum(
323  itHHH->second->getTH1()->GetMaximum() + 0.01);
324  } else {
325  itHHH->second->getTH1()->SetMinimum(minDelta -
326  2 * (maxDelta - minDelta));
327  itHHH->second->getTH1()->SetMaximum(maxDelta +
328  2 * (maxDelta - minDelta));
329  }
330  }
331  else {
332  LogInfo("AlcaBeamMonitorClient")
333  << "The histosMap_ have a histogram named " << itHHH->first
334  << " that I can't recognize in this loop!";
335  }
336  }
337  }
338  }
339  }
340  }
341 
342 }
343 
344 //----------------------------------------------------------------------------------------------------------------------
346  const EventSetup& iSetup) {}
347 
LuminosityBlockID id() const
TProfile * getTProfile() const
RunID const & id() const
Definition: RunBase.h:39
RunNumber_t run() const
Definition: RunID.h:39
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void beginJob(void) override
PositionContainer positionsMap_
void beginRun(const edm::Run &iRun, const edm::EventSetup &iSetup) override
unsigned int LuminosityBlockNumber_t
std::multimap< std::string, std::string > histoByCategoryNames_
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override
U second(std::pair< T, U > const &p)
int iEvent
Definition: GenABIO.cc:230
virtual void endJob()
Definition: EDAnalyzer.h:86
void endRun(const edm::Run &iRun, const edm::EventSetup &iSetup) override
T min(T a, T b)
Definition: MathUtil.h:58
bin
set the eta bin as selection string.
LuminosityBlockNumber_t luminosityBlock() const
AlcaBeamMonitorClient(const edm::ParameterSet &)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
std::map< edm::LuminosityBlockNumber_t, std::vector< double > > valuesMap_
std::vector< std::string > varNamesV_
HLT enums.
void endLuminosityBlock(const edm::LuminosityBlock &iLumi, const edm::EventSetup &iSetup) override
Definition: Run.h:44