Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-02 22:41:50

0001 /****************************************************************************

0002 * Authors: 

0003 *  Jan Kašpar (jan.kaspar@gmail.com) 

0004 *  Mateusz Kocot (mateuszkocot99@gmail.com)

0005 ****************************************************************************/
0006 
0007 #include "FWCore/Framework/interface/MakerMacros.h"
0008 #include "FWCore/Framework/interface/SourceFactory.h"
0009 #include "FWCore/Framework/interface/ModuleFactory.h"
0010 
0011 #include "FWCore/Framework/interface/ESProducer.h"
0012 #include "FWCore/Framework/interface/ESProducts.h"
0013 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 #include "FWCore/Utilities/interface/ESInputTag.h"
0017 
0018 #include "CondFormats/PPSObjects/interface/PPSAlignmentConfiguration.h"
0019 #include "CondFormats/DataRecord/interface/PPSAlignmentConfigurationRcd.h"
0020 
0021 #include "CalibPPS/AlignmentGlobal/interface/utils.h"
0022 
0023 #include <string>
0024 #include <vector>
0025 #include <map>
0026 #include <memory>
0027 #include <queue>
0028 #include <algorithm>
0029 
0030 #include "TF1.h"
0031 #include "TProfile.h"
0032 #include "TFile.h"
0033 #include "TKey.h"
0034 #include "TSystemFile.h"
0035 
0036 //---------------------------------------------------------------------------------------------

0037 
0038 class PPSAlignmentConfigurationESSource : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0039 public:
0040   PPSAlignmentConfigurationESSource(const edm::ParameterSet& iConfig);
0041 
0042   std::unique_ptr<PPSAlignmentConfiguration> produce(const PPSAlignmentConfigurationRcd&);
0043   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0044 
0045 private:
0046   TDirectory* findDirectoryWithName(TDirectory* dir, std::string searchName);
0047   std::vector<PPSAlignmentConfiguration::PointErrors> buildVectorFromDirectory(
0048       TDirectory* dir, const PPSAlignmentConfiguration::RPConfig& rpd);
0049 
0050   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey& key,
0051                       const edm::IOVSyncValue& iosv,
0052                       edm::ValidityInterval& oValidity) override;
0053 
0054   bool debug;
0055 
0056   PPSAlignmentConfiguration::SectorConfig sectorConfig45, sectorConfig56;
0057 
0058   double x_ali_sh_step;
0059 
0060   double y_mode_sys_unc;
0061   double chiSqThreshold;
0062   double y_mode_unc_max_valid;
0063   double y_mode_max_valid;
0064 
0065   unsigned int minRPTracksSize;
0066   unsigned int maxRPTracksSize;
0067   double n_si;
0068 
0069   std::map<unsigned int, std::vector<PPSAlignmentConfiguration::PointErrors>> matchingReferencePoints;
0070   std::map<unsigned int, PPSAlignmentConfiguration::SelectionRange> matchingShiftRanges;
0071 
0072   std::map<unsigned int, PPSAlignmentConfiguration::SelectionRange> alignment_x_meth_o_ranges;
0073   unsigned int fitProfileMinBinEntries;
0074   unsigned int fitProfileMinNReasonable;
0075   unsigned int methOGraphMinN;
0076   double methOUncFitRange;
0077 
0078   std::map<unsigned int, PPSAlignmentConfiguration::SelectionRange> alignment_x_relative_ranges;
0079   unsigned int nearFarMinEntries;
0080 
0081   std::map<unsigned int, PPSAlignmentConfiguration::SelectionRange> alignment_y_ranges;
0082   unsigned int modeGraphMinN;
0083   unsigned int multSelProjYMinEntries;
0084 
0085   PPSAlignmentConfiguration::Binning binning;
0086 
0087   std::vector<double> extraParams;
0088 
0089   std::string label;
0090 };
0091 
0092 //---------------------------------------------------------------------------------------------

