CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DQM/BeamMonitor/plugins/AlcaBeamMonitorClient.cc

Go to the documentation of this file.
00001 /*
00002  * \file AlcaBeamMonitorClient.cc
00003  * \author Lorenzo Uplegger/FNAL
00004  * $Date: 2011/01/06 17:35:56 $
00005  * $Revision: 1.5 $
00006  *
00007  */
00008 
00009 #include "FWCore/Framework/interface/MakerMacros.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "FWCore/Framework/interface/ESHandle.h"
00012 #include "FWCore/ServiceRegistry/interface/Service.h"
00013 #include "DQM/BeamMonitor/plugins/AlcaBeamMonitorClient.h"
00014 #include "DQMServices/Core/interface/QReport.h"
00015 #include "FWCore/Framework/interface/Run.h"
00016 #include "FWCore/Framework/interface/LuminosityBlock.h"
00017 #include "FWCore/Framework/interface/EventSetup.h"
00018 #include <numeric>
00019 //#include <iostream>
00020 
00021 using namespace std;
00022 using namespace edm;
00023 //using namespace reco;
00024 
00025 //----------------------------------------------------------------------------------------------------------------------
00026 AlcaBeamMonitorClient::AlcaBeamMonitorClient( const ParameterSet& ps ) :
00027   parameters_         (ps),
00028   monitorName_        (parameters_.getUntrackedParameter<string>("MonitorName","YourSubsystemName")),
00029   numberOfValuesToSave_ (0)
00030 {
00031   dbe_            = Service<DQMStore>().operator->();
00032   
00033   if (monitorName_ != "" ) monitorName_ = monitorName_+"/" ;
00034   
00035 
00036   varNamesV_.push_back("x");
00037   varNamesV_.push_back("y");
00038   varNamesV_.push_back("z");
00039   varNamesV_.push_back("sigmaX");
00040   varNamesV_.push_back("sigmaY");
00041   varNamesV_.push_back("sigmaZ");
00042 
00043 /*
00044   histoByCategoryNames_.insert( pair<string,string>("run",        "Coordinate"));
00045   histoByCategoryNames_.insert( pair<string,string>("run",        "PrimaryVertex fit-DataBase"));
00046   histoByCategoryNames_.insert( pair<string,string>("run",        "PrimaryVertex fit-BeamFit"));
00047   histoByCategoryNames_.insert( pair<string,string>("run",        "PrimaryVertex fit-Scalers"));
00048   histoByCategoryNames_.insert( pair<string,string>("run",        "PrimaryVertex-DataBase"));
00049   histoByCategoryNames_.insert( pair<string,string>("run",        "PrimaryVertex-BeamFit"));
00050   histoByCategoryNames_.insert( pair<string,string>("run",        "PrimaryVertex-Scalers"));
00051 */
00052   histoByCategoryNames_.insert( pair<string,string>("lumi",       "Lumibased BeamSpotFit"));  
00053   histoByCategoryNames_.insert( pair<string,string>("lumi",       "Lumibased PrimaryVertex"));
00054   histoByCategoryNames_.insert( pair<string,string>("lumi",       "Lumibased DataBase"));     
00055   histoByCategoryNames_.insert( pair<string,string>("lumi",       "Lumibased Scalers"));      
00056   histoByCategoryNames_.insert( pair<string,string>("lumi",       "Lumibased PrimaryVertex-DataBase fit"));
00057   histoByCategoryNames_.insert( pair<string,string>("lumi",       "Lumibased PrimaryVertex-Scalers fit"));
00058   histoByCategoryNames_.insert( pair<string,string>("validation", "Lumibased Scalers-DataBase fit"));
00059   histoByCategoryNames_.insert( pair<string,string>("validation", "Lumibased PrimaryVertex-DataBase"));
00060   histoByCategoryNames_.insert( pair<string,string>("validation", "Lumibased PrimaryVertex-Scalers"));
00061 
00062 
00063   for(vector<string>::iterator itV=varNamesV_.begin(); itV!=varNamesV_.end(); itV++){
00064     for(multimap<string,string>::iterator itM=histoByCategoryNames_.begin(); itM!=histoByCategoryNames_.end(); itM++){
00065       histosMap_[*itV][itM->first][itM->second] = 0;
00066       positionsMap_[*itV][itM->first][itM->second] = 3*numberOfValuesToSave_;//value, error, ok 
00067       ++numberOfValuesToSave_;
00068     }
00069   }
00070 }
00071 
00072 
00073 AlcaBeamMonitorClient::~AlcaBeamMonitorClient() {
00074 }
00075 
00076 
00077 //----------------------------------------------------------------------------------------------------------------------
00078 void AlcaBeamMonitorClient::beginJob() {
00079 }
00080 
00081 //----------------------------------------------------------------------------------------------------------------------
00082 void AlcaBeamMonitorClient::beginRun(const edm::Run& r, const EventSetup& context) {
00083   for(HistosContainer::iterator itM=histosMap_.begin(); itM!=histosMap_.end(); itM++){
00084     for(map<string,map<string,MonitorElement*> >::iterator itMM=itM->second.begin(); itMM!=itM->second.end(); itMM++){
00085       if(itMM->first != "run"){
00086         for(map<string,MonitorElement*>::iterator itMMM=itMM->second.begin(); itMMM!=itMM->second.end(); itMMM++){
00087           if( itMMM->second != 0){
00088             if(itMM->first == "lumi"){
00089               dbe_->removeElement(monitorName_+"Debug",itMMM->second->getName());
00090             }
00091             else if(itMM->first == "validation"){
00092               dbe_->removeElement(monitorName_+"Validation",itMMM->second->getName());
00093             }
00094             else{
00095               LogInfo("AlcaBeamMonitorClient") 
00096                 << "Unrecognized category " << itMM->first;
00097                 //assert(0);        
00098             }
00099             itMMM->second = 0;
00100           }
00101         }
00102       }
00103     }
00104   }
00105 }
00106 
00107 //----------------------------------------------------------------------------------------------------------------------
00108 void AlcaBeamMonitorClient::beginLuminosityBlock(const LuminosityBlock& iLumi, const EventSetup& iSetup) {
00109 }
00110 
00111 //----------------------------------------------------------------------------------------------------------------------
00112 void AlcaBeamMonitorClient::analyze(const Event& iEvent, const EventSetup& iSetup ){
00113 }
00114 
00115 
00116 //----------------------------------------------------------------------------------------------------------------------
00117 void AlcaBeamMonitorClient::endLuminosityBlock(const LuminosityBlock& iLumi, const EventSetup& iSetup) {
00118   MonitorElement * tmp = 0;     
00119   tmp = dbe_->get(monitorName_+"Service/hHistoLumiValues");
00120   if(!tmp){
00121     return;
00122   }
00123   valuesMap_[iLumi.id().luminosityBlock()] = vector<double>();
00124   for(int i=0; i<3*numberOfValuesToSave_; i++){
00125     valuesMap_[iLumi.id().luminosityBlock()].push_back(tmp->getTProfile()->GetBinContent(i+1)); 
00126   }
00127 }
00128 
00129 //----------------------------------------------------------------------------------------------------------------------
00130 void AlcaBeamMonitorClient::endRun(const Run& iRun, const EventSetup& context){
00131 
00132   // use this in case any LS is missing.
00133   if(valuesMap_.size() == 0){
00134     LogInfo("AlcaBeamMonitorClient") 
00135       << "The histogram " << monitorName_+"Service/hHistoLumiValues which contains all the values has not been found in any lumi!";
00136     return;
00137   }
00138   int lastLumi = (--valuesMap_.end())->first;
00139   int firstLumi = valuesMap_.begin()->first;
00140 
00141   // create and cd into new folder
00142   dbe_->setCurrentFolder(monitorName_+"Validation");
00143 
00144   LogInfo("AlcaBeamMonitorClient") 
00145     << "End of run " << iRun.id().run() << "(" << firstLumi << "-" << lastLumi << ")";
00146 
00147   string name;
00148   string title;
00149   //x,y,z,sigmaX,sigmaY,sigmaZ
00150   for(HistosContainer::iterator itM=histosMap_.begin(); itM!=histosMap_.end(); itM++){
00151     for(map<string,map<string,MonitorElement*> >::iterator itMM=itM->second.begin(); itMM!=itM->second.end(); itMM++){
00152       if(itMM->first != "run"){
00153         for(map<string,MonitorElement*>::iterator itMMM=itMM->second.begin(); itMMM!=itMM->second.end(); itMMM++){
00154           name = string("h") + itM->first + itMMM->first;
00155           title = itM->first + "_{0} " + itMMM->first;
00156           if(itMM->first == "lumi"){
00157             dbe_->setCurrentFolder(monitorName_+"Debug");
00158             itMMM->second = dbe_->book1D(name,title,lastLumi-firstLumi+1,firstLumi-0.5,lastLumi+0.5);
00159           }
00160           else if(itMM->first == "validation" && itMMM->first == "Lumibased Scalers-DataBase fit"){
00161             dbe_->setCurrentFolder(monitorName_+"Validation");
00162             itMMM->second = dbe_->book1D(name,title,lastLumi-firstLumi+1,firstLumi-0.5,lastLumi+0.5);
00163           }
00164           else if(itMM->first == "validation" && itMMM->first != "Lumibased Scalers-DataBase fit" && (itM->first == "x" || itM->first == "y" || itM->first == "z")){
00165 //LORE    else if(itMM->first == "validation" && itMMM->first != "Lumibased Scalers-DataBase fit" && (itM->first == "x" || itM->first == "y") ){
00166             dbe_->setCurrentFolder(monitorName_+"Validation");
00167             itMMM->second = dbe_->book1D(name,title,lastLumi-firstLumi+1,firstLumi-0.5,lastLumi+0.5);
00168 //LORE      itMMM->second = dbe_->bookProfile(name,title,lastLumi-firstLumi+1,firstLumi-0.5,lastLumi+0.5,-0.1,0.1,"");
00169           }
00170 //LORE    else if(itMM->first == "validation" && itMMM->first != "Lumibased Scalers-DataBase fit" && itM->first == "z" ){
00171 //LORE      dbe_->setCurrentFolder(monitorName_+"Validation");
00172 //LORE      itMMM->second = dbe_->book1D(name,title,lastLumi-firstLumi+1,firstLumi-0.5,lastLumi+0.5);
00173 //LORE      itMMM->second = dbe_->bookProfile(name,title,lastLumi-firstLumi+1,firstLumi-0.5,lastLumi+0.5,-1,1,"");
00174 //        }
00175 //        else if(itMM->first == "validation" && itMMM->first == "Lumibased Scalers-DataBase" && (itM->first == "sigmaX" || itM->first == "sigmaY") ){
00176 //          dbe_->setCurrentFolder(monitorName_+"Validation");
00177 //          itMMM->second = dbe_->bookProfile(name,title,lastLumi-firstLumi+1,firstLumi-0.5,lastLumi+0.5,-0.1,0.1,"");
00178 //        }
00179 //        else if(itMM->first == "validation" && itMMM->first == "Lumibased Scalers-DataBase" && (itM->first == "sigmaZ") ){
00180 //          dbe_->setCurrentFolder(monitorName_+"Validation");
00181 //          itMMM->second = dbe_->bookProfile(name,title,lastLumi-firstLumi+1,firstLumi-0.5,lastLumi+0.5,-10,10,"");
00182 //        }
00183 //        else if(itMM->first == "validation" && itMMM->first != "Lumibased Scalers-DataBase" && (itM->first == "sigmaX" || itM->first == "sigmaY" || itM->first == "sigmaZ") ){
00184           else if(itMM->first == "validation" && (itM->first == "sigmaX" || itM->first == "sigmaY" || itM->first == "sigmaZ") ){
00185             dbe_->setCurrentFolder(monitorName_+"Validation");
00186             itMMM->second = 0;
00187           }
00188           else{
00189             LogInfo("AlcaBeamMonitorClient") 
00190               << "Unrecognized category " << itMM->first;
00191               //assert(0);          
00192           }
00193           if(itMMM->second != 0){
00194             if(itMMM->first.find('-') != string::npos){                                     
00195               itMMM->second->setAxisTitle(string("#Delta ") + itM->first + "_{0} (cm)",2);  
00196             }
00197             else{
00198               itMMM->second->setAxisTitle(itM->first + "_{0} (cm)",2);  
00199             }
00200             itMMM->second->setAxisTitle("Lumisection",1);
00201           }
00202         }
00203       }           
00204     }
00205   }
00206 
00207   unsigned int bin=0;
00208   for(HistosContainer::iterator itH=histosMap_.begin(); itH!=histosMap_.end(); itH++){
00209     for(map<string, map<string,MonitorElement*> >::iterator itHH=itH->second.begin(); itHH!=itH->second.end(); itHH++){
00210       for(map<string,MonitorElement*>::iterator itHHH=itHH->second.begin(); itHHH!=itHH->second.end(); itHHH++){
00211         for(map<LuminosityBlockNumber_t,vector<double> >::iterator itVal = valuesMap_.begin(); itVal != valuesMap_.end(); itVal++){
00212           if(itHHH->second != 0){
00213 //          cout << positionsMap_[itH->first][itHH->first][itHHH->first] << endl;
00214             if(itVal->second[positionsMap_[itH->first][itHH->first][itHHH->first]+2] == 1){
00215               bin = itHHH->second->getTH1()->FindBin(itVal->first);
00216               itHHH->second->setBinContent(bin,itVal->second[positionsMap_[itH->first][itHH->first][itHHH->first]]);
00217               itHHH->second->setBinError(bin,itVal->second[positionsMap_[itH->first][itHH->first][itHHH->first]+1]);
00218             }
00219           }
00220         }
00221       }
00222     }
00223   }
00224 
00225 
00226   const double bigNumber = 1000000.;
00227   for(HistosContainer::iterator itH=histosMap_.begin(); itH!=histosMap_.end(); itH++){
00228     for(map<string, map<string,MonitorElement*> >::iterator itHH=itH->second.begin(); itHH!=itH->second.end(); itHH++){
00229       double min = bigNumber;
00230       double max = -bigNumber;
00231       double minDelta = bigNumber;
00232       double maxDelta = -bigNumber;
00233 //      double minDeltaProf = bigNumber;
00234 //      double maxDeltaProf = -bigNumber;
00235       if(itHH->first != "run"){
00236         for(map<string,MonitorElement*>::iterator itHHH=itHH->second.begin(); itHHH!=itHH->second.end(); itHHH++){
00237           if(itHHH->second != 0){
00238             for(int bin=1; bin<=itHHH->second->getTH1()->GetNbinsX(); bin++){
00239               if(itHHH->second->getTH1()->GetBinError(bin) != 0 || itHHH->second->getTH1()->GetBinContent(bin) != 0){
00240                 if(itHHH->first == "Lumibased BeamSpotFit" 
00241                 || itHHH->first == "Lumibased PrimaryVertex" 
00242                 || itHHH->first == "Lumibased DataBase" 
00243                 || itHHH->first == "Lumibased Scalers"){
00244                   if(min > itHHH->second->getTH1()->GetBinContent(bin)){                                                   
00245                     min = itHHH->second->getTH1()->GetBinContent(bin);
00246                   }                                                                                                        
00247                   if(max < itHHH->second->getTH1()->GetBinContent(bin)){                                                   
00248                     max = itHHH->second->getTH1()->GetBinContent(bin);
00249                   }                                                                                                        
00250                 }
00251                 else if(itHHH->first == "Lumibased PrimaryVertex-DataBase fit" 
00252                 || itHHH->first == "Lumibased PrimaryVertex-Scalers fit"
00253                 || itHHH->first == "Lumibased Scalers-DataBase fit"
00254                 || itHHH->first == "Lumibased PrimaryVertex-DataBase" 
00255                 || itHHH->first == "Lumibased PrimaryVertex-Scalers"
00256                 ){
00257                   if(minDelta > itHHH->second->getTH1()->GetBinContent(bin)){
00258                     minDelta = itHHH->second->getTH1()->GetBinContent(bin);
00259                   }
00260                   if(maxDelta < itHHH->second->getTH1()->GetBinContent(bin)){
00261                     maxDelta = itHHH->second->getTH1()->GetBinContent(bin);
00262                   }
00263                 }
00264 //              else if(itHHH->first == "Lumibased PrimaryVertex-DataBase" 
00265 //              || itHHH->first == "Lumibased PrimaryVertex-Scalers"
00267 //              ){
00268 //                if(minDelta > itHHH->second->getTProfile()->GetBinContent(bin)){
00269 //                  minDelta = itHHH->second->getTProfile()->GetBinContent(bin);
00270 //                }
00271 //                if(maxDelta < itHHH->second->getTProfile()->GetBinContent(bin)){
00272 //                  maxDelta = itHHH->second->getTProfile()->GetBinContent(bin);
00273 //                }
00274 //              }
00275                 else{
00276                   LogInfo("AlcaBeamMonitorClient")
00277                     << "The histosMap_ have a histogram named " << itHHH->first << " that I can't recognize in this loop!";
00278                   //assert(0);
00279 
00280                 }
00281               }
00282             }
00283           }
00284         }
00285         for(map<string,MonitorElement*>::iterator itHHH=itHH->second.begin(); itHHH!=itHH->second.end(); itHHH++){
00286 //        LogInfo("AlcaBeamMonitorClient")
00287 //          << itH->first << itHHH->first << " max-min=" << max-min << " delta=" << maxDelta-minDelta;
00288           if(itHHH->second != 0){
00289             if(itHHH->first == "Lumibased BeamSpotFit" 
00290             || itHHH->first == "Lumibased PrimaryVertex" 
00291             || itHHH->first == "Lumibased DataBase" 
00292             || itHHH->first == "Lumibased Scalers"){
00293               if((max == -bigNumber && min == bigNumber) || max-min == 0){
00294                 itHHH->second->getTH1()->SetMinimum(itHHH->second->getTH1()->GetMinimum()-0.01);
00295                 itHHH->second->getTH1()->SetMaximum(itHHH->second->getTH1()->GetMaximum()+0.01);
00296               }
00297               else{
00298                 itHHH->second->getTH1()->SetMinimum(min-0.1*(max-min));
00299                 itHHH->second->getTH1()->SetMaximum(max+0.1*(max-min));
00300               }
00301             }
00302             else if(itHHH->first == "Lumibased PrimaryVertex-DataBase fit" 
00303             || itHHH->first == "Lumibased PrimaryVertex-Scalers fit"
00304             || itHHH->first == "Lumibased Scalers-DataBase fit"
00305             || itHHH->first == "Lumibased PrimaryVertex-DataBase"
00306             || itHHH->first == "Lumibased PrimaryVertex-Scalers"
00307             ){
00308               if((maxDelta == -bigNumber && minDelta == bigNumber) || maxDelta-minDelta == 0){
00309                 itHHH->second->getTH1()->SetMinimum(itHHH->second->getTH1()->GetMinimum()-0.01);
00310                 itHHH->second->getTH1()->SetMaximum(itHHH->second->getTH1()->GetMaximum()+0.01);
00311               }
00312               else{
00313                 itHHH->second->getTH1()->SetMinimum(minDelta-2*(maxDelta-minDelta));
00314                 itHHH->second->getTH1()->SetMaximum(maxDelta+2*(maxDelta-minDelta));
00315               }
00316             }
00317 //          else if(itHHH->first == "Lumibased PrimaryVertex-DataBase" 
00318 //          || itHHH->first == "Lumibased PrimaryVertex-Scalers"
00320 //          ){
00321 //            if((maxDelta == -bigNumber && minDelta == bigNumber) || maxDelta-minDelta == 0){
00322 //              itHHH->second->getTProfile()->SetMinimum(itHHH->second->getTProfile()->GetMinimum()-0.01);
00323 //              itHHH->second->getTProfile()->SetMaximum(itHHH->second->getTProfile()->GetMaximum()+0.01);
00324 //            }
00325 //            else{
00326 //              itHHH->second->getTProfile()->SetMinimum(minDelta-5*(maxDelta-minDelta));
00327 //              itHHH->second->getTProfile()->SetMaximum(maxDelta+5*(maxDelta-minDelta));
00328  //             }
00329 //          }
00330             else{
00331               LogInfo("AlcaBeamMonitorClient")
00332                 << "The histosMap_ have a histogram named " << itHHH->first << " that I can't recognize in this loop!";
00333               //assert(0);
00334 
00335             }
00336           }
00337         }
00338       }
00339     }
00340   }
00341 
00342 
00343 }
00344 
00345 //----------------------------------------------------------------------------------------------------------------------
00346 void AlcaBeamMonitorClient::endJob(const LuminosityBlock& iLumi, const EventSetup& iSetup){
00347 }
00348 
00349 
00350 DEFINE_FWK_MODULE(AlcaBeamMonitorClient);