CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BeamSpotObjectsPyWrapper.cc
Go to the documentation of this file.
2 
7 
8 #include "TROOT.h"
9 #include "TCanvas.h"
10 #include "TStyle.h"
11 #include "TColor.h"
12 #include "TLine.h"
13 #include "TGraph.h"
14 #include "TAxis.h"
15 #include "TMultiGraph.h"
16 #include "TLegend.h"
17 
18 #include <string>
19 #include <sstream>
20 
21 namespace cond {
22 
23  template<>
24  class ValueExtractor<BeamSpotObjects>: public BaseValueExtractor<BeamSpotObjects> {
25  public:
28  static What what() { return What();}
29 
32  {
33  // here one can make stuff really complicated...
34  }
35  void compute(Class const & it){
36  }
37  private:
38  };
39 
40  template<>
42  std::stringstream ss;
43  object().print(ss);
44  return ss.str();
45  }
46 
47  template<>
48  std::string PayLoadInspector<BeamSpotObjects>::plot(std::string const & filename,
49  std::string const &,
50  std::vector<int> const &,
51  std::vector<float> const& ) const
52  {
53 
54  TCanvas canvas("iC","iC",800,800);
55 
56  canvas.SaveAs(filename.c_str());
57  return filename;
58  }
59 
60  template<>
62  std::string const & opt_string,
63  std::vector<int> const& ints,
64  std::vector<float> const & floats,
65  std::vector<std::string> const& strings) const
66  {
67  std::stringstream ss("");
68 
69  if (strings.size() < 2)
70  return ("Error! Not enough data for initializing connection for making plots! (from \
71  template<>\
72  std::string PayLoadInspector<BeamSpotObjects>::trend_plot)");
73 
74  std::vector<std::string>::const_iterator iter_beg = strings.begin();
75 
76  std::string conString= (*iter_beg);
77  ++iter_beg;
78  std::string authPath = (*iter_beg);
79  ++iter_beg;
80 
81  //make connection object
82  DbConnection dbConn;
83 
84  //set in configuration object authentication path
85  dbConn.configuration().setAuthenticationPath(authPath);
86  dbConn.configure();
87 
88  //create session object from connection
89  DbSession dbSes=dbConn.createSession();
90 
91  //try to make connection
92  dbSes.open(conString,true);
93 
94  //start a transaction (true=readOnly)
95  dbSes.transaction().start(true);
96 
97  //get the actual object
98  boost::shared_ptr<BeamSpotObjects> ptrBeamSpot;
99 
100  //iter_beg now stands on first token in the vector
101  std::string token;
102 
103  std::vector<float> vecX;
104  std::vector<float> vecY;
105  std::vector<float> vecZ;
106  for (std::vector<float>::const_iterator iter_float = floats.begin() ;iter_beg != strings.end();++iter_beg, ++iter_float){
107  token = (*iter_beg);
108  //std::cout << token << " ";
109 
110  ptrBeamSpot = dbSes.getTypedObject<BeamSpotObjects>(token);
111 
112  //get data from the objects:
113  vecX.push_back(ptrBeamSpot->GetX());
114  vecY.push_back(ptrBeamSpot->GetY());
115  vecZ.push_back(ptrBeamSpot->GetZ());
116  std::cout << "since: "<<(*iter_float)<< " X: "<< ptrBeamSpot->GetX() << " Y: "<< ptrBeamSpot->GetY() << " Z: " << ptrBeamSpot->GetZ() << std::endl;
117 
118 
119  }
120 
121  //close db session
122  dbSes.close();
123 
124  TCanvas canvas("iC","iC",1200,1200);
125  //canvas.UseCurrentStyle();
126  //gStyle->SetPalette(1);
127  std::cout << *(floats.end() -1) << " " << *(floats.begin());
128  float max = *(floats.end() -1);
129  float min = *(floats.begin());
130 
131  unsigned int lineWidth = 2;
132  unsigned int startColor = 2;
133 
134  float result = ((max - min) / max) ;
135  if ( result >= 0.1 ){
136  canvas.SetLogx(1);
137  }
138  std::cout << "result: " << result << std::endl;
139 
140 
141  //1)
142  TGraph graphdataX(vecX.size(), static_cast<const float *>(&floats[0]), static_cast<const float *>(&vecX[0]));
143  //graphdataX.GetXaxis()->SetRangeUser(*(floats.begin()),*(floats.end() -1));
144  //graphdataX.GetXaxis()-> SetLabelSize(0.03);
145  graphdataX.SetLineColor(startColor++);
146  graphdataX.SetLineWidth(lineWidth);
147 
148  //2)
149  TGraph graphdataY(vecY.size(), static_cast<const float *>(&floats[0]), static_cast<const float *>(&vecY[0]));
150  //graphdataY.GetXaxis()->SetRangeUser(*(floats.begin()),*(floats.end() -1));
151  //graphdataY.GetXaxis()-> SetLabelSize(0.03);
152  graphdataY.SetLineColor(startColor++);
153  graphdataY.SetLineWidth(lineWidth);
154 
155  //3)
156  TGraph graphdataZ(vecZ.size(), static_cast<const float *>(&floats[0]), static_cast<const float *>(&vecZ[0]));
157  //graphdataZ.GetXaxis()->SetRangeUser(*(floats.begin()),*(floats.end() -1));
158  //graphdataZ.GetXaxis()-> SetLabelSize(0.03);
159  graphdataZ.SetLineColor(startColor++);
160  graphdataZ.SetLineWidth(lineWidth);
161 
162 
163  TMultiGraph mg;
164  std::stringstream plotTitle;
165  plotTitle.precision(0);
166  plotTitle << std::fixed;
167  //plotTitle << "BeamSpot trend graph. X0 = black, Y0 = red, Z0 = green; since(first="
168  plotTitle << "BeamSpot trend graph (first="
169  <<(double)min
170  << ", last="
171  << (double) max
172  << ", total="
173  << floats.size()
174  <<");since;value";
175  mg.SetTitle( plotTitle.str().c_str());
176 
177  //graphdataX.GetXaxis()->SetBinLabel(2,"mylabel");
178  //graphdataY.GetXaxis()->SetBinLabel(2,"mylabel");
179  //graphdataZ.GetXaxis()->SetBinLabel(2,"mylabel");
180 
181  mg.Add(&graphdataX);
182  mg.Add(&graphdataY);
183  mg.Add(&graphdataZ);
184 
185 
186  //float size1 = mg.GetXaxis()-> GetBinWidth();
187  //std::cout <<" BinWidth: " << size1 << std::endl;
188 
189  mg.Draw("LA*");
190 
191  TLegend leg(0.7,0.7,0.9,0.9,"Beam Spot Legend");
192  leg.AddEntry(&graphdataX,"X_{0}","lpf");
193  leg.AddEntry(&graphdataY,"Y_{0}","lpf");
194  leg.AddEntry(&graphdataZ,"Z_{0}","lpf");
195 
196  leg.Draw();
197 
198  ss.str("");
199  ss << filename << ".png";
200  canvas.SaveAs((ss.str()).c_str());
201  return ss.str();
202  }
203 }
204 
205 
207 
208 
209 
std::string trend_plot(std::string const &, std::string const &, std::vector< int > const &, std::vector< float > const &, std::vector< std::string > const &) const
std::string plot(std::string const &, std::string const &, std::vector< int > const &, std::vector< float > const &) const
DbTransaction & transaction()
Definition: DbSession.cc:189
ExtractWhat< Class > What
void open(const std::string &connectionString, bool readOnly=false)
Definition: DbSession.cc:144
DbConnectionConfiguration & configuration()
Definition: DbConnection.cc:89
#define PYTHON_WRAPPER(_class, _name)
#define min(a, b)
Definition: mlp_lapack.h:161
def canvas
Definition: svgfig.py:481
std::string summary() const
int start(bool readOnly=false)
start transaction
const T & max(const T &a, const T &b)
tuple result
Definition: query.py:137
DbSession createSession() const
Definition: DbConnection.cc:72
list object
Definition: dbtoconf.py:77
void setAuthenticationPath(const std::string &p)
tuple filename
Definition: lut2db_cfg.py:20
tuple cout
Definition: gather_cfg.py:121
boost::shared_ptr< T > getTypedObject(const std::string &objectId)
Definition: DbSession.h:125