0093 
0094 PPSAlignmentConfigurationESSource::PPSAlignmentConfigurationESSource(const edm::ParameterSet& iConfig) {
0095   label = iConfig.getParameter<std::string>("label");
0096 
0097   debug = iConfig.getParameter<bool>("debug");
0098   std::unique_ptr<TFile> debugFile;
0099   if (debug) {
0100     debugFile =
0101         std::make_unique<TFile>(("debug_producer_" + (label.empty() ? "test" : label) + ".root").c_str(), "recreate");
0102   }
0103 
0104   sectorConfig45.name_ = "sector 45";
0105 
0106   sectorConfig45.rp_N_.position_ = "N";
0107   sectorConfig45.rp_F_.position_ = "F";
0108 
0109   sectorConfig56.name_ = "sector 56";
0110 
0111   sectorConfig56.rp_N_.position_ = "N";
0112   sectorConfig56.rp_F_.position_ = "F";
0113 
0114   for (std::string sectorName : {"sector_45", "sector_56"}) {
0115     const auto& sps = iConfig.getParameter<edm::ParameterSet>(sectorName);
0116     PPSAlignmentConfiguration::SectorConfig* sc;
0117     if (sectorName == "sector_45")
0118       sc = &sectorConfig45;
0119     else
0120       sc = &sectorConfig56;
0121 
0122     for (std::string rpName : {"rp_N", "rp_F"}) {
0123       const auto& rpps = sps.getParameter<edm::ParameterSet>(rpName);
0124       PPSAlignmentConfiguration::RPConfig* rc;
0125       if (rpName == "rp_N")
0126         rc = &sc->rp_N_;
0127       else
0128         rc = &sc->rp_F_;
0129 
0130       rc->name_ = rpps.getParameter<std::string>("name");
0131       rc->id_ = rpps.getParameter<int>("id");
0132 
0133       rc->slope_ = rpps.getParameter<double>("slope");
0134       rc->sh_x_ = rpps.getParameter<double>("sh_x");
0135 
0136       rc->x_min_fit_mode_ = rpps.getParameter<double>("x_min_fit_mode");
0137       rc->x_max_fit_mode_ = rpps.getParameter<double>("x_max_fit_mode");
0138       rc->y_max_fit_mode_ = rpps.getParameter<double>("y_max_fit_mode");
0139       rc->y_cen_add_ = rpps.getParameter<double>("y_cen_add");
0140       rc->y_width_mult_ = rpps.getParameter<double>("y_width_mult");
0141 
0142       rc->x_slice_min_ = rpps.getParameter<double>("x_slice_min");
0143       rc->x_slice_w_ = rpps.getParameter<double>("x_slice_w");
0144       rc->x_slice_n_ = std::ceil((rpps.getParameter<double>("x_slice_max") - rc->x_slice_min_) / rc->x_slice_w_);
0145     }
0146 
0147     sc->slope_ = sps.getParameter<double>("slope");
0148 
0149     sc->cut_h_apply_ = sps.getParameter<bool>("cut_h_apply");
0150     sc->cut_h_a_ = sps.getParameter<double>("cut_h_a");
0151     sc->cut_h_c_ = sps.getParameter<double>("cut_h_c");
0152     sc->cut_h_si_ = sps.getParameter<double>("cut_h_si");
0153 
0154     sc->cut_v_apply_ = sps.getParameter<bool>("cut_v_apply");
0155     sc->cut_v_a_ = sps.getParameter<double>("cut_v_a");
0156     sc->cut_v_c_ = sps.getParameter<double>("cut_v_c");
0157     sc->cut_v_si_ = sps.getParameter<double>("cut_v_si");
0158   }
0159 
0160   std::map<unsigned int, std::string> rpTags = {{sectorConfig45.rp_F_.id_, "rp_L_F"},
0161                                                 {sectorConfig45.rp_N_.id_, "rp_L_N"},
0162                                                 {sectorConfig56.rp_N_.id_, "rp_R_N"},
0163                                                 {sectorConfig56.rp_F_.id_, "rp_R_F"}};
0164 
0165   std::map<unsigned int, std::string> sectorNames = {{sectorConfig45.rp_F_.id_, sectorConfig45.name_},
0166                                                      {sectorConfig45.rp_N_.id_, sectorConfig45.name_},
0167                                                      {sectorConfig56.rp_N_.id_, sectorConfig56.name_},
0168                                                      {sectorConfig56.rp_F_.id_, sectorConfig56.name_}};
0169 
0170   std::map<unsigned int, const PPSAlignmentConfiguration::RPConfig*> rpConfigs = {
0171       {sectorConfig45.rp_F_.id_, &sectorConfig45.rp_F_},
0172       {sectorConfig45.rp_N_.id_, &sectorConfig45.rp_N_},
0173       {sectorConfig56.rp_N_.id_, &sectorConfig56.rp_N_},
0174       {sectorConfig56.rp_F_.id_, &sectorConfig56.rp_F_}};
0175 
0176   x_ali_sh_step = iConfig.getParameter<double>("x_ali_sh_step");
0177 
0178   y_mode_sys_unc = iConfig.getParameter<double>("y_mode_sys_unc");
0179   chiSqThreshold = iConfig.getParameter<double>("chiSqThreshold");
0180   y_mode_unc_max_valid = iConfig.getParameter<double>("y_mode_unc_max_valid");
0181   y_mode_max_valid = iConfig.getParameter<double>("y_mode_max_valid");
0182 
0183   minRPTracksSize = iConfig.getParameter<unsigned int>("min_RP_tracks_size");
0184   maxRPTracksSize = iConfig.getParameter<unsigned int>("max_RP_tracks_size");
0185   n_si = iConfig.getParameter<double>("n_si");
0186 
0187   const auto& c_axo = iConfig.getParameter<edm::ParameterSet>("x_alignment_meth_o");
0188   for (const auto& p : rpTags) {
0189     const auto& ps = c_axo.getParameter<edm::ParameterSet>(p.second);
0190     alignment_x_meth_o_ranges[p.first] = {ps.getParameter<double>("x_min"), ps.getParameter<double>("x_max")};
0191   }
0192   fitProfileMinBinEntries = c_axo.getParameter<unsigned int>("fit_profile_min_bin_entries");
0193   fitProfileMinNReasonable = c_axo.getParameter<unsigned int>("fit_profile_min_N_reasonable");
0194   methOGraphMinN = c_axo.getParameter<unsigned int>("meth_o_graph_min_N");
0195   methOUncFitRange = c_axo.getParameter<double>("meth_o_unc_fit_range");
0196 
0197   const auto& c_m = iConfig.getParameter<edm::ParameterSet>("matching");
0198   const auto& referenceDataset = c_m.getParameter<std::string>("reference_dataset");
0199 
0200   // constructing vectors with reference data

0201   if (!referenceDataset.empty()) {
0202     auto f_ref = std::make_unique<TFile>(referenceDataset.c_str(), "READ");
0203     if (!f_ref->IsOpen()) {
0204       edm::LogWarning("PPS") << "[ESSource] could not find reference dataset file: " << referenceDataset;
0205     } else {
0206       TDirectory* ad_ref = findDirectoryWithName((TDirectory*)f_ref.get(), sectorConfig45.name_);
0207       if (ad_ref == nullptr) {
0208         edm::LogWarning("PPS") << "[ESSource] could not find reference dataset in " << referenceDataset;
0209       } else {
0210         edm::LogInfo("PPS") << "[ESSource] loading reference dataset from " << ad_ref->GetPath();
0211 
0212         for (const auto& p : rpTags) {
0213           if (debug)
0214             gDirectory = debugFile->mkdir(rpConfigs[p.first]->name_.c_str())->mkdir("fits_ref");
0215 
0216           auto* d_ref = (TDirectory*)ad_ref->Get(
0217               (sectorNames[p.first] + "/near_far/x slices " + rpConfigs[p.first]->position_).c_str());
0218           if (d_ref == nullptr) {
0219             edm::LogWarning("PPS") << "[ESSource] could not load d_ref";
0220           } else {
0221             matchingReferencePoints[p.first] = buildVectorFromDirectory(d_ref, *rpConfigs[p.first]);
0222           }
0223         }
0224       }
0225     }
0226   }
0227 
0228   for (const auto& p : rpTags) {
0229     const auto& ps = c_m.getParameter<edm::ParameterSet>(p.second);
0230     matchingShiftRanges[p.first] = {ps.getParameter<double>("sh_min"), ps.getParameter<double>("sh_max")};
0231   }
0232 
0233   const auto& c_axr = iConfig.getParameter<edm::ParameterSet>("x_alignment_relative");
0234   for (const auto& p : rpTags) {
0235     if (p.second.back() == 'N') {  // only near RPs

0236       const auto& ps = c_axr.getParameter<edm::ParameterSet>(p.second);
0237       alignment_x_relative_ranges[p.first] = {ps.getParameter<double>("x_min"), ps.getParameter<double>("x_max")};
0238     }
0239   }
0240   nearFarMinEntries = c_axr.getParameter<unsigned int>("near_far_min_entries");
0241 
0242   const auto& c_ay = iConfig.getParameter<edm::ParameterSet>("y_alignment");
0243   for (const auto& p : rpTags) {
0244     const auto& ps = c_ay.getParameter<edm::ParameterSet>(p.second);
0245     alignment_y_ranges[p.first] = {ps.getParameter<double>("x_min"), ps.getParameter<double>("x_max")};
0246   }
0247   modeGraphMinN = c_ay.getParameter<unsigned int>("mode_graph_min_N");
0248   multSelProjYMinEntries = c_ay.getParameter<unsigned int>("mult_sel_proj_y_min_entries");
0249 
0250   const auto& bps = iConfig.getParameter<edm::ParameterSet>("binning");
0251   binning.bin_size_x_ = bps.getParameter<double>("bin_size_x");
0252   binning.n_bins_x_ = bps.getParameter<unsigned int>("n_bins_x");
0253   binning.pixel_x_offset_ = bps.getParameter<double>("pixel_x_offset");
0254   binning.n_bins_y_ = bps.getParameter<unsigned int>("n_bins_y");
0255   binning.y_min_ = bps.getParameter<double>("y_min");
0256   binning.y_max_ = bps.getParameter<double>("y_max");
0257 
0258   binning.diffFN_n_bins_x_ = bps.getParameter<unsigned int>("diffFN_n_bins_x");
0259   binning.diffFN_x_min_ = bps.getParameter<double>("diffFN_x_min");
0260   binning.diffFN_x_max_ = bps.getParameter<double>("diffFN_x_max");
0261 
0262   binning.slice_n_bins_x_ = bps.getParameter<unsigned int>("slice_n_bins_x");
0263   binning.slice_x_min_ = bps.getParameter<double>("slice_x_min");
0264   binning.slice_x_max_ = bps.getParameter<double>("slice_x_max");
0265   binning.slice_n_bins_y_ = bps.getParameter<unsigned int>("slice_n_bins_y");
0266   binning.slice_y_min_ = bps.getParameter<double>("slice_y_min");
0267   binning.slice_y_max_ = bps.getParameter<double>("slice_y_max");
0268 
0269   extraParams = iConfig.getParameter<std::vector<double>>("extra_params");
0270 
0271   setWhatProduced(this, label);
0272   findingRecord<PPSAlignmentConfigurationRcd>();
0273 }
0274 
0275 //---------------------------------------------------------------------------------------------

