CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalPulseShapesPyWrapper.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(EcalPulseShape const & item) {
30  for(int s = 0; s<EcalPulseShape::TEMPLATESAMPLES; ++s)
31  ss << item.val(s);
32  ss << " ";
33  }
34  std::stringstream ss;
35  };
36 }
37 
38 namespace cond {
39 
40  namespace ecalpulseshape {
43 
44  float average(EcalPulseShapes const & pulseshapes, Quantity q) {
45  return std::accumulate(
46  boost::make_transform_iterator(pulseshapes.barrelItems().begin(),bind(&EcalPulseShape::val,_1,q-1)),
47  boost::make_transform_iterator(pulseshapes.barrelItems().end(),bind(&EcalPulseShape::val,_1,q-1)),
48  0.)/float(pulseshapes.barrelItems().size());
49  }
50 
51  void extractAverage(EcalPulseShapes const & pulseshapes, Quantity q, std::vector<int> const &, std::vector<float> & result) {
52  result.resize(1);
53  result[0] = average(pulseshapes,q);
54  }
55 
56  void extractSuperModules(EcalPulseShapes const & pulseshapes, Quantity q, std::vector<int> const & which, std::vector<float> & result) {
57  // bho...
58  }
59 
60  void extractSingleChannel(EcalPulseShapes const & pulseshapes, 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])< pulseshapes.barrelItems().size())
64  result.push_back( pulseshapes.barrelItems()[which[i]].val(q-1));
65  }
66  }
67 
68  typedef boost::function<void(EcalPulseShapes const & pulseshapes, Quantity q, std::vector<int> const & which, std::vector<float> & result)> PulseShapeExtractor;
69  }
70 
71  template<>
73 
76  std::vector<int> m_which;
77 
78  ecalpulseshape::Quantity const & quantity() const { return m_quantity;}
79  ecalpulseshape::How const & how() const { return m_how;}
80  std::vector<int> const & which() const { return m_which;}
81 
82 
83  void set_quantity( ecalpulseshape::Quantity i) { m_quantity=i;}
84  void set_how(ecalpulseshape::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<EcalPulseShapes>: public BaseValueExtractor<EcalPulseShapes> {
91  public:
92 
94  static ecalpulseshape::PulseShapeExtractor 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<<EcalPulseShapesXMLTranslator::dumpXML(header,object());
132  return ss.str();
133  } // dump
134 
135 
136  class EcalPulseShapesHelper: public EcalPyWrapperHelper<EcalPulseShape>{
137  public:
139  protected:
141  type_vValues getValues( const std::vector<EcalPulseShape> & vItems) override
142  {
143  type_vValues vValues(total_values);
144 
145  for(int s = 0; s<EcalPulseShape::TEMPLATESAMPLES; ++s) vValues[s].first = Form("sample_%d",s);
146 
147  //get info:
148  for(std::vector<EcalPulseShape>::const_iterator iItems = vItems.begin(); iItems != vItems.end(); ++iItems) {
149  for(int s = 0; s<EcalPulseShape::TEMPLATESAMPLES; ++s) vValues[s].second = iItems->val(s);
150  }
151  return vValues;
152  }
153  };
154 
155  template<>
157  std::stringstream ss;
158  EcalPulseShapesHelper helper;
159  ss << helper.printBarrelsEndcaps(object().barrelItems(), object().endcapItems());
160  return ss.str();
161  } // summary
162 
163 
164  // return the real name of the file including extension...
165  template<>
167  std::string const &,
168  std::vector<int> const&,
169  std::vector<float> const& ) const {
170  gStyle->SetPalette(1);
171  // TCanvas canvas("CC map","CC map",840,600);
172  TCanvas canvas("CC map","CC map",800,1200);
173 
174  float xmi[3] = {0.0 , 0.22, 0.78};
175  float xma[3] = {0.22, 0.78, 1.00};
176  TPad*** pad = new TPad**[6];
177  for (int s = 0; s < 6; s++) {
178  pad[s] = new TPad*[3];
179  for (int obj = 0; obj < 3; obj++) {
180  float yma = 1.- (0.17 * s);
181  float ymi = yma - 0.15;
182  pad[s][obj] = new TPad(Form("p_%i_%i", obj, s),Form("p_%i_%i", obj, s),
183  xmi[obj], ymi, xma[obj], yma);
184  pad[s][obj]->Draw();
185  }
186  }
187 
188  const int kSides = 2;
189  const int kBarlRings = EBDetId::MAX_IETA;
190  const int kBarlWedges = EBDetId::MAX_IPHI;
191  const int kEndcWedgesX = EEDetId::IX_MAX;
192  const int kEndcWedgesY = EEDetId::IY_MAX;
193 
194  TH2F** barrel_s = new TH2F*[EcalPulseShape::TEMPLATESAMPLES];
195  TH2F** endc_p_s = new TH2F*[EcalPulseShape::TEMPLATESAMPLES];
196  TH2F** endc_m_s = new TH2F*[EcalPulseShape::TEMPLATESAMPLES];
197  for(int s = 0; s<EcalPulseShape::TEMPLATESAMPLES; ++s) {
198  barrel_s[s] = new TH2F(Form("EBs%i",s),Form("sample %i EB",s),360,0,360, 170, -85,85);
199  endc_p_s[s] = new TH2F(Form("EE+s%i",s),Form("sample %i EE+",s),100,1,101,100,1,101);
200  endc_m_s[s] = new TH2F(Form("EE-s%i",s),Form("sample %i EE-",s),100,1,101,100,1,101);
201  }
202 
203  for (int sign=0; sign < kSides; sign++) {
204  int thesign = sign==1 ? 1:-1;
205 
206  for (int ieta=0; ieta<kBarlRings; ieta++) {
207  for (int iphi=0; iphi<kBarlWedges; iphi++) {
208  EBDetId id((ieta+1)*thesign, iphi+1);
209  float y = -1 - ieta;
210  if(sign == 1) y = ieta;
211  for(int s = 0; s<EcalPulseShape::TEMPLATESAMPLES; ++s) {
212  barrel_s[s]->Fill(iphi, y, object()[id.rawId()].pdfval[s]);
213  }
214  } // iphi
215  } // ieta
216 
217  for (int ix=0; ix<kEndcWedgesX; ix++) {
218  for (int iy=0; iy<kEndcWedgesY; iy++) {
219  if (! EEDetId::validDetId(ix+1,iy+1,thesign)) continue;
220  EEDetId id(ix+1,iy+1,thesign);
221  if (thesign==1) {
222  for(int s = 0; s<EcalPulseShape::TEMPLATESAMPLES; ++s)
223  endc_p_s[s]->Fill(ix+1,iy+1,object()[id.rawId()].pdfval[s]);
224  }
225  else {
226  for(int s = 0; s<EcalPulseShape::TEMPLATESAMPLES; ++s)
227  endc_m_s[s]->Fill(ix+1,iy+1,object()[id.rawId()].pdfval[s]);
228  }
229  } // iy
230  } // ix
231  } // side
232 
233  //canvas.cd(1);
234  TLine* l = new TLine(0., 0., 0., 0.);
235  l->SetLineWidth(1);
236  int ixSectorsEE[202] = {
237  62, 62, 61, 61, 60, 60, 59, 59, 58, 58, 56, 56, 46, 46, 44, 44, 43, 43, 42, 42,
238  41, 41, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 46, 46, 56, 56, 58, 58, 59, 59,
239  60, 60, 61, 61, 62, 62, 0,101,101, 98, 98, 96, 96, 93, 93, 88, 88, 86, 86, 81,
240  81, 76, 76, 66, 66, 61, 61, 41, 41, 36, 36, 26, 26, 21, 21, 16, 16, 14, 14, 9,
241  9, 6, 6, 4, 4, 1, 1, 4, 4, 6, 6, 9, 9, 14, 14, 16, 16, 21, 21, 26,
242  26, 36, 36, 41, 41, 61, 61, 66, 66, 76, 76, 81, 81, 86, 86, 88, 88, 93, 93, 96,
243  96, 98, 98,101,101, 0, 62, 66, 66, 71, 71, 81, 81, 91, 91, 93, 0, 62, 66, 66,
244  91, 91, 98, 0, 58, 61, 61, 66, 66, 71, 71, 76, 76, 81, 81, 0, 51, 51, 0, 44,
245  41, 41, 36, 36, 31, 31, 26, 26, 21, 21, 0, 40, 36, 36, 11, 11, 4, 0, 40, 36,
246  36, 31, 31, 21, 21, 11, 11, 9, 0, 46, 46, 41, 41, 36, 36, 0, 56, 56, 61, 61, 66, 66};
247 
248  int iySectorsEE[202] = {
249  51, 56, 56, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 61, 61, 60, 60, 59, 59, 58,
250  58, 56, 56, 46, 46, 44, 44, 43, 43, 42, 42, 41, 41, 40, 40, 41, 41, 42, 42, 43,
251  43, 44, 44, 46, 46, 51, 0, 51, 61, 61, 66, 66, 76, 76, 81, 81, 86, 86, 88, 88,
252  93, 93, 96, 96, 98, 98,101,101, 98, 98, 96, 96, 93, 93, 88, 88, 86, 86, 81, 81,
253  76, 76, 66, 66, 61, 61, 41, 41, 36, 36, 26, 26, 21, 21, 16, 16, 14, 14, 9, 9,
254  6, 6, 4, 4, 1, 1, 4, 4, 6, 6, 9, 9, 14, 14, 16, 16, 21, 21, 26, 26,
255  36, 36, 41, 41, 51, 0, 46, 46, 41, 41, 36, 36, 31, 31, 26, 26, 0, 51, 51, 56,
256  56, 61, 61, 0, 61, 61, 66, 66, 71, 71, 76, 76, 86, 86, 88, 0, 62,101, 0, 61,
257  61, 66, 66, 71, 71, 76, 76, 86, 86, 88, 0, 51, 51, 56, 56, 61, 61, 0, 46, 46,
258  41, 41, 36, 36, 31, 31, 26, 26, 0, 40, 31, 31, 16, 16, 6, 0, 40, 31, 31, 16, 16, 6};
259 
260  int ipad=0;
261  // plot only the measured ones, not the extrapolated
262  for(int s = 0; s<7; ++s) {
263  // plot only the extrapolated ones
264  // for(int s = 7; s<12; ++s) {
265  // do not plot the maximum sample, which is 1 by default
266  if(s==2) continue;
267  pad[ipad][0]->cd();
268  endc_m_s[s]->SetStats(0);
269  endc_m_s[s]->SetMaximum(1);
270  endc_m_s[s]->SetMinimum(0);
271  endc_m_s[s]->Draw("colz");
272  for ( int i=0; i<201; i=i+1) {
273  if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) &&
274  (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
275  l->DrawLine(ixSectorsEE[i], iySectorsEE[i],
276  ixSectorsEE[i+1], iySectorsEE[i+1]);
277  l->SetLineWidth(0.2);
278  }
279  }
280  //canvas.cd(2);
281  pad[ipad][1]->cd();
282  barrel_s[s]->SetStats(0);
283  barrel_s[s]->SetMaximum(1);
284  barrel_s[s]->SetMinimum(0);
285  barrel_s[s]->Draw("colz");
286  for(int i = 0; i <17; i++) {
287  Double_t x = 20.+ (i *20);
288  l = new TLine(x,-85.,x,86.);
289  l->Draw();
290  l->SetLineWidth(0.2);
291  }
292  l = new TLine(0.,0.,360.,0.);
293  l->Draw();
294  //canvas.cd(3);
295  pad[ipad][2]->cd();
296  endc_p_s[s]->SetStats(0);
297  endc_p_s[s]->SetMaximum(1);
298  endc_p_s[s]->SetMinimum(0);
299  endc_p_s[s]->Draw("colz");
300  for ( int i=0; i<201; i=i+1) {
301  if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) &&
302  (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
303  l->DrawLine(ixSectorsEE[i], iySectorsEE[i],
304  ixSectorsEE[i+1], iySectorsEE[i+1]);
305  l->SetLineWidth(0.2);
306  }
307  }
308  ipad++;
309  }
310 
311  canvas.SaveAs(filename.c_str());
312  return filename;
313  } // plot
314 }
315 
316 namespace condPython {
317  template<>
319  using namespace boost::python;
320  enum_<cond::ecalpulseshape::Quantity>("Quantity")
322  .value("sample_1",cond::ecalpulseshape::sample_1)
323  .value("sample_2",cond::ecalpulseshape::sample_2)
324  .value("sample_3",cond::ecalpulseshape::sample_3)
325  .value("sample_4",cond::ecalpulseshape::sample_4)
326  .value("sample_5",cond::ecalpulseshape::sample_5)
327  .value("sample_6",cond::ecalpulseshape::sample_6)
328  .value("sample_7",cond::ecalpulseshape::sample_7)
329  .value("sample_8",cond::ecalpulseshape::sample_8)
330  .value("sample_9",cond::ecalpulseshape::sample_9)
331  ;
332  enum_<cond::ecalpulseshape::How>("How")
334  .value("bySuperModule",cond::ecalpulseshape::bySuperModule)
335  .value("all",cond::ecalpulseshape::all)
336  ;
337 
339  class_<What>("What",init<>())
340  .def("set_quantity",&What::set_quantity)
341  .def("set_how",&What::set_how)
342  .def("set_which",&What::set_which)
343  .def("quantity",&What::quantity, return_value_policy<copy_const_reference>())
344  .def("how",&What::how, return_value_policy<copy_const_reference>())
345  .def("which",&What::which, return_value_policy<copy_const_reference>())
346  ;
347  }
348 }
349 
350 
351 
static ecalpulseshape::PulseShapeExtractor & extractor(ecalpulseshape::How how)
int i
Definition: DBlmapReader.cc:9
float average(EcalPulseShapes const &pulseshapes, Quantity q)
def which
Definition: eostools.py:333
std::string plot(std::string const &, std::string const &, std::vector< int > const &, std::vector< float > const &) const
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 const int TEMPLATESAMPLES
#define PYTHON_WRAPPER(_class, _name)
boost::function< void(EcalPulseShapes const &pulseshapes, Quantity q, std::vector< int > const &which, std::vector< float > &result)> PulseShapeExtractor
ecalpulseshape::Quantity const & quantity() const
static const int kSides
type_vValues getValues(const std::vector< EcalPulseShape > &vItems) override
def canvas
Definition: svgfig.py:481
static const int kBarlWedges
U second(std::pair< T, U > const &p)
std::string summary() const
static const int kEndcWedgesX
float val(int isample) const
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
tuple result
Definition: query.py:137
void swap(std::vector< float > &v)
static const int IX_MAX
Definition: EEDetId.h:302
static const int MAX_IPHI
Definition: EBDetId.h:144
void extractSuperModules(EcalPulseShapes const &pulseshapes, Quantity q, std::vector< int > const &which, std::vector< float > &result)
void extractSingleChannel(EcalPulseShapes const &pulseshapes, Quantity q, std::vector< int > const &which, std::vector< float > &result)
static bool validDetId(int crystal_ix, int crystal_iy, int iz)
Definition: EEDetId.h:248
static const int MAX_IETA
Definition: EBDetId.h:143
ecalpulseshape::How const & how() const
void defineWhat< EcalPulseShapes >()
std::string printBarrelsEndcaps(const std::vector< T > &barrelItems, const std::vector< T > &endcapItems)
static std::string dumpXML(const EcalCondHeader &header, const EcalPulseShapes &record)
void extractAverage(EcalPulseShapes const &pulseshapes, Quantity q, std::vector< int > const &, std::vector< float > &result)
tuple filename
Definition: lut2db_cfg.py:20
static const int IY_MAX
Definition: EEDetId.h:306
std::vector< int > const & which() const
Definition: DDAxes.h:10
void set_quantity(ecalpulseshape::Quantity i)
std::string dump() const
static const int kEndcWedgesY