CMS 3D CMS Logo

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