0276 
0277 std::unique_ptr<PPSAlignmentConfiguration> PPSAlignmentConfigurationESSource::produce(
0278     const PPSAlignmentConfigurationRcd&) {
0279   auto p = std::make_unique<PPSAlignmentConfiguration>();
0280 
0281   p->setSectorConfig45(sectorConfig45);
0282   p->setSectorConfig56(sectorConfig56);
0283 
0284   p->setX_ali_sh_step(x_ali_sh_step);
0285 
0286   p->setY_mode_sys_unc(y_mode_sys_unc);
0287   p->setChiSqThreshold(chiSqThreshold);
0288   p->setY_mode_unc_max_valid(y_mode_unc_max_valid);
0289   p->setY_mode_max_valid(y_mode_max_valid);
0290 
0291   p->setMinRPTracksSize(minRPTracksSize);
0292   p->setMaxRPTracksSize(maxRPTracksSize);
0293   p->setN_si(n_si);
0294 
0295   p->setMatchingReferencePoints(matchingReferencePoints);
0296   p->setMatchingShiftRanges(matchingShiftRanges);
0297 
0298   p->setAlignment_x_meth_o_ranges(alignment_x_meth_o_ranges);
0299   p->setFitProfileMinBinEntries(fitProfileMinBinEntries);
0300   p->setFitProfileMinNReasonable(fitProfileMinNReasonable);
0301   p->setMethOGraphMinN(methOGraphMinN);
0302   p->setMethOUncFitRange(methOUncFitRange);
0303 
0304   p->setAlignment_x_relative_ranges(alignment_x_relative_ranges);
0305   p->setNearFarMinEntries(nearFarMinEntries);
0306 
0307   p->setAlignment_y_ranges(alignment_y_ranges);
0308   p->setModeGraphMinN(modeGraphMinN);
0309   p->setMultSelProjYMinEntries(multSelProjYMinEntries);
0310 
0311   p->setBinning(binning);
0312 
0313   p->setExtraParams(extraParams);
0314 
0315   edm::LogInfo("PPS") << "\n"
0316                       << "[ESSource] " << (label.empty() ? "empty label" : "label = " + label) << ":\n\n"
0317                       << (*p);
0318 
0319   return p;
0320 }
0321 
0322 //---------------------------------------------------------------------------------------------

