CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SumHistoCalibration.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ImpactParameterCalibration
4 // Class: ImpactParameterCalibration
5 //
13 //
14 // Original Author: Jeremy Andrea
15 // Created: Wed Mar 5 19:17:38 CEST 2008
16 // $Id: SumHistoCalibration.cc,v 1.9 2012/07/05 15:07:03 eulisse Exp $
17 //
18 //
19 // system include files
20 
30 
34 
35 
39 
42 
43 
47 
49 
50 
51 
52 #include "RVersion.h"
53 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
54 #include "TBufferFile.h"
55 typedef TBufferFile MyTBuffer;
56 #else
57 #include "TBuffer.h"
58 typedef TBuffer MyTBuffer;
59 #endif
60 
61 #include <TClass.h>
62 #include <TBufferXML.h>
63 #include <iostream>
64 #include <fstream>
65 #include <sstream>
66 #include <string>
67 #include <vector>
68 #include <memory>
69 
70 #include "TrackClassMatch.h"
71 
72 
73 using namespace reco;
74 using namespace std;
75 //
76 // class decleration
77 //
78 
79 
80 
82  public:
83  explicit SumHistoCalibration(const edm::ParameterSet&);
85 
86 
87  private:
88  virtual void beginJob() ;
89  virtual void analyze(const edm::Event&, const edm::EventSetup&);
90  virtual void endJob() ;
92 
93  TrackProbabilityCalibration * fromXml(edm::FileInPath xmlCalibration);
94 
95 
96  std::vector<std::string> m_xmlilelist2d;
97  std::vector<std::string> m_xmlilelist3d;
98  bool m_sum2D;
99  bool m_sum3D;
100  unsigned int minLoop, maxLoop;
101  TrackProbabilityCalibration * m_calibration[2];
102 
103 
104 
105 };
106 
108 {
109  m_xmlilelist2d = iConfig.getParameter<std::vector<std::string> >("xmlfiles2d");
110  m_xmlilelist3d = iConfig.getParameter<std::vector<std::string> >("xmlfiles3d");
111  m_sum2D = iConfig.getParameter<bool>("sum2D");
112  m_sum3D = iConfig.getParameter<bool>("sum3D");
113 }
114 
115 
117 {
118 }
119 
120 
121 void
123 {
124  using namespace edm;
125  using namespace reco;
126  using namespace std;
127 
128 }
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 // ------------ method called once each job just before starting event loop ------------
140 void
142 {
143  if(m_sum2D && m_sum3D){minLoop = 0; maxLoop =1;}
144  if(m_sum2D && !m_sum3D){minLoop = 0; maxLoop =0;}
145  if(!m_sum2D && m_sum3D){minLoop = 1; maxLoop =1;}
146  using namespace edm;
149 
150  const TrackProbabilityCalibration * ca[2];
153  ca[0] = fromXml(fip);
154  ca[1] = fromXml(fip2);
155 
156  for(unsigned int i=minLoop;i <=maxLoop ;i++)
157  for(unsigned int j=0;j<ca[i]->data.size() ; j++)
158  {
160  e.category=ca[i]->data[j].category;
161  e.histogram=ca[i]->data[j].histogram;
162  m_calibration[i]->data.push_back(e);
163  }
164 
165  delete ca[0];
166  delete ca[1];
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 }
181 
183 {
184  std::ifstream xmlFile(xmlCalibration.fullPath().c_str());
185  if (!xmlFile.good())
186  throw cms::Exception("BTauFakeMVAJetTagConditions")
187  << "File \"" << xmlCalibration.fullPath()
188  << "\" could not be opened for reading."
189  << std::endl;
190  std::ostringstream ss;
191  ss << xmlFile.rdbuf();
192  xmlFile.close();
193  TClass *classType = 0;
194  void *ptr = TBufferXML(TBuffer::kRead).ConvertFromXMLAny(
195  ss.str().c_str(), &classType, kTRUE, kFALSE);
196  if (!ptr)
197  throw cms::Exception("SumHistoCalibration")
198  << "Unknown error parsing XML serialization"
199  << std::endl;
200 
201  if (std::strcmp(classType->GetName(),
202  "TrackProbabilityCalibration")) {
203  classType->Destructor(ptr);
204  throw cms::Exception("SumHistoCalibration")
205  << "Serialized object has wrong C++ type."
206  << std::endl;
207  }
208 
209  return static_cast<TrackProbabilityCalibration*>(ptr);
210 }
211 
212 
213 
214 
215 // ------------ method called once each job just after ending the event loop ------------
216 void
218 
219 
220  using namespace edm;
221  using namespace reco;
222  using namespace std;
223 
224 
225 
226 
227  if(m_sum3D){
228  for(unsigned int itFile =1; itFile< m_xmlilelist3d.size(); itFile++){
229  edm::FileInPath fip(m_xmlilelist3d[itFile]);
230  const TrackProbabilityCalibration *ca = fromXml(fip);
231  for(unsigned int j=0;j<ca->data.size() ; j++)
232  {
233  for(int k = 0; k< m_calibration[0]->data[j].histogram.numberOfBins(); k++){
234  m_calibration[0]->data[j].histogram.setBinContent(k, ca->data[j].histogram.binContent(k)
235  + m_calibration[0]->data[j].histogram.binContent(k));
236  }
237  }
238  delete ca;
239  }
240  }
241  if(m_sum2D){
242  for(unsigned int itFile =1; itFile< m_xmlilelist2d.size(); itFile++){
243  edm::FileInPath fip(m_xmlilelist2d[itFile]);
245  for(unsigned int j=0;j<ca->data.size() ; j++)
246  {
247  for(int k = 0; k< m_calibration[1]->data[j].histogram.numberOfBins(); k++){
248  m_calibration[1]->data[j].histogram.setBinContent(k,ca->data[j].histogram.binContent(k)
249  + m_calibration[1]->data[j].histogram.binContent(k));
250  }
251  }
252  delete ca;
253  }
254  }
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269  if(config.getParameter<bool>("writeToDB"))
270  {
272  if( !mydbservice.isAvailable() ) return;
273  //mydbservice->createNewIOV<TrackProbabilityCalibration>(m_calibration[0], mydbservice->endOfTime(),"BTagTrackProbability3DRcd");
274  //mydbservice->createNewIOV<TrackProbabilityCalibration>(m_calibration[1], mydbservice->endOfTime(),"BTagTrackProbability2DRcd");
275  mydbservice->createNewIOV<TrackProbabilityCalibration>(m_calibration[0], mydbservice->beginOfTime(), mydbservice->endOfTime(),"BTagTrackProbability3DRcd");
276  mydbservice->createNewIOV<TrackProbabilityCalibration>(m_calibration[1], mydbservice->beginOfTime(), mydbservice->endOfTime(),"BTagTrackProbability2DRcd");
277 
278  }
279 
280 
281  if(config.getParameter<bool>("writeToRootXML"))
282  {
283  std::ofstream of2("2d.xml");
284  TBufferXML b2(TBuffer::kWrite);
285  of2 << b2.ConvertToXML(static_cast<void*>(m_calibration[1]),
286  TClass::GetClass("TrackProbabilityCalibration"),
287  kTRUE, kFALSE);
288  of2.close();
289  std::ofstream of3("3d.xml");
290  TBufferXML b3(TBuffer::kWrite);
291  of3 << b3.ConvertToXML(static_cast<void*>(m_calibration[0]),
292  TClass::GetClass("TrackProbabilityCalibration"),
293  kTRUE, kFALSE);
294  of3.close();
295  }
296 
297 
298  if(config.getParameter<bool>("writeToBinary"))
299  {
300  std::ofstream ofile("2d.dat");
301  MyTBuffer buffer(TBuffer::kWrite);
302  buffer.StreamObject(const_cast<void*>(static_cast<const void*>(m_calibration[1])),
303  TClass::GetClass("TrackProbabilityCalibration"));
304  Int_t size = buffer.Length();
305  ofile.write(buffer.Buffer(),size);
306  ofile.close();
307 
308  std::ofstream ofile3("3d.dat");
309  MyTBuffer buffer3(TBuffer::kWrite);
310  buffer3.StreamObject(const_cast<void*>(static_cast<const void*>(m_calibration[0])),
311  TClass::GetClass("TrackProbabilityCalibration"));
312  Int_t size3 = buffer3.Length();
313  ofile3.write(buffer3.Buffer(),size3);
314  ofile3.close();
315  }
316 
317 
318 
319 
320 
321 }
322 
323 //define this as a plug-in
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
std::vector< std::string > m_xmlilelist2d
virtual void analyze(const edm::Event &, const edm::EventSetup &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
TBufferFile MyTBuffer
void beginJob()
Definition: Breakpoints.cc:15
int iEvent
Definition: GenABIO.cc:243
std::vector< std::string > m_xmlilelist3d
bool isAvailable() const
Definition: Service.h:47
int j
Definition: DBlmapReader.cc:9
edm::ParameterSet config
TrackProbabilityCalibration * fromXml(edm::FileInPath xmlCalibration)
void createNewIOV(T *firstPayloadObj, cond::Time_t firstSinceTime, cond::Time_t firstTillTime, const std::string &recordName, bool withlogging=false)
int k[5][pyjets_maxn]
PhysicsTools::Calibration::HistogramF histogram
bool xmlFile(const std::string fParam)
TrackProbabilityCalibration * m_calibration[2]
std::string fullPath() const
Definition: FileInPath.cc:171
SumHistoCalibration(const edm::ParameterSet &)
tuple size
Write out results.