Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-08 23:57:36

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 
0029 #include "TF1.h"
0030 #include "TProfile.h"
0031 #include "TFile.h"
0032 #include "TKey.h"
0033 #include "TSystemFile.h"
0034 
0035 //---------------------------------------------------------------------------------------------

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

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

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

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

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

0322 
0323 // most default values come from 2018 period

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

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

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

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

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

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

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

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

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

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

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

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

0619 std::vector<PPSAlignmentConfiguration::PointErrors> PPSAlignmentConfigurationESSource::buildVectorFromDirectory(
0620     TDirectory* dir, const PPSAlignmentConfiguration::RPConfig& rpd) {
0621   std::vector<PPSAlignmentConfiguration::PointErrors> pv;
0622 
0623   TIter next(dir->GetListOfKeys());
0624   TObject* o;
0625   while ((o = next())) {
0626     TKey* k = (TKey*)o;
0627 
0628     std::string name = k->GetName();
0629     size_t d = name.find('-');
0630     const double x_min = std::stod(name.substr(0, d));
0631     const double x_max = std::stod(name.substr(d + 1));
0632 
0633     TDirectory* d_slice = (TDirectory*)k->ReadObj();
0634 
0635     TH1D* h_y = (TH1D*)d_slice->Get("h_y");
0636     TProfile* p_y_diffFN_vs_y = (TProfile*)d_slice->Get("p_y_diffFN_vs_y");
0637 
0638     double y_cen = h_y->GetMean();
0639     double y_width = h_y->GetRMS();
0640 
0641     y_cen += rpd.y_cen_add_;
0642     y_width *= rpd.y_width_mult_;
0643 
0644     double sl = 0., sl_unc = 0.;
0645     int fr = alig_utils::fitProfile(
0646         p_y_diffFN_vs_y, y_cen, y_width, fitProfileMinBinEntries, fitProfileMinNReasonable, sl, sl_unc);
0647     if (fr != 0)
0648       continue;
0649 
0650     if (debug)
0651       p_y_diffFN_vs_y->Write(name.c_str());
0652 
0653     pv.push_back({(x_max + x_min) / 2., sl, (x_max - x_min) / 2., sl_unc});
0654   }
0655 
0656   return pv;
0657 }
0658 
0659 //---------------------------------------------------------------------------------------------

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