0323 
0324 // most default values come from 2018 period

0325 void PPSAlignmentConfigurationESSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0326   edm::ParameterSetDescription desc;
0327 
0328   desc.add<bool>("debug", false);
0329 
0330   desc.add<std::string>("label", "");
0331 
0332   // sector_45

0333   {
0334     edm::ParameterSetDescription sector45;
0335 
0336     edm::ParameterSetDescription rp_N;
0337     rp_N.add<std::string>("name", "L_1_F");
0338     rp_N.add<int>("id", 3);
0339 
0340     rp_N.add<double>("slope", 0.19);
0341     rp_N.add<double>("sh_x", -3.6);
0342 
0343     rp_N.add<double>("x_min_fit_mode", 2.);
0344     rp_N.add<double>("x_max_fit_mode", 7.0);
0345     rp_N.add<double>("y_max_fit_mode", 7.0);
0346     rp_N.add<double>("y_cen_add", -0.3);
0347     rp_N.add<double>("y_width_mult", 1.1);
0348 
0349     rp_N.add<double>("x_slice_min", 7.);
0350     rp_N.add<double>("x_slice_max", 19.);
0351     rp_N.add<double>("x_slice_w", 0.2);
0352     sector45.add<edm::ParameterSetDescription>("rp_N", rp_N);
0353 
0354     edm::ParameterSetDescription rp_F;
0355     rp_F.add<std::string>("name", "L_2_F");
0356     rp_F.add<int>("id", 23);
0357 
0358     rp_F.add<double>("slope", 0.19);
0359     rp_F.add<double>("sh_x", -42.);
0360 
0361     rp_F.add<double>("x_min_fit_mode", 2.);
0362     rp_F.add<double>("x_max_fit_mode", 7.5);
0363     rp_F.add<double>("y_max_fit_mode", 7.5);
0364     rp_F.add<double>("y_cen_add", -0.3);
0365     rp_F.add<double>("y_width_mult", 1.1);
0366 
0367     rp_F.add<double>("x_slice_min", 46.);
0368     rp_F.add<double>("x_slice_max", 58.);
0369     rp_F.add<double>("x_slice_w", 0.2);
0370     sector45.add<edm::ParameterSetDescription>("rp_F", rp_F);
0371 
0372     sector45.add<double>("slope", 0.006);
0373     sector45.add<bool>("cut_h_apply", true);
0374     sector45.add<double>("cut_h_a", -1.);
0375     sector45.add<double>("cut_h_c", -38.55);
0376     sector45.add<double>("cut_h_si", 0.2);
0377     sector45.add<bool>("cut_v_apply", true);
0378     sector45.add<double>("cut_v_a", -1.07);
0379     sector45.add<double>("cut_v_c", 1.63);
0380     sector45.add<double>("cut_v_si", 0.15);
0381 
0382     desc.add<edm::ParameterSetDescription>("sector_45", sector45);
0383   }
0384 
0385   // sector_56

