Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-23 22:52:46

0001 #include "CalibTracker/SiStripLorentzAngle/plugins/EnsembleCalibrationLA.h"
0002 #include "CalibTracker/SiStripCommon/interface/Book.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include <TChain.h>
0005 #include <TFile.h>
0006 #include <fstream>
0007 #include <regex>
0008 
0009 namespace sistrip {
0010 
0011   EnsembleCalibrationLA::EnsembleCalibrationLA(const edm::ParameterSet& conf)
0012       : inputFiles(conf.getParameter<std::vector<std::string> >("InputFiles")),
0013         inFileLocation(conf.getParameter<std::string>("InFileLocation")),
0014         Prefix(conf.getUntrackedParameter<std::string>("Prefix", "")),
0015         maxEvents(conf.getUntrackedParameter<unsigned>("MaxEvents", 0)),
0016         samples(conf.getParameter<unsigned>("Samples")),
0017         nbins(conf.getParameter<unsigned>("NBins")),
0018         lowBin(conf.getParameter<double>("LowBin")),
0019         highBin(conf.getParameter<double>("HighBin")),
0020         vMethods(conf.getParameter<std::vector<int> >("Methods")),
0021         tTopoToken_(esConsumes<edm::Transition::EndRun>()) {}
0022 
0023   EnsembleCalibrationLA::~EnsembleCalibrationLA(){};
0024 
0025   void EnsembleCalibrationLA::endJob() {
0026     Book book("la_ensemble");
0027     TChain* const chain = new TChain("la_ensemble");
0028     for (auto const& file : inputFiles)
0029       chain->Add((file + inFileLocation).c_str());
0030 
0031     int methods = 0;
0032     for (unsigned int method : vMethods)
0033       methods |= method;
0034 
0035     LA_Filler_Fitter laff(methods, samples, nbins, lowBin, highBin, maxEvents, tTopo_);
0036     laff.fill(chain, book);
0037     laff.fit(book);
0038     laff.summarize_ensembles(book);
0039 
0040     write_ensembles_text(book);
0041     write_ensembles_plots(book);
0042     write_samples_plots(book);
0043     write_calibrations();
0044   }
0045 
0046   void EnsembleCalibrationLA::beginRun(const edm::Run&, const edm::EventSetup& eSetup) {}
0047 
0048   void EnsembleCalibrationLA::endRun(const edm::Run&, const edm::EventSetup& eSetup) {
0049     tTopo_ = &eSetup.getData(tTopoToken_);
0050   }
0051 
0052   void EnsembleCalibrationLA::write_ensembles_text(const Book& book) {
0053     for (auto const& ensemble : LA_Filler_Fitter::ensemble_summary(book)) {
0054       std::fstream file((Prefix + ensemble.first + ".dat").c_str(), std::ios::out);
0055       for (auto const& summary : ensemble.second)
0056         file << summary << std::endl;
0057 
0058       const std::pair<std::pair<float, float>, std::pair<float, float> > line =
0059           LA_Filler_Fitter::offset_slope(ensemble.second);
0060       const float pull = LA_Filler_Fitter::pull(ensemble.second);
0061 
0062       unsigned index = 15;
0063       std::string label;
0064       {
0065         std::cout << ensemble.first << std::endl;
0066         std::regex format(".*(T[IO]B)_layer(\\d)([as])_(.*)");
0067         if (std::regex_match(ensemble.first, format)) {
0068           const bool TIB = "TIB" == std::regex_replace(ensemble.first, format, "\\1");
0069           const bool stereo = "s" == std::regex_replace(ensemble.first, format, "\\3");
0070           const unsigned layer = std::stoul(std::regex_replace(ensemble.first, format, "\\2"));
0071           label = std::regex_replace(ensemble.first, format, "\\4");
0072           index = LA_Filler_Fitter::layer_index(TIB, stereo, layer);
0073 
0074           calibrations[label].slopes[index] = line.second.first;
0075           calibrations[label].offsets[index] = line.first.first;
0076           calibrations[label].pulls[index] = pull;
0077         }
0078       }
0079 
0080       file << std::endl
0081            << std::endl
0082            << "# Best Fit Line: " << line.first.first << "(" << line.first.second << ")   +   x* " << line.second.first
0083            << "(" << line.second.second << ")" << std::endl
0084            << "# Pull (average sigma of (x_measure-x_truth)/e_measure): " << pull << std::endl
0085            << "LA_Calibration( METHOD_XXXXX , xxx, " << line.second.first << ", " << line.first.first << ", " << pull
0086            << ")," << std::endl;
0087       file.close();
0088     }
0089   }
0090 
0091   void EnsembleCalibrationLA::write_ensembles_plots(const Book& book) const {
0092     TFile file((Prefix + "sampleFits.root").c_str(), "RECREATE");
0093     for (Book::const_iterator hist = book.begin(".*(profile|ratio|reconstruction|symm|symmchi2|_w\\d)");
0094          hist != book.end();
0095          ++hist)
0096       hist->second->Write();
0097     file.Close();
0098   }
0099 
0100   void EnsembleCalibrationLA::write_samples_plots(const Book& book) const {
0101     TFile file((Prefix + "ensembleFits.root").c_str(), "RECREATE");
0102     for (Book::const_iterator hist = book.begin(".*(measure|merr|ensembleReco|pull)"); hist != book.end(); ++hist)
0103       hist->second->Write();
0104     file.Close();
0105   }
0106 
0107   void EnsembleCalibrationLA::write_calibrations() const {
0108     std::fstream file((Prefix + "calibrations.dat").c_str(), std::ios::out);
0109     for (auto const& cal : calibrations) {
0110       file << cal.first << std::endl << "\t slopes(";
0111       for (float i : cal.second.slopes)
0112         file << i << ",";
0113       file << ")" << std::endl << "\t offsets(";
0114       for (float i : cal.second.offsets)
0115         file << i << ",";
0116       file << ")" << std::endl << "\t pulls(";
0117       for (float i : cal.second.pulls)
0118         file << i << ",";
0119       file << ")" << std::endl;
0120     }
0121     file.close();
0122   }
0123 
0124 }  // namespace sistrip