CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/CondCore/BeamSpotPlugins/plugins/BeamSpotObjectsPyWrapper.cc

Go to the documentation of this file.
00001 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
00002 
00003 #include "CondCore/DBCommon/interface/DbConnection.h"
00004 #include "CondCore/DBCommon/interface/DbTransaction.h"
00005 #include "CondCore/Utilities/interface/PayLoadInspector.h"
00006 #include "CondCore/Utilities/interface/InspectorPythonWrapper.h"
00007 
00008 #include "TROOT.h"
00009 #include "TCanvas.h"
00010 #include "TStyle.h"
00011 #include "TColor.h"
00012 #include "TLine.h"
00013 #include "TGraph.h"
00014 #include "TAxis.h"
00015 #include "TMultiGraph.h"
00016 #include "TLegend.h"
00017 
00018 #include <string>
00019 #include <sstream>
00020 
00021 namespace cond {
00022 
00023         template<>
00024         class ValueExtractor<BeamSpotObjects>: public  BaseValueExtractor<BeamSpotObjects> {
00025         public:
00026                 typedef BeamSpotObjects Class;
00027                 typedef ExtractWhat<Class> What;
00028                 static What what() { return What();}
00029 
00030                 ValueExtractor(){}
00031                 ValueExtractor(What const & what)
00032                 {
00033                         // here one can make stuff really complicated...
00034                 }
00035                 void compute(Class const & it){
00036                 }
00037         private:
00038         };
00039 
00040         template<>
00041         std::string PayLoadInspector<BeamSpotObjects>::summary() const {
00042                 std::stringstream ss;
00043                 object().print(ss);
00044                 return ss.str();
00045         }
00046 
00047         template<>
00048         std::string PayLoadInspector<BeamSpotObjects>::plot(std::string const & filename,
00049                 std::string const &,
00050                 std::vector<int> const &,
00051                 std::vector<float> const& ) const 
00052         {
00053 
00054                 TCanvas canvas("iC","iC",800,800);    
00055 
00056                 canvas.SaveAs(filename.c_str());
00057                 return filename;
00058         }
00059 
00060         template<>
00061         std::string PayLoadInspector<BeamSpotObjects>::trend_plot(std::string const & filename,
00062                 std::string const & opt_string,
00063                 std::vector<int> const& ints,
00064                 std::vector<float> const & floats,
00065                 std::vector<std::string> const& strings) const 
00066         {
00067                 std::stringstream ss("");
00068 
00069                 if (strings.size() < 2)
00070                         return ("Error! Not enough data for initializing connection for making plots! (from \
00071                                         template<>\
00072                                         std::string PayLoadInspector<BeamSpotObjects>::trend_plot)");
00073 
00074                 std::vector<std::string>::const_iterator iter_beg = strings.begin();
00075 
00076                 std::string conString= (*iter_beg); 
00077                 ++iter_beg;
00078                 std::string authPath = (*iter_beg);
00079                 ++iter_beg;
00080 
00081                 //make connection object
00082                 DbConnection dbConn;
00083 
00084                 //set in configuration object authentication path
00085                 dbConn.configuration().setAuthenticationPath(authPath);
00086                 dbConn.configure();
00087 
00088                 //create session object from connection
00089                 DbSession dbSes=dbConn.createSession();
00090 
00091                 //try to make connection
00092                 dbSes.open(conString,true);
00093 
00094                 //start a transaction (true=readOnly)
00095                 dbSes.transaction().start(true);
00096 
00097                 //get the actual object
00098                 boost::shared_ptr<BeamSpotObjects> ptrBeamSpot;
00099 
00100                 //iter_beg now stands on first token in the vector
00101                 std::string token;
00102 
00103                 std::vector<float> vecX;
00104                 std::vector<float> vecY;
00105                 std::vector<float> vecZ;
00106                 for (std::vector<float>::const_iterator iter_float = floats.begin() ;iter_beg != strings.end();++iter_beg, ++iter_float){
00107                         token = (*iter_beg);
00108                         //std::cout << token << " ";
00109                         
00110                         ptrBeamSpot = dbSes.getTypedObject<BeamSpotObjects>(token);
00111 
00112                         //get data from the objects:
00113                         vecX.push_back(ptrBeamSpot->GetX());
00114                         vecY.push_back(ptrBeamSpot->GetY());
00115                         vecZ.push_back(ptrBeamSpot->GetZ());
00116                         std::cout << "since: "<<(*iter_float)<< " X: "<< ptrBeamSpot->GetX() << " Y: "<< ptrBeamSpot->GetY() << " Z: " << ptrBeamSpot->GetZ() << std::endl;
00117 
00118 
00119                 }
00120                 
00121                 //close db session
00122                 dbSes.close();
00123 
00124                 TCanvas canvas("iC","iC",1200,1200); 
00125                 //canvas.UseCurrentStyle();
00126                 //gStyle->SetPalette(1);
00127                 std::cout << *(floats.end() -1) << "   " << *(floats.begin());
00128                 float max = *(floats.end() -1);
00129                 float min =  *(floats.begin());
00130 
00131                 unsigned int lineWidth = 2;
00132                 unsigned int startColor = 2;
00133 
00134                 float result = ((max - min) / max) ;
00135                 if ( result >= 0.1 ){
00136                         canvas.SetLogx(1);
00137                 }
00138                 std::cout << "result: " << result << std::endl;
00139 
00140         
00141         //1)
00142                 TGraph graphdataX(vecX.size(), static_cast<const float *>(&floats[0]), static_cast<const float *>(&vecX[0]));
00143                 //graphdataX.GetXaxis()->SetRangeUser(*(floats.begin()),*(floats.end() -1));
00144                 //graphdataX.GetXaxis()-> SetLabelSize(0.03);
00145                 graphdataX.SetLineColor(startColor++);
00146                 graphdataX.SetLineWidth(lineWidth);
00147 
00148         //2)
00149                 TGraph graphdataY(vecY.size(), static_cast<const float *>(&floats[0]), static_cast<const float *>(&vecY[0]));
00150                 //graphdataY.GetXaxis()->SetRangeUser(*(floats.begin()),*(floats.end() -1));
00151                 //graphdataY.GetXaxis()-> SetLabelSize(0.03);
00152                 graphdataY.SetLineColor(startColor++);
00153                 graphdataY.SetLineWidth(lineWidth);
00154 
00155         //3)
00156                 TGraph graphdataZ(vecZ.size(), static_cast<const float *>(&floats[0]), static_cast<const float *>(&vecZ[0]));
00157                 //graphdataZ.GetXaxis()->SetRangeUser(*(floats.begin()),*(floats.end() -1));
00158                 //graphdataZ.GetXaxis()-> SetLabelSize(0.03);
00159                 graphdataZ.SetLineColor(startColor++);
00160                 graphdataZ.SetLineWidth(lineWidth);
00161 
00162 
00163                 TMultiGraph mg;
00164                 std::stringstream plotTitle;
00165                 plotTitle.precision(0);
00166                 plotTitle << std::fixed;
00167                 //plotTitle << "BeamSpot trend graph. X0 = black, Y0 = red, Z0 = green; since(first="
00168                 plotTitle << "BeamSpot trend graph (first="
00169                                         <<(double)min 
00170                         << ", last=" 
00171                         << (double) max 
00172                         << ", total="
00173                         << floats.size()
00174                         <<");since;value";
00175                 mg.SetTitle( plotTitle.str().c_str());
00176 
00177                 //graphdataX.GetXaxis()->SetBinLabel(2,"mylabel");
00178                 //graphdataY.GetXaxis()->SetBinLabel(2,"mylabel");
00179                 //graphdataZ.GetXaxis()->SetBinLabel(2,"mylabel");
00180 
00181                 mg.Add(&graphdataX);
00182                 mg.Add(&graphdataY);
00183                 mg.Add(&graphdataZ);
00184                 
00185 
00186                 //float size1 = mg.GetXaxis()-> GetBinWidth();
00187                 //std::cout <<" BinWidth: " << size1 << std::endl;
00188 
00189                 mg.Draw("LA*");
00190 
00191                 TLegend leg(0.7,0.7,0.9,0.9,"Beam Spot Legend");
00192                 leg.AddEntry(&graphdataX,"X_{0}","lpf");
00193                 leg.AddEntry(&graphdataY,"Y_{0}","lpf");
00194                 leg.AddEntry(&graphdataZ,"Z_{0}","lpf");
00195 
00196                 leg.Draw();
00197 
00198                 ss.str("");
00199                 ss << filename << ".png";
00200                 canvas.SaveAs((ss.str()).c_str());
00201                 return ss.str();
00202         }
00203 }
00204 
00205 
00206 PYTHON_WRAPPER(BeamSpotObjects,BeamSpotObjects);
00207 
00208 
00209