0386   {
0387     edm::ParameterSetDescription sector56;
0388 
0389     edm::ParameterSetDescription rp_N;
0390     rp_N.add<std::string>("name", "R_1_F");
0391     rp_N.add<int>("id", 103);
0392 
0393     rp_N.add<double>("slope", 0.40);
0394     rp_N.add<double>("sh_x", -2.8);
0395 
0396     rp_N.add<double>("x_min_fit_mode", 2.);
0397     rp_N.add<double>("x_max_fit_mode", 7.4);
0398     rp_N.add<double>("y_max_fit_mode", 7.4);
0399     rp_N.add<double>("y_cen_add", -0.8);
0400     rp_N.add<double>("y_width_mult", 1.0);
0401 
0402     rp_N.add<double>("x_slice_min", 6.);
0403     rp_N.add<double>("x_slice_max", 17.);
0404     rp_N.add<double>("x_slice_w", 0.2);
0405     sector56.add<edm::ParameterSetDescription>("rp_N", rp_N);
0406 
0407     edm::ParameterSetDescription rp_F;
0408     rp_F.add<std::string>("name", "R_2_F");
0409     rp_F.add<int>("id", 123);
0410 
0411     rp_F.add<double>("slope", 0.39);
0412     rp_F.add<double>("sh_x", -41.9);
0413 
0414     rp_F.add<double>("x_min_fit_mode", 2.);
0415     rp_F.add<double>("x_max_fit_mode", 8.0);
0416     rp_F.add<double>("y_max_fit_mode", 8.0);
0417     rp_F.add<double>("y_cen_add", -0.8);
0418     rp_F.add<double>("y_width_mult", 1.0);
0419 
0420     rp_F.add<double>("x_slice_min", 45.);
0421     rp_F.add<double>("x_slice_max", 57.);
0422     rp_F.add<double>("x_slice_w", 0.2);
0423     sector56.add<edm::ParameterSetDescription>("rp_F", rp_F);
0424 
0425     sector56.add<double>("slope", -0.015);
0426     sector56.add<bool>("cut_h_apply", true);
0427     sector56.add<double>("cut_h_a", -1.);
0428     sector56.add<double>("cut_h_c", -39.26);
0429     sector56.add<double>("cut_h_si", 0.2);
0430     sector56.add<bool>("cut_v_apply", true);
0431     sector56.add<double>("cut_v_a", -1.07);
0432     sector56.add<double>("cut_v_c", 1.49);
0433     sector56.add<double>("cut_v_si", 0.15);
0434 
0435     desc.add<edm::ParameterSetDescription>("sector_56", sector56);
0436   }
0437 
0438   desc.add<double>("x_ali_sh_step", 0.01);
0439 
0440   desc.add<double>("y_mode_sys_unc", 0.03);
0441   desc.add<double>("chiSqThreshold", 50.);
0442   desc.add<double>("y_mode_unc_max_valid", 5.);
0443   desc.add<double>("y_mode_max_valid", 20.);
0444 
0445   desc.add<unsigned int>("min_RP_tracks_size", 1);
0446   desc.add<unsigned int>("max_RP_tracks_size", 1);
0447   desc.add<double>("n_si", 4.);
0448 
0449   // matching

