CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalPedestalsPyWrapper.cc
Go to the documentation of this file.
4 #include "TH2F.h"
5 #include "TCanvas.h"
6 #include "TStyle.h"
7 #include "TLine.h"
10 
13 
14 #include <string>
15 #include <sstream>
16 #include <algorithm>
17 #include <numeric>
18 #include <iterator>
19 #include <boost/ref.hpp>
20 #include <boost/bind.hpp>
21 #include <boost/function.hpp>
22 #include <boost/iterator/transform_iterator.hpp>
23 #include <fstream>
24 
26 
27 namespace {
28  struct Printer {
29  void doit(EcalPedestal const & item) {
30  for (int i=1; i<4; i++)
31  ss << item.mean(i) << "," << item.rms(i) <<";";
32  ss << " ";
33  }
34  std::stringstream ss;
35  };
36 }
37 
38 namespace cond {
39 
40  namespace ecalped {
41  enum Quantity { mean_x12=1, mean_x6=2, mean_x3=3 };
43 
44  float average(EcalPedestals const & peds, Quantity q) {
45  return std::accumulate(
46  boost::make_transform_iterator(peds.barrelItems().begin(),bind(&EcalPedestal::mean,_1,q)),
47  boost::make_transform_iterator(peds.barrelItems().end(),bind(&EcalPedestal::mean,_1,q)),
48  0.)/float(peds.barrelItems().size());
49  }
50 
51  void extractAverage(EcalPedestals const & peds, Quantity q, std::vector<int> const &, std::vector<float> & result) {
52  result.resize(1);
53  result[0] = average(peds,q);
54  }
55 
56  void extractSuperModules(EcalPedestals const & peds, Quantity q, std::vector<int> const & which, std::vector<float> & result) {
57  // bho...
58  }
59 
60  void extractSingleChannel(EcalPedestals const & peds, Quantity q, std::vector<int> const & which, std::vector<float> & result) {
61  for (unsigned int i=0; i<which.size();i++) {
62  // absolutely arbitraty
63  if ((unsigned int) (which[i])< peds.barrelItems().size())
64  result.push_back( peds.barrelItems()[which[i]].mean(q));
65  }
66  }
67 
68  typedef boost::function<void(EcalPedestals const & peds, Quantity q, std::vector<int> const & which, std::vector<float> & result)> PedExtractor;
69  }
70 
71  template<>
73 
76  std::vector<int> m_which;
77 
78  ecalped::Quantity const & quantity() const { return m_quantity;}
79  ecalped::How const & how() const { return m_how;}
80  std::vector<int> const & which() const { return m_which;}
81 
82 
83  void set_quantity( ecalped::Quantity i) { m_quantity=i;}
84  void set_how(ecalped::How i) {m_how=i;}
85  void set_which(std::vector<int> & i) { m_which.swap(i);}
86  };
87 
88 
89  template<>
90  class ValueExtractor<EcalPedestals>: public BaseValueExtractor<EcalPedestals> {
91  public:
92 
94  static ecalped::PedExtractor fun[3] = {
98  };
99  return fun[how];
100  }
101 
104  static What what() { return What();}
105 
108  : m_what(what)
109  {
110  // here one can make stuff really complicated... (select mean rms, 12,6,1)
111  // ask to make average on selected channels...
112  }
113 
114  void compute(Class const & it) override{
115  std::vector<float> res;
116  extractor(m_what.how())(it,m_what.quantity(),m_what.which(),res);
117  swap(res);
118  }
119 
120  private:
122 
123  };
124 
125 
126  template<>
129  std::stringstream ss;
130  EcalCondHeader header;
131  ss<<EcalPedestalsXMLTranslator::dumpXML(header,object());
132  return ss.str();
133  } // dump
134 
135 
136  class EcalPedestalsHelper: public EcalPyWrapperHelper<EcalPedestal>{
137  public:
139  protected:
141  type_vValues getValues( const std::vector<EcalPedestal> & vItems) override
142  {
143  //unsigned int totalValues = 6;
144  type_vValues vValues(total_values);
145 
146  vValues[0].first = "mean_x12";
147  vValues[1].first = "rms_x12";
148  vValues[2].first = "mean_x6";
149  vValues[3].first = "rms_x6";
150  vValues[4].first = "mean_x1";
151  vValues[5].first = "rms_x1";
152 
153  vValues[0].second = .0;
154  vValues[1].second = .0;
155  vValues[2].second = .0;
156  vValues[3].second = .0;
157  vValues[4].second = .0;
158  vValues[5].second = .0;
159 
160  //get info:
161  for(std::vector<EcalPedestal>::const_iterator iItems = vItems.begin(); iItems != vItems.end(); ++iItems){
162  vValues[0].second += iItems->mean(1);
163  vValues[1].second += iItems->rms(1);
164  vValues[2].second += iItems->mean(2);
165  vValues[3].second += iItems->rms(2);
166  vValues[4].second += iItems->mean(3);
167  vValues[5].second += iItems->rms(3);
168  }
169  return vValues;
170  }
171  };
172 
173  template<>
175  std::stringstream ss;
176  EcalPedestalsHelper helper;
177  ss << helper.printBarrelsEndcaps(object().barrelItems(), object().endcapItems());
178  return ss.str();
179  } // summary
180 
181 
182  // return the real name of the file including extension...
183  template<>
185  std::string const &,
186  std::vector<int> const&,
187  std::vector<float> const& ) const {
188  gStyle->SetPalette(1);
189  // TCanvas canvas("CC map","CC map",840,600);
190  TCanvas canvas("CC map","CC map",800,1200);
191 
192  float xmi[3] = {0.0 , 0.22, 0.78};
193  float xma[3] = {0.22, 0.78, 1.00};
194  TPad*** pad = new TPad**[6];
195  for (int gId = 0; gId < 6; gId++) {
196  pad[gId] = new TPad*[3];
197  for (int obj = 0; obj < 3; obj++) {
198  float yma = 1.- (0.17 * gId);
199  float ymi = yma - 0.15;
200  pad[gId][obj] = new TPad(Form("p_%i_%i", obj, gId),Form("p_%i_%i", obj, gId),
201  xmi[obj], ymi, xma[obj], yma);
202  pad[gId][obj]->Draw();
203  }
204  }
205 
206  const int kGains = 3;
207  const int gainValues[3] = {12, 6, 1};
208  const int kSides = 2;
209  const int kBarlRings = EBDetId::MAX_IETA;
210  const int kBarlWedges = EBDetId::MAX_IPHI;
211  const int kEndcWedgesX = EEDetId::IX_MAX;
212  const int kEndcWedgesY = EEDetId::IY_MAX;
213 
214  TH2F** barrel_m = new TH2F*[3];
215  TH2F** endc_p_m = new TH2F*[3];
216  TH2F** endc_m_m = new TH2F*[3];
217  TH2F** barrel_r = new TH2F*[3];
218  TH2F** endc_p_r = new TH2F*[3];
219  TH2F** endc_m_r = new TH2F*[3];
220  for (int gainId = 0; gainId < kGains; gainId++) {
221  barrel_m[gainId] = new TH2F(Form("EBm%i",gainId),Form("mean %i EB",gainValues[gainId]),360,0,360, 170, -85,85);
222  endc_p_m[gainId] = new TH2F(Form("EE+m%i",gainId),Form("mean %i EE+",gainValues[gainId]),100,1,101,100,1,101);
223  endc_m_m[gainId] = new TH2F(Form("EE-m%i",gainId),Form("mean %i EE-",gainValues[gainId]),100,1,101,100,1,101);
224  barrel_r[gainId] = new TH2F(Form("EBr%i",gainId),Form("rms %i EB",gainValues[gainId]),360,0,360, 170, -85,85);
225  endc_p_r[gainId] = new TH2F(Form("EE+r%i",gainId),Form("rms %i EE+",gainValues[gainId]),100,1,101,100,1,101);
226  endc_m_r[gainId] = new TH2F(Form("EE-r%i",gainId),Form("rms %i EE-",gainValues[gainId]),100,1,101,100,1,101);
227  }
228 
229  for (int sign=0; sign < kSides; sign++) {
230  int thesign = sign==1 ? 1:-1;
231 
232  for (int ieta=0; ieta<kBarlRings; ieta++) {
233  for (int iphi=0; iphi<kBarlWedges; iphi++) {
234  EBDetId id((ieta+1)*thesign, iphi+1);
235  float y = -1 - ieta;
236  if(sign == 1) y = ieta;
237  barrel_m[0]->Fill(iphi, y, object()[id.rawId()].mean_x12);
238  barrel_r[0]->Fill(iphi, y, object()[id.rawId()].rms_x12);
239  barrel_m[1]->Fill(iphi, y, object()[id.rawId()].mean_x6);
240  barrel_r[1]->Fill(iphi, y, object()[id.rawId()].rms_x6);
241  barrel_m[2]->Fill(iphi, y, object()[id.rawId()].mean_x1);
242  barrel_r[2]->Fill(iphi, y, object()[id.rawId()].rms_x1);
243  } // iphi
244  } // ieta
245 
246  for (int ix=0; ix<kEndcWedgesX; ix++) {
247  for (int iy=0; iy<kEndcWedgesY; iy++) {
248  if (! EEDetId::validDetId(ix+1,iy+1,thesign)) continue;
249  EEDetId id(ix+1,iy+1,thesign);
250  if (thesign==1) {
251  endc_p_m[0]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x12);
252  endc_p_r[0]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x12);
253  endc_p_m[1]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x6);
254  endc_p_r[1]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x6);
255  endc_p_m[2]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x1);
256  endc_p_r[2]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x1);
257  }
258  else{
259  endc_m_m[0]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x12);
260  endc_m_r[0]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x12);
261  endc_m_m[1]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x6);
262  endc_m_r[1]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x6);
263  endc_m_m[2]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x1);
264  endc_m_r[2]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x1);
265  }
266  } // iy
267  } // ix
268  } // side
269 
270  //canvas.cd(1);
271  float bmin[3] ={0.7, 0.5, 0.4};
272  float bmax[3] ={1.7, 1.0, 0.8};
273  float emin[3] ={1.5, 0.8, 0.4};
274  float emax[3] ={2.5, 1.5, 0.8};
275  TLine* l = new TLine(0., 0., 0., 0.);
276  l->SetLineWidth(1);
277  int ixSectorsEE[202] = {
278  62, 62, 61, 61, 60, 60, 59, 59, 58, 58, 56, 56, 46, 46, 44, 44, 43, 43, 42, 42,
279  41, 41, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 46, 46, 56, 56, 58, 58, 59, 59,
280  60, 60, 61, 61, 62, 62, 0,101,101, 98, 98, 96, 96, 93, 93, 88, 88, 86, 86, 81,
281  81, 76, 76, 66, 66, 61, 61, 41, 41, 36, 36, 26, 26, 21, 21, 16, 16, 14, 14, 9,
282  9, 6, 6, 4, 4, 1, 1, 4, 4, 6, 6, 9, 9, 14, 14, 16, 16, 21, 21, 26,
283  26, 36, 36, 41, 41, 61, 61, 66, 66, 76, 76, 81, 81, 86, 86, 88, 88, 93, 93, 96,
284  96, 98, 98,101,101, 0, 62, 66, 66, 71, 71, 81, 81, 91, 91, 93, 0, 62, 66, 66,
285  91, 91, 98, 0, 58, 61, 61, 66, 66, 71, 71, 76, 76, 81, 81, 0, 51, 51, 0, 44,
286  41, 41, 36, 36, 31, 31, 26, 26, 21, 21, 0, 40, 36, 36, 11, 11, 4, 0, 40, 36,
287  36, 31, 31, 21, 21, 11, 11, 9, 0, 46, 46, 41, 41, 36, 36, 0, 56, 56, 61, 61, 66, 66};
288 
289  int iySectorsEE[202] = {
290  51, 56, 56, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 61, 61, 60, 60, 59, 59, 58,
291  58, 56, 56, 46, 46, 44, 44, 43, 43, 42, 42, 41, 41, 40, 40, 41, 41, 42, 42, 43,
292  43, 44, 44, 46, 46, 51, 0, 51, 61, 61, 66, 66, 76, 76, 81, 81, 86, 86, 88, 88,
293  93, 93, 96, 96, 98, 98,101,101, 98, 98, 96, 96, 93, 93, 88, 88, 86, 86, 81, 81,
294  76, 76, 66, 66, 61, 61, 41, 41, 36, 36, 26, 26, 21, 21, 16, 16, 14, 14, 9, 9,
295  6, 6, 4, 4, 1, 1, 4, 4, 6, 6, 9, 9, 14, 14, 16, 16, 21, 21, 26, 26,
296  36, 36, 41, 41, 51, 0, 46, 46, 41, 41, 36, 36, 31, 31, 26, 26, 0, 51, 51, 56,
297  56, 61, 61, 0, 61, 61, 66, 66, 71, 71, 76, 76, 86, 86, 88, 0, 62,101, 0, 61,
298  61, 66, 66, 71, 71, 76, 76, 86, 86, 88, 0, 51, 51, 56, 56, 61, 61, 0, 46, 46,
299  41, 41, 36, 36, 31, 31, 26, 26, 0, 40, 31, 31, 16, 16, 6, 0, 40, 31, 31, 16, 16, 6};
300 
301  for (int gId = 0; gId < 3; gId++) {
302  pad[gId][0]->cd();
303  endc_m_m[gId]->SetStats(0);
304  endc_m_m[gId]->SetMaximum(225);
305  endc_m_m[gId]->SetMinimum(175);
306  endc_m_m[gId]->Draw("colz");
307  for ( int i=0; i<201; i=i+1) {
308  if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) &&
309  (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
310  l->DrawLine(ixSectorsEE[i], iySectorsEE[i],
311  ixSectorsEE[i+1], iySectorsEE[i+1]);
312  l->SetLineWidth(0.2);
313  }
314  }
315  pad[gId + 3][0]->cd();
316  endc_m_r[gId]->SetStats(0);
317  endc_m_r[gId]->SetMaximum(emax[gId]);
318  endc_m_r[gId]->SetMinimum(emin[gId]);
319  endc_m_r[gId]->Draw("colz");
320  for ( int i=0; i<201; i=i+1) {
321  if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) &&
322  (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
323  l->DrawLine(ixSectorsEE[i], iySectorsEE[i],
324  ixSectorsEE[i+1], iySectorsEE[i+1]);
325  }
326  }
327  //canvas.cd(2);
328  pad[gId][1]->cd();
329  barrel_m[gId]->SetStats(0);
330  barrel_m[gId]->SetMaximum(225);
331  barrel_m[gId]->SetMinimum(175);
332  barrel_m[gId]->Draw("colz");
333  for(int i = 0; i <17; i++) {
334  Double_t x = 20.+ (i *20);
335  l = new TLine(x,-85.,x,86.);
336  l->Draw();
337  }
338  l = new TLine(0.,0.,360.,0.);
339  l->Draw();
340  pad[gId + 3][1]->cd();
341  barrel_r[gId]->SetStats(0);
342  barrel_r[gId]->SetMaximum(bmax[gId]);
343  barrel_r[gId]->SetMinimum(bmin[gId]);
344  barrel_r[gId]->Draw("colz");
345  for(int i = 0; i <17; i++) {
346  Double_t x = 20.+ (i *20);
347  l = new TLine(x,-85.,x,86.);
348  l->Draw();
349  }
350  l = new TLine(0.,0.,360.,0.);
351  l->Draw();
352  //canvas.cd(3);
353  pad[gId][2]->cd();
354  endc_p_m[gId]->SetStats(0);
355  endc_p_m[gId]->SetMaximum(225);
356  endc_p_m[gId]->SetMinimum(175);
357  endc_p_m[gId]->Draw("colz");
358  for ( int i=0; i<201; i=i+1) {
359  if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) &&
360  (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
361  l->DrawLine(ixSectorsEE[i], iySectorsEE[i],
362  ixSectorsEE[i+1], iySectorsEE[i+1]);
363  }
364  }
365  pad[gId + 3][2]->cd();
366  endc_p_r[gId]->SetStats(0);
367  endc_p_r[gId]->SetMaximum(emax[gId]);
368  endc_p_r[gId]->SetMinimum(emin[gId]);
369  endc_p_r[gId]->Draw("colz");
370  for ( int i=0; i<201; i=i+1) {
371  if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) &&
372  (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
373  l->DrawLine(ixSectorsEE[i], iySectorsEE[i],
374  ixSectorsEE[i+1], iySectorsEE[i+1]);
375  }
376  }
377  }
378 
379  canvas.SaveAs(filename.c_str());
380  return filename;
381  } // plot
382 }
383 
384 namespace condPython {
385  template<>
387  using namespace boost::python;
388  enum_<cond::ecalped::Quantity>("Quantity")
389  .value("mean_x12",cond::ecalped::mean_x12)
390  .value("mean_x6", cond::ecalped::mean_x6)
391  .value("mean_x3", cond::ecalped::mean_x3)
392  ;
393  enum_<cond::ecalped::How>("How")
394  .value("singleChannel",cond::ecalped::singleChannel)
395  .value("bySuperModule",cond::ecalped::bySuperModule)
396  .value("all",cond::ecalped::all)
397  ;
398 
400  class_<What>("What",init<>())
401  .def("set_quantity",&What::set_quantity)
402  .def("set_how",&What::set_how)
403  .def("set_which",&What::set_which)
404  .def("quantity",&What::quantity, return_value_policy<copy_const_reference>())
405  .def("how",&What::how, return_value_policy<copy_const_reference>())
406  .def("which",&What::which, return_value_policy<copy_const_reference>())
407  ;
408  }
409 }
410 
411 
412 
int i
Definition: DBlmapReader.cc:9
def which
Definition: eostools.py:335
std::string plot(std::string const &, std::string const &, std::vector< int > const &, std::vector< float > const &) const
float average(EcalPedestals const &peds, Quantity q)
int gainId(sample_type sample)
get the gainId (2 bits)
ExtractWhat< Class > What
static const int kBarlRings
const Items & barrelItems() const
std::vector< std::pair< std::string, float > > type_vValues
double sign(double x)
static ecalped::PedExtractor & extractor(ecalped::How how)
ecalped::Quantity const & quantity() const
#define PYTHON_WRAPPER(_class, _name)
std::vector< int > const & which() const
type_vValues getValues(const std::vector< EcalPedestal > &vItems) override
static const int kSides
boost::function< void(EcalPedestals const &peds, Quantity q, std::vector< int > const &which, std::vector< float > &result)> PedExtractor
ecalped::How const & how() const
def canvas
Definition: svgfig.py:481
static std::string dumpXML(const EcalCondHeader &header, const EcalPedestals &record)
static const int kBarlWedges
T x() const
Cartesian x coordinate.
std::string summary() const
void extractAverage(EcalPedestals const &peds, Quantity q, std::vector< int > const &, std::vector< float > &result)
static const int kEndcWedgesX
void set_which(std::vector< int > &i)
tuple result
Definition: query.py:137
float mean(int i) const
Definition: EcalPedestals.h:38
void swap(std::vector< float > &v)
static const int IX_MAX
Definition: EEDetId.h:302
void compute(Class const &it) override
float rms(int i) const
Definition: EcalPedestals.h:43
static const int MAX_IPHI
Definition: EBDetId.h:144
static bool validDetId(int crystal_ix, int crystal_iy, int iz)
Definition: EEDetId.h:248
static const int MAX_IETA
Definition: EBDetId.h:143
void defineWhat< EcalPedestals >()
std::string printBarrelsEndcaps(const std::vector< T > &barrelItems, const std::vector< T > &endcapItems)
tuple filename
Definition: lut2db_cfg.py:20
static const int IY_MAX
Definition: EEDetId.h:306
std::string dump() const
void extractSingleChannel(EcalPedestals const &peds, Quantity q, std::vector< int > const &which, std::vector< float > &result)
void extractSuperModules(EcalPedestals const &peds, Quantity q, std::vector< int > const &which, std::vector< float > &result)
static const int kEndcWedgesY