CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/DQMServices/Components/src/DQMStoreStats.h

Go to the documentation of this file.
00001 #ifndef DQMStoreStats_H
00002 #define DQMStoreStats_H
00003 
00015 #include <string>
00016 #include <sstream>
00017 #include <vector>
00018 #include <iostream>
00019 #include <iomanip>
00020 #include <utility>
00021 #include <fstream>
00022 #include <sstream>
00023 
00024 #include "TFile.h"
00025 #include "TTree.h"
00026 
00027 #include <FWCore/Framework/interface/EDAnalyzer.h>
00028 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00029 #include "DQMServices/Core/interface/DQMStore.h"
00030 #include "DQMServices/Core/interface/MonitorElement.h"
00031 
00032 //
00033 // class declarations
00034 //
00035 
00036 
00041 class DQMStoreStatsSubfolder {
00042  public:
00043   DQMStoreStatsSubfolder() { totalHistos_ = 0; totalBins_ = 0; totalMemory_ = 0; totalEmptyBins_ =0; }
00044   std::string subfolderName_;
00045   unsigned int totalHistos_;
00046   unsigned int totalBins_;
00047   unsigned int totalEmptyBins_;
00048   unsigned int totalMemory_;
00049   void AddBinsF( unsigned int nBins, unsigned int nEmptyBins ) { ++totalHistos_; totalBins_ += nBins; totalEmptyBins_ += nEmptyBins; totalMemory_ += ( nBins *= sizeof( float ) ); }
00050   void AddBinsS( unsigned int nBins, unsigned int nEmptyBins ) { ++totalHistos_; totalBins_ += nBins; totalEmptyBins_ += nEmptyBins; totalMemory_ += ( nBins *= sizeof( short ) ); }
00051   void AddBinsD( unsigned int nBins, unsigned int nEmptyBins ) { ++totalHistos_; totalBins_ += nBins; totalEmptyBins_ += nEmptyBins; totalMemory_ += ( nBins *= sizeof( double ) ); }
00052 };
00053 
00058 class DQMStoreStatsSubsystem : public std::vector<DQMStoreStatsSubfolder> {
00059  public:
00060   DQMStoreStatsSubsystem() {}
00061   std::string subsystemName_;
00062 };
00063 
00064 
00069 class DQMStoreStatsTopLevel : public std::vector<DQMStoreStatsSubsystem> {
00070  public:
00071   DQMStoreStatsTopLevel() {}
00072 };
00073 
00074 template <class Item>
00075 class Iterator {
00076   public:
00077     virtual void First() = 0;
00078     virtual void Next() = 0;
00079     virtual bool IsDone() const = 0;
00080     virtual Item CurrentItem() const = 0;
00081    protected:
00082       Iterator(){;}
00083 };
00084 
00085 template <class Item>
00086 class VIterator : public Iterator<Item> 
00087 {
00088   public:
00089     VIterator(const std::vector<Item>* aVector):vector_(aVector),index(0) {;} 
00090                      
00091     virtual void First()     {index=0;}
00092     virtual void Next()      { ++index;}
00093     virtual int  size()      { return vector_->size();}
00094     virtual int  getIndex()  { return (int)index;}
00095 
00096     virtual bool IsDone() const
00097     {
00098       if(index < (unsigned int)vector_->size()) return false ;
00099       return true ;
00100     }
00101 
00102     virtual Item CurrentItem() const
00103     {
00104       return vector_->operator[](index) ;
00105     }
00106 
00107     private:
00108       const std::vector<Item> * vector_ ;
00109       unsigned int index ;
00110 };
00111 
00112 static unsigned int getId(void)
00113 {
00114   static unsigned int id=10;
00115   return ++id;
00116 }
00117 
00118 
00119 class Folder {
00120 public:
00121   Folder(const std::string name):totalHistos_(0),totalBins_(0),
00122                                  totalEmptyBins_(0),totalMemory_(0),
00123                                  id_(10),level_(0),folderName_(name),
00124                                  father_(0){;}
00125 
00126   ~Folder(void) {
00127     for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i)
00128       delete (*i);
00129   }     
00130   
00131   void setFather(Folder* e) {father_ = e;}
00132   Folder * getFather() {return father_;}
00133   const std::string & name(void)  {return folderName_;}
00134 
00135   Folder * cd(const std::string &name) {
00136     for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i)
00137       if ( (*i)->name()==name )
00138         return (*i);
00139     Folder * tmp = new Folder(name);
00140     this->add(tmp);
00141     return tmp;
00142   }
00143 
00144   void setId(unsigned int id)  {id_ = id;}
00145   unsigned int id(void)  {return id_;}
00146   void setLevel(unsigned int value) {level_=value;}
00147   unsigned int level(void) {return level_;}
00148   
00149   
00150   void add(Folder * f) {
00151     f->setFather(this);
00152     subfolders_.push_back(f);
00153     f->setLevel(level_+1);
00154     f->setId(getId());
00155   }
00156 
00157   unsigned int getHistos(void) {
00158     unsigned int result=totalHistos_;
00159     for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i)
00160       result += (*i)->getHistos();
00161     return result;
00162   }
00163   unsigned int getBins(void) {
00164     unsigned int result=totalBins_;
00165     for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i)
00166       result += (*i)->getBins();
00167     return result;
00168   }
00169   unsigned int getEmptyBins(void) {
00170     unsigned int result=totalEmptyBins_;
00171     for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i)
00172       result += (*i)->getEmptyBins();
00173     return result;
00174   }
00175   unsigned int getMemory(void) {
00176     unsigned int result=totalMemory_;
00177     for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i)
00178       result += (*i)->getMemory();
00179     return result;
00180   }
00181   void update(unsigned int bins, 
00182                       unsigned int empty, 
00183                       unsigned int memory) {
00184     totalHistos_    += 1;
00185     totalBins_      += bins;
00186     totalEmptyBins_ += empty;
00187     totalMemory_    += memory;
00188   }
00189   void dump(std::string indent)                     
00190     {
00191       indent.append(" ");
00192       std::cout << indent << "I'm a " << name() << " whose father is " << getFather()
00193                 << " with ID: " << id_
00194                 << " Histo: " << getHistos() << " Bins: " << getBins()
00195                 << " EmptyBins: " << getEmptyBins() << " Memory: " << getMemory()  
00196                 << " and my children are: " << std::endl;
00197       for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i )
00198         (*i)->dump(indent) ;
00199     }
00200   VIterator<Folder*> CreateIterator()
00201     {
00202       return VIterator<Folder *>(&subfolders_) ;
00203     }
00204 
00205   void mainrows(std::string & sql_statement)
00206     {
00207       std::stringstream s("");
00208       s << "INSERT INTO mainrows(id, symbol_id, self_count, cumulative_count, kids, self_calls, total_calls, self_paths, total_paths, pct)"
00209         " VALUES(" << id_ << ", " << id_ << ", "
00210         << getMemory() << ", " << getMemory() << ", " << subfolders_.size() << ", "
00211         << getBins() - getEmptyBins() << ", " << getBins() << ", "
00212         << getHistos() << ", " << getHistos() << ", 0.0);\n";
00213       sql_statement.append(s.str());
00214       for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i )
00215         (*i)->mainrows(sql_statement) ;
00216     }
00217 
00218   void symbols(std::string & sql_statement)
00219     {
00220       unsigned int parentid = this->getFather() ? this->getFather()->id() : id_;
00221       std::stringstream s("");
00222       s << "INSERT INTO symbols(id, name, filename_id) VALUES (" << id_ << ",\"" << folderName_ << "\", "
00223         << parentid << ");\n" ;
00224       sql_statement.append(s.str());
00225       for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i )
00226         (*i)->symbols(sql_statement) ;
00227     }
00228 
00229   void parents(std::string & sql_statement)
00230     {
00231       unsigned int parentid = this->getFather() ? this->getFather()->id() : id_;
00232       std::stringstream s("");
00233       s << "INSERT INTO parents(self_id, child_id, to_child_count, to_child_calls, to_child_paths, pct) VALUES("
00234         << parentid << "," << id_ << "," << totalMemory_ << ","
00235         << totalBins_ << "," << totalHistos_ << ",0" << ");\n";
00236       sql_statement.append(s.str());
00237       for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i )
00238         (*i)->parents(sql_statement) ;
00239     }
00240 
00241   void children(std::string & sql_statement)
00242     {
00243       unsigned int parentid = this->getFather() ? this->getFather()->id() : id_;
00244       std::stringstream s("");
00245       s << "INSERT INTO children(self_id, parent_id, from_parent_count, from_parent_calls, from_parent_paths, pct) VALUES("
00246         << id_ << "," << parentid << ","
00247         << getMemory() << "," << getBins() - getEmptyBins()
00248         << "," << totalHistos_ << ",0" << ");\n";
00249       sql_statement.append(s.str());
00250       for(std::vector<Folder*>::iterator i = subfolders_.begin(), e = subfolders_.end() ; i != e ; ++i )
00251         (*i)->children(sql_statement) ;
00252     }
00253 
00254   void mainrows_cumulative(std::string & sql_statement)
00255     {
00256       std::stringstream s("");
00257       s << "INSERT INTO mainrows(id, symbol_id, self_count, cumulative_count, kids, self_calls, total_calls, self_paths, total_paths, pct)"
00258         << " VALUES(" << id_ << "," << id_ << "," << 0 << "," << getMemory() << ", 0,"
00259         << getBins()-getEmptyBins() << "," << getBins()
00260         << ", 0, " << getHistos() << ", 0);\n";
00261       sql_statement.append(s.str());
00262     }
00263 
00264   void summary(std::string & sql_statement)
00265     {
00266       std::stringstream s("");
00267       s << "INSERT INTO summary(counter, total_count, total_freq, tick_period) VALUES (\"BINS_LIVE\","
00268         << getMemory() << "," << getBins() << ", 1);\n"; 
00269       sql_statement.append(s.str());
00270     }
00271 
00272   void files(std::string & sql_statement)
00273     {
00274       std::stringstream s("");
00275       s << "INSERT INTO files(id, name) VALUES(" << id_ << ",\"" << folderName_ << "\");\n" ;
00276       sql_statement.append(s.str());
00277     }
00278 
00279 private:
00280   unsigned int totalHistos_;
00281   unsigned int totalBins_;
00282   unsigned int totalEmptyBins_;
00283   unsigned int totalMemory_;
00284   unsigned int id_;
00285   unsigned int level_;
00286   std::string folderName_;
00287   Folder * father_;
00288   std::vector<Folder*> subfolders_;
00289 };
00290 
00294 class DQMStoreStats : public edm::EDAnalyzer {
00295 public:
00296   DQMStoreStats( const edm::ParameterSet& );
00297   ~DQMStoreStats();
00298 
00299   enum statsMode { considerAllME = 0, considerOnlyLumiProductME = 1 };
00300 
00301 protected:
00302    
00303   // BeginJob
00304   void beginJob();
00305 
00306   // BeginRun
00307   void beginRun(const edm::Run& r, const edm::EventSetup& c);
00308 
00309   // Fake Analyze
00310   void analyze(const edm::Event& e, const edm::EventSetup& c);
00311 
00312   void beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg, 
00313                             const edm::EventSetup& context);
00314 
00315   // DQM Client Diagnostic
00316   void endLuminosityBlock(const edm::LuminosityBlock& lumiSeg, 
00317                           const edm::EventSetup& c);
00318 
00319   // EndRun
00320   void endRun(const edm::Run& r, const edm::EventSetup& c);
00321 
00322   // Endjob
00323   void endJob();
00324 
00325 private:
00326 
00327   int calcstats( int );
00328   void calcIgProfDump(Folder &);
00329   void dumpMemoryProfile( void );
00330   std::pair<unsigned int, unsigned int> readMemoryEntry( void ) const;
00331   void print();
00332   
00333   DQMStore* dbe_;
00334   edm::ParameterSet parameters_;
00335 
00336   std::string subsystem_;
00337   std::string subfolder_;
00338   int nbinsglobal_;
00339   int nbinssubsys_;
00340   int nmeglobal_;
00341   int nmesubsys_;
00342   int maxbinsglobal_;
00343   int maxbinssubsys_;
00344   std::string maxbinsmeglobal_;
00345   std::string maxbinsmesubsys_;
00346 
00347   int statsdepth_ ;
00348   std::string pathnamematch_ ;
00349   int verbose_ ;
00350   
00351   std::vector<std::pair<time_t, unsigned int> > memoryHistoryVector_;
00352   time_t startingTime_;
00353   bool isOpenProcFileSuccessful_;
00354   std::stringstream procFileName_;
00355 
00356   bool runonendrun_ ;
00357   bool runonendjob_ ;
00358   bool runonendlumi_ ;
00359   bool runineventloop_ ;
00360   bool dumpMemHistory_;
00361   bool dumpToFWJR_;
00362 
00363   // ---------- member data ----------
00364 
00365 };
00366 
00367 #endif
00368