0450   {
0451     edm::ParameterSetDescription matching;
0452     matching.add<std::string>("reference_dataset", "");
0453 
0454     edm::ParameterSetDescription rpLF;
0455     rpLF.add<double>("sh_min", -43.);
0456     rpLF.add<double>("sh_max", -41.);
0457     matching.add<edm::ParameterSetDescription>("rp_L_F", rpLF);
0458 
0459     edm::ParameterSetDescription rpLN;
0460     rpLN.add<double>("sh_min", -4.2);
0461     rpLN.add<double>("sh_max", -2.4);
0462     matching.add<edm::ParameterSetDescription>("rp_L_N", rpLN);
0463 
0464     edm::ParameterSetDescription rpRN;
0465     rpRN.add<double>("sh_min", -3.6);
0466     rpRN.add<double>("sh_max", -1.8);
0467     matching.add<edm::ParameterSetDescription>("rp_R_N", rpRN);
0468 
0469     edm::ParameterSetDescription rpRF;
0470     rpRF.add<double>("sh_min", -43.2);
0471     rpRF.add<double>("sh_max", -41.2);
0472     matching.add<edm::ParameterSetDescription>("rp_R_F", rpRF);
0473 
0474     desc.add<edm::ParameterSetDescription>("matching", matching);
0475   }
0476 
0477   // x alignment meth o

