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