CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/CondCore/Utilities/interface/PayLoadInspector.h

Go to the documentation of this file.
00001 #ifndef  PayLoadInspector_H
00002 #define  PayLoadInspector_H
00003 #include "CondCore/ORA/interface/Object.h"
00004 #include "CondCore/IOVService/interface/IOVProxy.h"
00005 #include "CondCore/DBCommon/interface/DbTransaction.h"
00006 #include "CondCore/ORA/interface/PoolToken.h"
00007 #include "TFile.h"
00008 #include "Cintex/Cintex.h"
00009 #include <sstream>
00010 #include <iostream>
00011 //#include <fstream>
00012 #include <string>
00013 #include <utility>
00014 #include <vector>
00015 
00016 namespace cond {
00017   // to be moved elsewhere
00018   class PoolTransactionSentry {
00019   public:
00020     PoolTransactionSentry(){}
00021     explicit PoolTransactionSentry(cond::DbSession & db) : 
00022       elem(new Elem(db)){}      
00023   private:
00024     struct Elem {
00025       Elem(cond::DbSession & db) : pooldb(db){
00026         pooldb.transaction().start(true);
00027       }
00028       ~Elem() { pooldb.transaction().commit();}
00029       cond::DbSession pooldb;
00030     };
00031     boost::shared_ptr<Elem> elem;
00032       
00033   };
00034 
00035   template<typename T>
00036   class BaseValueExtractor {
00037   public:
00038     typedef T Class;
00039 
00040     BaseValueExtractor(){}
00041     
00042     virtual ~BaseValueExtractor(){}
00043     void computeW(Class const &o){
00044       reset();
00045       compute(o);
00046     }
00047     std::vector<float> const & values() const { return m_values;}
00048   protected:
00049     void add(float v) { m_values.push_back(v); }
00050     void swap(std::vector<float> & v) {m_values.swap(v);}
00051   private:
00052     void reset() { m_values.clear();}
00053     virtual void compute(Class const &){}
00054     
00055     
00056   private:
00057     std::vector<float> m_values;
00058   };
00059 
00060 
00061   // a collection of enumerators, strings, ints
00062   template<typename T>
00063   struct ExtractWhat {
00064 
00065   };
00066 
00067 
00068 
00069   template<typename T>
00070   class ValueExtractor : public  BaseValueExtractor<T> {
00071   public:
00072     typedef T Class;
00073     typedef ExtractWhat<Class> What;
00074     ValueExtractor(){}
00075     ValueExtractor(What const &){}
00076     static What what() { return What();}
00077   private:
00078     void compute(Class const &){}
00079   private:
00080   };
00081 
00082   template<typename DataT>
00083   class PayLoadInspector {
00084   public:
00085     typedef DataT Class;
00086     typedef ValueExtractor<DataT> Extractor;
00087     
00088     PayLoadInspector() {}
00089     
00090     ~PayLoadInspector() {
00091       m_object.destruct();
00092     }
00093     
00094     PayLoadInspector(const cond::IOVElementProxy & elem): m_since(elem.since()), m_token(elem.token()) {
00095       ROOT::Cintex::Cintex::Enable();
00096       cond::DbSession db = elem.db();
00097       db.transaction().start(true);
00098       load(db,m_token);
00099       db.transaction().commit();
00100     } 
00101     
00102     std::string dumpXML(std::string filename) const {
00103       size_t pos = filename.find(".xml");
00104       if(pos == std::string::npos)
00105         filename.append(".xml");
00107       TFile * xml =0;
00108       xml = TFile::Open(filename.c_str(), "recreate");
00109       xml->WriteObjectAny(m_object.address(), m_object.typeName().c_str(), filename.c_str());
00110       xml->Close();
00111       return filename;
00112     }
00113     
00114     std::string dump() const {
00115       std::ostringstream ss; 
00116       //token parser
00117       std::pair<std::string,int> oidData = parseToken( m_token );
00118       ss << m_since << "_"<< oidData.first << "_" << oidData.second;
00119       //std::string filename(ss.str()+".xml");
00120       return this->dumpXML(ss.str()+".xml");
00121     }
00122     
00123     // specialize...
00124     std::string summary() const {
00125       std::ostringstream os;
00126       os << std::endl;
00127       return os.str();
00128     }
00129 
00130     // return the real name of the file including extension...
00131     std::string plot(std::string const & /* filename */,
00132                      std::string const &, 
00133                      std::vector<int> const&, std::vector<float> const& ) const {return "";}
00134 
00135 
00136     // return the real name of the file including extension...
00137     std::string trend_plot(std::string const & /* filename */,//
00138                            std::string const &, 
00139                            std::vector<int> const&, std::vector<float> const&, std::vector<std::string> const&) const {return "";}
00140 
00141     // summary, which takes arguments
00142     std::string summary_adv(//int const &,
00143                             std::string const &, 
00144                             std::vector<int> const&, std::vector<float> const&, std::vector<std::string> const&) const {return "";}
00145     
00146     //returns a file in the format chosen by the user (typically root)
00147     std::string dumpFile(std::string const & /* filename */,//
00148                          std::string const &, 
00149                          std::vector<int> const&, std::vector<float> const&, std::vector<std::string> const&) const {return "";}
00150     
00151     void extract(Extractor & extractor) const {extractor.computeW(object()); }
00152 
00153     Class const & object() const {
00154       return *(m_object.cast<Class>());
00155     }
00156     
00157     ora::Object const & ora_object() const { 
00158       return m_object; 
00159     }
00160     
00161   private:
00162     bool load( cond::DbSession & db, std::string const & token) {
00163       bool ok = false;
00164       //m_Data =  db.getTypedObject<DataT>(token);
00165       m_object = db.getObject(token);
00166       //if (m_Data.get()) ok =  true;
00167       if(m_object.address()) ok = true;    
00168       return ok;
00169     }
00170     
00171   private:
00172     //boost::shared_ptr<DataT> m_Data;
00173     ora::Object m_object;
00174     cond::Time_t m_since;
00175     std::string m_token;
00176   };
00177 
00178 }
00179 
00180 #endif //   PayLoadInspector_H