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_ != "") 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] = 0;
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 != 0) {
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 = 0;
101  }
102  }
103  }
104  }
105  }
106 }
107 
108 //----------------------------------------------------------------------------------------------------------------------
110  const EventSetup& iSetup) {}
111 
112 //----------------------------------------------------------------------------------------------------------------------
114  const EventSetup& iSetup) {}
115 
116 //----------------------------------------------------------------------------------------------------------------------
118  const EventSetup& iSetup) {
119  MonitorElement* tmp = 0;
120  tmp = dbe_->get(monitorName_ + "Service/hHistoLumiValues");
121  if (!tmp) {
122  return;
123  }
124  valuesMap_[iLumi.id().luminosityBlock()] = vector<double>();
125  for (int i = 0; i < 3 * numberOfValuesToSave_; i++) {
126  valuesMap_[iLumi.id().luminosityBlock()].push_back(
127  tmp->getTProfile()->GetBinContent(i + 1));
128  }
129 }
130 
131 //----------------------------------------------------------------------------------------------------------------------
132 void AlcaBeamMonitorClient::endRun(const Run& iRun, const EventSetup& context) {
133  // use this in case any LS is missing.
134  if (valuesMap_.size() == 0) {
135  LogInfo("AlcaBeamMonitorClient")
136  << "The histogram "
137  << monitorName_ +
138  "Service/hHistoLumiValues which contains all the values has not "
139  "been found in any lumi!";
140  return;
141  }
142  int lastLumi = (--valuesMap_.end())->first;
143  int firstLumi = valuesMap_.begin()->first;
144 
145  // create and cd into new folder
146  dbe_->setCurrentFolder(monitorName_ + "Validation");
147 
148  LogInfo("AlcaBeamMonitorClient") << "End of run " << iRun.id().run() << "("
149  << firstLumi << "-" << lastLumi << ")";
150 
151  string name;
152  string title;
153  // x,y,z,sigmaX,sigmaY,sigmaZ
154  for (HistosContainer::iterator itM = histosMap_.begin();
155  itM != histosMap_.end(); itM++) {
156  for (map<string, map<string, MonitorElement*> >::iterator itMM =
157  itM->second.begin();
158  itMM != itM->second.end(); itMM++) {
159  if (itMM->first != "run") {
160  for (map<string, MonitorElement*>::iterator itMMM =
161  itMM->second.begin();
162  itMMM != itMM->second.end(); itMMM++) {
163  name = string("h") + itM->first + itMMM->first;
164  title = itM->first + "_{0} " + itMMM->first;
165  if (itMM->first == "lumi") {
166  dbe_->setCurrentFolder(monitorName_ + "Debug");
167  itMMM->second = dbe_->book1D(name, title, lastLumi - firstLumi + 1,
168  firstLumi - 0.5, lastLumi + 0.5);
169  itMMM->second->setEfficiencyFlag();
170  } else if (itMM->first == "validation" &&
171  itMMM->first == "Lumibased Scalers-DataBase fit") {
172  dbe_->setCurrentFolder(monitorName_ + "Validation");
173  itMMM->second = dbe_->book1D(name, title, lastLumi - firstLumi + 1,
174  firstLumi - 0.5, lastLumi + 0.5);
175  itMMM->second->setEfficiencyFlag();
176  } else if (itMM->first == "validation" &&
177  itMMM->first != "Lumibased Scalers-DataBase fit" &&
178  (itM->first == "x" || itM->first == "y" ||
179  itM->first == "z")) {
180  dbe_->setCurrentFolder(monitorName_ + "Validation");
181  itMMM->second = dbe_->book1D(name, title, lastLumi - firstLumi + 1,
182  firstLumi - 0.5, lastLumi + 0.5);
183  itMMM->second->setEfficiencyFlag();
184  }
185  else if (itMM->first == "validation" &&
186  (itM->first == "sigmaX" || itM->first == "sigmaY" ||
187  itM->first == "sigmaZ")) {
188  dbe_->setCurrentFolder(monitorName_ + "Validation");
189  itMMM->second = 0;
190  } else {
191  LogInfo("AlcaBeamMonitorClient") << "Unrecognized category "
192  << itMM->first;
193  // assert(0);
194  }
195  if (itMMM->second != 0) {
196  if (itMMM->first.find('-') != string::npos) {
197  itMMM->second->setAxisTitle(
198  string("#Delta ") + itM->first + "_{0} (cm)", 2);
199  } else {
200  itMMM->second->setAxisTitle(itM->first + "_{0} (cm)", 2);
201  }
202  itMMM->second->setAxisTitle("Lumisection", 1);
203  }
204  }
205  }
206  }
207  }
208 
209  unsigned int bin = 0;
210  for (HistosContainer::iterator itH = histosMap_.begin();
211  itH != histosMap_.end(); itH++) {
212  for (map<string, map<string, MonitorElement*> >::iterator itHH =
213  itH->second.begin();
214  itHH != itH->second.end(); itHH++) {
215  for (map<string, MonitorElement*>::iterator itHHH = itHH->second.begin();
216  itHHH != itHH->second.end(); itHHH++) {
217  for (map<LuminosityBlockNumber_t, vector<double> >::iterator itVal =
218  valuesMap_.begin();
219  itVal != valuesMap_.end(); itVal++) {
220  if (itHHH->second != 0) {
221  // cout << positionsMap_[itH->first][itHH->first][itHHH->first]
222  //<< endl;
223  if (itVal->second[positionsMap_[itH->first][itHH->first]
224  [itHHH->first] +
225  2] == 1) {
226  bin = itHHH->second->getTH1()->FindBin(itVal->first);
227  itHHH->second->setBinContent(
228  bin, itVal->second[positionsMap_[itH->first][itHH->first]
229  [itHHH->first]]);
230  itHHH->second->setBinError(
231  bin, itVal->second[positionsMap_[itH->first][itHH->first]
232  [itHHH->first] +
233  1]);
234  }
235  }
236  }
237  }
238  }
239  }
240 
241 
242  const double bigNumber = 1000000.;
243  for (HistosContainer::iterator itH = histosMap_.begin();
244  itH != histosMap_.end(); itH++) {
245  for (map<string, map<string, MonitorElement*> >::iterator itHH =
246  itH->second.begin();
247  itHH != itH->second.end(); itHH++) {
248  double min = bigNumber;
249  double max = -bigNumber;
250  double minDelta = bigNumber;
251  double maxDelta = -bigNumber;
252  // double minDeltaProf = bigNumber;
253  // double maxDeltaProf = -bigNumber;
254  if (itHH->first != "run") {
255  for (map<string, MonitorElement*>::iterator itHHH =
256  itHH->second.begin();
257  itHHH != itHH->second.end(); itHHH++) {
258  if (itHHH->second != 0) {
259  for (int bin = 1; bin <= itHHH->second->getTH1()->GetNbinsX();
260  bin++) {
261  if (itHHH->second->getTH1()->GetBinError(bin) != 0 ||
262  itHHH->second->getTH1()->GetBinContent(bin) != 0) {
263  if (itHHH->first == "Lumibased BeamSpotFit" ||
264  itHHH->first == "Lumibased PrimaryVertex" ||
265  itHHH->first == "Lumibased DataBase" ||
266  itHHH->first == "Lumibased Scalers") {
267  if (min > itHHH->second->getTH1()->GetBinContent(bin)) {
268  min = itHHH->second->getTH1()->GetBinContent(bin);
269  }
270  if (max < itHHH->second->getTH1()->GetBinContent(bin)) {
271  max = itHHH->second->getTH1()->GetBinContent(bin);
272  }
273  } else if (itHHH->first ==
274  "Lumibased PrimaryVertex-DataBase fit" ||
275  itHHH->first ==
276  "Lumibased PrimaryVertex-Scalers fit" ||
277  itHHH->first == "Lumibased Scalers-DataBase fit" ||
278  itHHH->first == "Lumibased PrimaryVertex-DataBase" ||
279  itHHH->first == "Lumibased PrimaryVertex-Scalers") {
280  if (minDelta > itHHH->second->getTH1()->GetBinContent(bin)) {
281  minDelta = itHHH->second->getTH1()->GetBinContent(bin);
282  }
283  if (maxDelta < itHHH->second->getTH1()->GetBinContent(bin)) {
284  maxDelta = itHHH->second->getTH1()->GetBinContent(bin);
285  }
286  }
287  else {
288  LogInfo("AlcaBeamMonitorClient")
289  << "The histosMap_ have a histogram named "
290  << itHHH->first
291  << " that I can't recognize in this loop!";
292  // assert(0);
293  }
294  }
295  }
296  }
297  }
298  for (map<string, MonitorElement*>::iterator itHHH =
299  itHH->second.begin();
300  itHHH != itHH->second.end(); itHHH++) {
301  if (itHHH->second != 0) {
302  if (itHHH->first == "Lumibased BeamSpotFit" ||
303  itHHH->first == "Lumibased PrimaryVertex" ||
304  itHHH->first == "Lumibased DataBase" ||
305  itHHH->first == "Lumibased Scalers") {
306  if ((max == -bigNumber && min == bigNumber) || max - min == 0) {
307  itHHH->second->getTH1()->SetMinimum(
308  itHHH->second->getTH1()->GetMinimum() - 0.01);
309  itHHH->second->getTH1()->SetMaximum(
310  itHHH->second->getTH1()->GetMaximum() + 0.01);
311  } else {
312  itHHH->second->getTH1()->SetMinimum(min - 0.1 * (max - min));
313  itHHH->second->getTH1()->SetMaximum(max + 0.1 * (max - min));
314  }
315  } else if (itHHH->first == "Lumibased PrimaryVertex-DataBase fit" ||
316  itHHH->first == "Lumibased PrimaryVertex-Scalers fit" ||
317  itHHH->first == "Lumibased Scalers-DataBase fit" ||
318  itHHH->first == "Lumibased PrimaryVertex-DataBase" ||
319  itHHH->first == "Lumibased PrimaryVertex-Scalers") {
320  if ((maxDelta == -bigNumber && minDelta == bigNumber) ||
321  maxDelta - minDelta == 0) {
322  itHHH->second->getTH1()->SetMinimum(
323  itHHH->second->getTH1()->GetMinimum() - 0.01);
324  itHHH->second->getTH1()->SetMaximum(
325  itHHH->second->getTH1()->GetMaximum() + 0.01);
326  } else {
327  itHHH->second->getTH1()->SetMinimum(minDelta -
328  2 * (maxDelta - minDelta));
329  itHHH->second->getTH1()->SetMaximum(maxDelta +
330  2 * (maxDelta - minDelta));
331  }
332  }
333  else {
334  LogInfo("AlcaBeamMonitorClient")
335  << "The histosMap_ have a histogram named " << itHHH->first
336  << " that I can't recognize in this loop!";
337  }
338  }
339  }
340  }
341  }
342  }
343 
344 }
345 
346 //----------------------------------------------------------------------------------------------------------------------
348  const EventSetup& iSetup) {}
349 
LuminosityBlockID id() 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
void beginLuminosityBlock(const edm::LuminosityBlock &iLumi, const edm::EventSetup &iSetup) override
virtual void endJob()
Definition: EDAnalyzer.h:80
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.
TProfile * getTProfile(void) const
void endLuminosityBlock(const edm::LuminosityBlock &iLumi, const edm::EventSetup &iSetup) override
Definition: Run.h:42