00001 #include "CalibTracker/SiStripLorentzAngle/plugins/MeasureLA.h"
00002 #include "CalibTracker/SiStripLorentzAngle/interface/LA_Filler_Fitter.h"
00003 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
00004 #include <boost/lexical_cast.hpp>
00005 #include <TChain.h>
00006 #include <TFile.h>
00007
00008 namespace sistrip {
00009
00010 void MeasureLA::
00011 store_methods_and_granularity( const edm::VParameterSet& vpset) {
00012 BOOST_FOREACH(edm::ParameterSet p, vpset) {
00013 methods |= p.getParameter<int32_t>("Method");
00014 byModule = byModule || p.getParameter<int32_t>("Granularity");
00015 byLayer = byLayer || !p.getParameter<int32_t>("Granularity");
00016 }
00017 }
00018
00019 MeasureLA::MeasureLA(const edm::ParameterSet& conf) :
00020 inputFiles( conf.getParameter<std::vector<std::string> >("InputFiles") ),
00021 inFileLocation( conf.getParameter<std::string>("InFileLocation")),
00022 fp_(conf.getParameter<edm::FileInPath>("SiStripDetInfo") ),
00023 reports( conf.getParameter<edm::VParameterSet>("Reports")),
00024 measurementPreferences( conf.getParameter<edm::VParameterSet>("MeasurementPreferences")),
00025 calibrations(conf.getParameter<edm::VParameterSet>("Calibrations")),
00026 methods(0), byModule(false), byLayer(false),
00027 localybin(conf.getUntrackedParameter<double>("LocalYBin",0.0)),
00028 stripsperbin(conf.getUntrackedParameter<unsigned>("StripsPerBin",0)),
00029 maxEvents( conf.getUntrackedParameter<unsigned>("MaxEvents",0))
00030 {
00031 store_methods_and_granularity( reports );
00032 store_methods_and_granularity( measurementPreferences );
00033 store_calibrations();
00034
00035 TChain*const chain = new TChain("la_data");
00036 BOOST_FOREACH(const std::string file, inputFiles) chain->Add((file+inFileLocation).c_str());
00037
00038 LA_Filler_Fitter laff(methods, byLayer, byModule, localybin, stripsperbin, maxEvents);
00039 laff.fill(chain, book);
00040 laff.fit(book);
00041 summarize_module_muH_byLayer();
00042 process_reports();
00043
00044 setWhatProduced(this,&MeasureLA::produce);
00045 }
00046
00047
00048 boost::shared_ptr<SiStripLorentzAngle> MeasureLA::
00049 produce(const SiStripLorentzAngleRcd& ) {
00050 boost::shared_ptr<SiStripLorentzAngle> lorentzAngle(new SiStripLorentzAngle());
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 return lorentzAngle;
00061 }
00062
00063 void MeasureLA::
00064 summarize_module_muH_byLayer() {
00065 for(int m = LA_Filler_Fitter::FIRST_METHOD; m <= LA_Filler_Fitter::LAST_METHOD; m<<=1) {
00066 const LA_Filler_Fitter::Method method = (LA_Filler_Fitter::Method)m;
00067 std::pair<uint32_t,LA_Filler_Fitter::Result> result;
00068 BOOST_FOREACH(result, LA_Filler_Fitter::module_results(book, method)) {
00069
00070 calibrate( calibration_key(result.first,method), result.second);
00071 std::string label = LA_Filler_Fitter::layerLabel(result.first) + granularity(MODULESUMMARY) + LA_Filler_Fitter::method(method);
00072 label = boost::regex_replace(label,boost::regex("layer"),"");
00073
00074 const double mu_H = -result.second.calMeasured.first / result.second.field;
00075 const double sigma_mu_H = result.second.calMeasured.second / result.second.field;
00076 const double weight = pow(1./sigma_mu_H, 2);
00077
00078 book.fill(mu_H, label, 150,-0.05,0.1, weight);
00079 }
00080 for(Book::iterator it = book.begin(".*"+granularity(MODULESUMMARY)+".*"); it!=book.end(); ++it) {
00081 if(it->second->GetEntries()) it->second->Fit("gaus","LLQ");
00082 }
00083 }
00084 }
00085
00086 void MeasureLA::
00087 process_reports() const {
00088 BOOST_FOREACH(edm::ParameterSet p, reports) {
00089 const GRANULARITY gran = (GRANULARITY) p.getParameter<int32_t>("Granularity");
00090 const std::string name = p.getParameter<std::string>("ReportName");
00091 const LA_Filler_Fitter::Method method = (LA_Filler_Fitter::Method) p.getParameter<int32_t>("Method");
00092
00093 write_report_plots( name, method, gran);
00094 switch(gran) {
00095 case LAYER: write_report_text( name, method, LA_Filler_Fitter::layer_results(book, method) ); break;
00096 case MODULE: write_report_text( name, method, LA_Filler_Fitter::module_results(book, method)); break;
00097 case MODULESUMMARY: write_report_text_ms( name, method); break;
00098 }
00099 }
00100
00101 { TFile widthsFile("widths.root","RECREATE");
00102 for(Book::const_iterator it = book.begin(".*_width"); it!=book.end(); it++) if(it->second) it->second->Write();
00103 widthsFile.Close();}
00104 }
00105
00106 void MeasureLA::
00107 write_report_plots(std::string name, LA_Filler_Fitter::Method method, GRANULARITY gran ) const {
00108 TFile file((name+".root").c_str(),"RECREATE");
00109 const std::string key = ".*" + granularity(gran) + ".*("+LA_Filler_Fitter::method(method)+"|"+LA_Filler_Fitter::method(method,0)+".*)";
00110 for(Book::const_iterator hist = book.begin(key); hist!=book.end(); ++hist)
00111 if(hist->second) hist->second->Write();
00112 file.Close();
00113 }
00114
00115 template <class T>
00116 void MeasureLA::
00117 write_report_text(std::string name, LA_Filler_Fitter::Method method, std::map<T,LA_Filler_Fitter::Result> results) const {
00118 fstream file((name+".dat").c_str(),std::ios::out);
00119 std::pair<T,LA_Filler_Fitter::Result> result;
00120 BOOST_FOREACH(result, results) {
00121 calibrate( calibration_key(result.first,method), result.second);
00122 file << result.first << "\t" << result.second << std::endl;
00123 }
00124 file.close();
00125 }
00126
00127 void MeasureLA::
00128 write_report_text_ms(std::string name, LA_Filler_Fitter::Method method) const {
00129 fstream file((name+".dat").c_str(),std::ios::out);
00130 const std::string key = ".*"+granularity(MODULESUMMARY)+LA_Filler_Fitter::method(method);
00131 for(Book::const_iterator it = book.begin(key); it!=book.end(); ++it) {
00132 const TF1*const f = it->second->GetFunction("gaus");
00133 if(f) {
00134 file << it->first << "\t"
00135 << f->GetParameter(1) << "\t"
00136 << f->GetParError(1) << "\t"
00137 << f->GetParameter(2) << "\t"
00138 << f->GetParError(2) << std::endl;
00139 }
00140 }
00141 file.close();
00142 }
00143
00144 void MeasureLA::
00145 store_calibrations() {
00146 BOOST_FOREACH(edm::ParameterSet p, calibrations) {
00147 LA_Filler_Fitter::Method method = (LA_Filler_Fitter::Method) p.getParameter<int32_t>("Method");
00148 std::vector<double> slopes(p.getParameter<std::vector<double> >("Slopes")); assert(slopes.size()==14);
00149 std::vector<double> offsets(p.getParameter<std::vector<double> >("Offsets")); assert(offsets.size()==14);
00150 std::vector<double> pulls(p.getParameter<std::vector<double> >("Pulls")); assert(pulls.size()==14);
00151
00152 for(unsigned i=0; i<14; i++) {
00153 const std::pair<unsigned,LA_Filler_Fitter::Method> key( i, method);
00154 offset[key] = offsets[i];
00155 slope[key] = slopes[i];
00156 error_scaling[key] = pulls[i];
00157 }
00158 }
00159 }
00160
00161 inline
00162 void MeasureLA::
00163 calibrate(const std::pair<unsigned,LA_Filler_Fitter::Method> key, LA_Filler_Fitter::Result& result) const {
00164 result.calMeasured = std::make_pair<float,float>( ( result.measured.first - offset.find(key)->second ) / slope.find(key)->second ,
00165 result.measured.second * error_scaling.find(key)->second / slope.find(key)->second );
00166 }
00167
00168 std::pair<uint32_t,LA_Filler_Fitter::Method> MeasureLA::
00169 calibration_key(const std::string layer, const LA_Filler_Fitter::Method method) {
00170 boost::regex format(".*(T[IO]B)_layer(\\d)([as]).*");
00171 const bool TIB = "TIB" == boost::regex_replace(layer, format, "\\1");
00172 const bool stereo = "s" == boost::regex_replace(layer, format, "\\3");
00173 const unsigned layerNum = boost::lexical_cast<unsigned>(boost::regex_replace(layer, format, "\\2"));
00174 return std::make_pair(LA_Filler_Fitter::layer_index(TIB,stereo,layerNum),method);
00175 }
00176
00177 std::pair<uint32_t,LA_Filler_Fitter::Method> MeasureLA::
00178 calibration_key(const uint32_t detid, const LA_Filler_Fitter::Method method) {
00179 const bool TIB = SiStripDetId(detid).subDetector() == SiStripDetId::TIB;
00180 const bool stereo = TIB ? TIBDetId(detid).stereo() : TOBDetId(detid).stereo();
00181 const unsigned layer = TIB ? TIBDetId(detid).layer() : TOBDetId(detid).layer();
00182 return std::make_pair(LA_Filler_Fitter::layer_index(TIB,stereo,layer),method);
00183 }
00184
00185 }