CMS 3D CMS Logo

EnsembleCalibrationLA.cc
Go to the documentation of this file.
6 #include <TChain.h>
7 #include <TFile.h>
8 #include <boost/foreach.hpp>
9 #include <boost/lexical_cast.hpp>
10 #include <fstream>
11 
12 namespace sistrip {
13 
15  inputFiles( conf.getParameter<std::vector<std::string> >("InputFiles") ),
16  inFileLocation( conf.getParameter<std::string>("InFileLocation")),
17  Prefix( conf.getUntrackedParameter<std::string>("Prefix","")),
18  maxEvents( conf.getUntrackedParameter<unsigned>("MaxEvents",0)),
19  samples( conf.getParameter<unsigned>("Samples")),
20  nbins( conf.getParameter<unsigned>("NBins")),
21  lowBin( conf.getParameter<double>("LowBin")),
22  highBin( conf.getParameter<double>("HighBin")),
23  vMethods( conf.getParameter<std::vector<int> >("Methods"))
24 {}
25 
27 {
28  Book book("la_ensemble");
29  TChain*const chain = new TChain("la_ensemble");
30  BOOST_FOREACH(std::string file, inputFiles) chain->Add((file+inFileLocation).c_str());
31 
32  int methods = 0; BOOST_FOREACH(unsigned method, vMethods) methods|=method;
33 
35  laff.fill(chain,book);
36  laff.fit(book);
37  laff.summarize_ensembles(book);
38 
41  write_samples_plots(book);
43 }
44 
46 {
48  eSetup.get<TrackerTopologyRcd>().get(tTopoHandle);
49  tTopo_ = tTopoHandle.product();
50 }
51 
53 {
54  std::pair<std::string, std::vector<LA_Filler_Fitter::EnsembleSummary> > ensemble;
55  BOOST_FOREACH(ensemble, LA_Filler_Fitter::ensemble_summary(book)) {
56  std::fstream file((Prefix+ensemble.first+".dat").c_str(),std::ios::out);
57  BOOST_FOREACH(LA_Filler_Fitter::EnsembleSummary summary, ensemble.second)
58  file << summary << std::endl;
59 
60  const std::pair<std::pair<float,float>,std::pair<float,float> > line = LA_Filler_Fitter::offset_slope(ensemble.second);
61  const float pull = LA_Filler_Fitter::pull(ensemble.second);
62 
63  unsigned index = 15;
65  {
66  std::cout << ensemble.first << std::endl;
67  boost::regex format(".*(T[IO]B)_layer(\\d)([as])_(.*)");
68  if(boost::regex_match(ensemble.first,format)) {
69  const bool TIB = "TIB" == boost::regex_replace(ensemble.first, format, "\\1");
70  const bool stereo = "s" == boost::regex_replace(ensemble.first, format, "\\3");
71  const unsigned layer = boost::lexical_cast<unsigned>(boost::regex_replace(ensemble.first, format, "\\2"));
72  label = boost::regex_replace(ensemble.first, format, "\\4");
73  index = LA_Filler_Fitter::layer_index(TIB,stereo,layer);
74 
75  calibrations[label].slopes[index]=line.second.first;
76  calibrations[label].offsets[index]=line.first.first;
77  calibrations[label].pulls[index]=pull;
78  }
79  }
80 
81  file << std::endl << std::endl
82  << "# Best Fit Line: "
83  << line.first.first <<"("<< line.first.second<<") + x* "
84  << line.second.first<<"("<< line.second.second<<")"
85  << std::endl
86  << "# Pull (average sigma of (x_measure-x_truth)/e_measure): " << pull
87  << std::endl
88  << "LA_Calibration( METHOD_XXXXX , xxx, " << line.second.first << ", " << line.first.first << ", " << pull << ")," << std::endl;
89  file.close();
90  }
91 }
92 
94 write_ensembles_plots(const Book& book) const {
95  TFile file((Prefix+"sampleFits.root").c_str(),"RECREATE");
96  for(Book::const_iterator hist = book.begin(".*(profile|ratio|reconstruction|symm|symmchi2|_w\\d)"); hist!=book.end(); ++hist)
97  hist->second->Write();
98  file.Close();
99 }
100 
102 write_samples_plots(const Book& book) const {
103  TFile file((Prefix+"ensembleFits.root").c_str(),"RECREATE");
104  for(Book::const_iterator hist = book.begin(".*(measure|merr|ensembleReco|pull)"); hist!=book.end(); ++hist)
105  hist->second->Write();
106  file.Close();
107 }
108 
111  std::fstream file((Prefix+"calibrations.dat").c_str(),std::ios::out);
112  std::pair<std::string,MethodCalibrations> cal;
113  BOOST_FOREACH(cal,calibrations) {
114  file << cal.first << std::endl
115  << "\t slopes("; BOOST_FOREACH(float i, cal.second.slopes) file << i<< ","; file << ")" << std::endl
116  << "\t offsets("; BOOST_FOREACH(float i, cal.second.offsets) file << i<< ","; file << ")" << std::endl
117  << "\t pulls("; BOOST_FOREACH(float i, cal.second.pulls) file << i<< ","; file << ")" << std::endl;
118  }
119  file.close();
120 }
121 
122 }
123 
std::map< std::string, MethodCalibrations > calibrations
static unsigned layer_index(bool TIB, bool stereo, unsigned layer)
static void fit(Book &book)
Definition: chain.py:1
void write_ensembles_plots(const Book &) const
static std::pair< std::pair< float, float >, std::pair< float, float > > offset_slope(const std::vector< EnsembleSummary > &)
Definition: LA_Results.cc:132
static std::map< std::string, std::vector< EnsembleSummary > > ensemble_summary(const Book &)
Definition: LA_Results.cc:107
void fill(TTree *, Book &) const
Definition: LA_Filler.cc:9
sistrip classes
void summarize_ensembles(Book &) const
Definition: LA_Results.cc:83
char const * label
const std::vector< std::string > inputFiles
iterator begin(string_t re=".*")
Definition: Book.h:48
format
Some error handling for the usage.
void write_samples_plots(const Book &) const
EnsembleCalibrationLA(const edm::ParameterSet &)
inputFiles
Definition: merge.py:6
boost::filter_iterator< match_name, book_t::const_iterator > const_iterator
Definition: Book.h:47
static float pull(const std::vector< EnsembleSummary > &)
Definition: LA_Results.cc:156
T get() const
Definition: EventSetup.h:68
Definition: Book.h:16
T const * product() const
Definition: ESHandle.h:84
void endRun(const edm::Run &, const edm::EventSetup &) override
Definition: Run.h:44
iterator end(string_t re=".*")
Definition: Book.h:50