0478   {
0479     edm::ParameterSetDescription x_alignment_meth_o;
0480 
0481     edm::ParameterSetDescription rpLF;
0482     rpLF.add<double>("x_min", 47.);
0483     rpLF.add<double>("x_max", 56.5);
0484     x_alignment_meth_o.add<edm::ParameterSetDescription>("rp_L_F", rpLF);
0485 
0486     edm::ParameterSetDescription rpLN;
0487     rpLN.add<double>("x_min", 9.);
0488     rpLN.add<double>("x_max", 18.5);
0489     x_alignment_meth_o.add<edm::ParameterSetDescription>("rp_L_N", rpLN);
0490 
0491     edm::ParameterSetDescription rpRN;
0492     rpRN.add<double>("x_min", 7.);
0493     rpRN.add<double>("x_max", 15.);
0494     x_alignment_meth_o.add<edm::ParameterSetDescription>("rp_R_N", rpRN);
0495 
0496     edm::ParameterSetDescription rpRF;
0497     rpRF.add<double>("x_min", 46.);
0498     rpRF.add<double>("x_max", 54.);
0499     x_alignment_meth_o.add<edm::ParameterSetDescription>("rp_R_F", rpRF);
0500 
0501     x_alignment_meth_o.add<unsigned int>("fit_profile_min_bin_entries", 5);
0502     x_alignment_meth_o.add<unsigned int>("fit_profile_min_N_reasonable", 10);
0503     x_alignment_meth_o.add<unsigned int>("meth_o_graph_min_N", 5);
0504     x_alignment_meth_o.add<double>("meth_o_unc_fit_range", 0.5);
0505 
0506     desc.add<edm::ParameterSetDescription>("x_alignment_meth_o", x_alignment_meth_o);
0507   }
0508 
0509   // x alignment relative

0510   {
0511     edm::ParameterSetDescription x_alignment_relative;
0512 
0513     edm::ParameterSetDescription rpLN;
0514     rpLN.add<double>("x_min", 7.5);
0515     rpLN.add<double>("x_max", 12.);
0516     x_alignment_relative.add<edm::ParameterSetDescription>("rp_L_N", rpLN);
0517 
0518     edm::ParameterSetDescription rpRN;
0519     rpRN.add<double>("x_min", 6.);
0520     rpRN.add<double>("x_max", 10.);
0521     x_alignment_relative.add<edm::ParameterSetDescription>("rp_R_N", rpRN);
0522 
0523     x_alignment_relative.add<unsigned int>("near_far_min_entries", 100);
0524 
0525     desc.add<edm::ParameterSetDescription>("x_alignment_relative", x_alignment_relative);
0526   }
0527 
0528   // y alignment

0529   {
0530     edm::ParameterSetDescription y_alignment;
0531 
0532     edm::ParameterSetDescription rpLF;
0533     rpLF.add<double>("x_min", 44.5);
0534     rpLF.add<double>("x_max", 49.);
0535     y_alignment.add<edm::ParameterSetDescription>("rp_L_F", rpLF);
0536 
0537     edm::ParameterSetDescription rpLN;
0538     rpLN.add<double>("x_min", 6.7);
0539     rpLN.add<double>("x_max", 11.);
0540     y_alignment.add<edm::ParameterSetDescription>("rp_L_N", rpLN);
0541 
0542     edm::ParameterSetDescription rpRN;
0543     rpRN.add<double>("x_min", 5.9);
0544     rpRN.add<double>("x_max", 10.);
0545     y_alignment.add<edm::ParameterSetDescription>("rp_R_N", rpRN);
0546 
0547     edm::ParameterSetDescription rpRF;
0548     rpRF.add<double>("x_min", 44.5);
0549     rpRF.add<double>("x_max", 49.);
0550     y_alignment.add<edm::ParameterSetDescription>("rp_R_F", rpRF);
0551 
0552     y_alignment.add<unsigned int>("mode_graph_min_N", 5);
0553     y_alignment.add<unsigned int>("mult_sel_proj_y_min_entries", 300);
0554 
0555     desc.add<edm::ParameterSetDescription>("y_alignment", y_alignment);
0556   }
0557 
0558   // binning

0559   {
0560     edm::ParameterSetDescription binning;
0561 
0562     binning.add<double>("bin_size_x", 142.3314E-3);
0563     binning.add<unsigned int>("n_bins_x", 210);
0564     binning.add<double>("pixel_x_offset", 40.);
0565     binning.add<unsigned int>("n_bins_y", 400);
0566     binning.add<double>("y_min", -20.);
0567     binning.add<double>("y_max", 20.);
0568 
0569     binning.add<unsigned int>("diffFN_n_bins_x", 100);
0570     binning.add<double>("diffFN_x_min", 0.);
0571     binning.add<double>("diffFN_x_max", 20.);
0572 
0573     binning.add<unsigned int>("slice_n_bins_x", 100);
0574     binning.add<double>("slice_x_min", -10.);
0575     binning.add<double>("slice_x_max", 10.);
0576     binning.add<unsigned int>("slice_n_bins_y", 100);
0577     binning.add<double>("slice_y_min", -2.);
0578     binning.add<double>("slice_y_max", 2.);
0579 
0580     desc.add<edm::ParameterSetDescription>("binning", binning);
0581   }
0582 
0583   desc.add<std::vector<double>>("extra_params", {});
0584 
0585   descriptions.add("ppsAlignmentConfigurationESSource", desc);
0586 }
0587 
0588 //---------------------------------------------------------------------------------------------

0589 
0590 // Performs a breadth first search on dir. If found, returns the directory with object

0591 // named searchName inside. Otherwise, returns nullptr.

0592 TDirectory* PPSAlignmentConfigurationESSource::findDirectoryWithName(TDirectory* dir, std::string searchName) {
0593   TIter next(dir->GetListOfKeys());
0594   std::queue<TDirectory*> dirQueue;
0595   TObject* o;
0596   while ((o = next())) {
0597     TKey* k = (TKey*)o;
0598 
0599     std::string name = k->GetName();
0600     if (name == searchName)
0601       return dir;
0602 
0603     if (((TSystemFile*)k)->IsDirectory())
0604       dirQueue.push((TDirectory*)k->ReadObj());
0605   }
0606 
0607   while (!dirQueue.empty()) {
0608     TDirectory* resultDir = findDirectoryWithName(dirQueue.front(), searchName);
0609     dirQueue.pop();
0610     if (resultDir != nullptr)
0611       return resultDir;
0612   }
0613 
0614   return nullptr;
0615 }
0616 
0617 //---------------------------------------------------------------------------------------------

0618 
0619 // Builds vector of PointErrors instances from slice plots in dir.

0620 std::vector<PPSAlignmentConfiguration::PointErrors> PPSAlignmentConfigurationESSource::buildVectorFromDirectory(
0621     TDirectory* dir, const PPSAlignmentConfiguration::RPConfig& rpd) {
0622   std::vector<PPSAlignmentConfiguration::PointErrors> pv;
0623 
0624   TIter next(dir->GetListOfKeys());
0625   TObject* o;
0626   while ((o = next())) {
0627     TKey* k = (TKey*)o;
0628 
0629     std::string name = k->GetName();
0630     size_t d = name.find('-');
0631     std::replace(name.begin(), name.end(), '_', '.');  // replace _ with .

0632     const double x_min = std::stod(name.substr(0, d));
0633     const double x_max = std::stod(name.substr(d + 1));
0634 
0635     TDirectory* d_slice = (TDirectory*)k->ReadObj();
0636 
0637     TH1D* h_y = (TH1D*)d_slice->Get("h_y");
0638     TProfile* p_y_diffFN_vs_y = (TProfile*)d_slice->Get("p_y_diffFN_vs_y");
0639 
0640     double y_cen = h_y->GetMean();
0641     double y_width = h_y->GetRMS();
0642 
0643     y_cen += rpd.y_cen_add_;
0644     y_width *= rpd.y_width_mult_;
0645 
0646     double sl = 0., sl_unc = 0.;
0647     int fr = alig_utils::fitProfile(
0648         p_y_diffFN_vs_y, y_cen, y_width, fitProfileMinBinEntries, fitProfileMinNReasonable, sl, sl_unc);
0649     if (fr != 0)
0650       continue;
0651 
0652     if (debug)
0653       p_y_diffFN_vs_y->Write(name.c_str());
0654 
0655     pv.push_back({(x_max + x_min) / 2., sl, (x_max - x_min) / 2., sl_unc});
0656   }
0657 
0658   return pv;
0659 }
0660 
0661 //---------------------------------------------------------------------------------------------

0662 
0663 void PPSAlignmentConfigurationESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey& key,
0664                                                        const edm::IOVSyncValue& iosv,
0665                                                        edm::ValidityInterval& oValidity) {
0666   edm::LogInfo("PPS") << ">> PPSAlignmentConfigurationESSource::setIntervalFor(" << key.name() << ")\n"
0667                       << "    run=" << iosv.eventID().run() << ", event=" << iosv.eventID().event();
0668 
0669   edm::ValidityInterval infinity(iosv.beginOfTime(), iosv.endOfTime());
0670   oValidity = infinity;
0671 }
0672 
0673 DEFINE_FWK_EVENTSETUP_SOURCE(PPSAlignmentConfigurationESSource);