Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-11-09 11:29:10

0001 #ifndef ALIGNMENT_OFFLINEVALIDATION_PREPAREPVTRENDS_H_
0002 #define ALIGNMENT_OFFLINEVALIDATION_PREPAREPVTRENDS_H_
0003 
0004 #include "TArrow.h"
0005 #include "TAxis.h"
0006 #include "TCanvas.h"
0007 #include "TF1.h"
0008 #include "TFile.h"
0009 #include "TGaxis.h"
0010 #include "TGraph.h"
0011 #include "TGraphAsymmErrors.h"
0012 #include "TGraphErrors.h"
0013 #include "TH1F.h"
0014 #include "TH2F.h"
0015 #include "TLatex.h"
0016 #include "TLegend.h"
0017 #include "TList.h"
0018 #include "TMath.h"
0019 #include "TObjArray.h"
0020 #include "TObjString.h"
0021 #include "TPad.h"
0022 #include "TPaveText.h"
0023 #include "TProcPool.h"
0024 #include "TProfile.h"
0025 #include "TROOT.h"
0026 #include "TStyle.h"
0027 #include "TSystem.h"
0028 #include "TSystemDirectory.h"
0029 #include "TSystemFile.h"
0030 #include <TStopwatch.h>
0031 #include <algorithm>
0032 #include <bitset>
0033 #include <fstream>
0034 #include <functional>
0035 #include <iostream>
0036 #include <iterator>
0037 #include <map>
0038 #include <sstream>
0039 #include <vector>
0040 #include "boost/property_tree/ptree.hpp"
0041 #include "boost/property_tree/json_parser.hpp"
0042 
0043 #include "Alignment/OfflineValidation/interface/OutPVtrends.h"
0044 
0045 /*!
0046  * \def some convenient I/O
0047  */
0048 #define logInfo std::cout << "INFO: "
0049 #define logWarning std::cout << "WARNING: "
0050 #define logError std::cout << "ERROR!!! "
0051 
0052 /*!
0053  * \def boolean to decide if it is in debug mode
0054  */
0055 #define VERBOSE false
0056 
0057 namespace pv {
0058   enum view { dxyphi, dzphi, dxyeta, dzeta, pT, generic };
0059 
0060   /*! \fn closest
0061    *  \brief method to find first value that doesn not compare left
0062    */
0063 
0064   inline int closest(std::vector<int> const &vec, int value) {
0065     auto const it = std::lower_bound(vec.begin(), vec.end(), value);
0066     if (it == vec.end()) {
0067       return -1;
0068     }
0069     return *it;
0070   }
0071 
0072   const Int_t markers[8] = {kFullSquare,
0073                             kFullCircle,
0074                             kFullTriangleDown,
0075                             kOpenSquare,
0076                             kOpenCircle,
0077                             kFullTriangleUp,
0078                             kOpenTriangleDown,
0079                             kOpenTriangleUp};
0080   const Int_t colors[8] = {kBlack, kRed, kBlue, kGreen + 2, kMagenta, kViolet, kCyan, kYellow};
0081 
0082   /*! \struct biases
0083    *  \brief Structure biases
0084    *         Contains characterization of a single run PV bias plot
0085    *
0086    * @param m_mean:             mean value of the profile points
0087    * @param m_rms:              RMS value of the profle points
0088    * @param m_w_mean:           mean weighted on the errors
0089    * @param m_w_rms:            RMS weighted on the errors
0090    * @param m_min:              minimum of the profile
0091    * @param m_max:              maximum of the profile
0092    * @param m_chi2:             chi2 of a liner fit
0093    * @param m_ndf:              number of the dof of the linear fit
0094    * @param m_ks:               Kolmogorov-Smirnov score of comparison with flat line
0095    */
0096 
0097   struct biases {
0098     // contructor
0099     biases(double mean, double rms, double wmean, double wrms, double min, double max, double chi2, int ndf, double ks) {
0100       m_mean = mean;
0101       m_rms = rms;
0102       m_w_mean = wmean;
0103       m_w_rms = wrms;
0104       m_min = min;
0105       m_max = max;
0106       m_chi2 = chi2;
0107       m_ndf = ndf;
0108       m_ks = ks;
0109     }
0110 
0111     // empty constructor
0112     biases() { init(); }
0113 
0114     /*! \fn init
0115      *  \brief initialising all members one by one
0116      */
0117 
0118     void init() {
0119       m_mean = 0;
0120       m_rms = 0.;
0121       m_min = +999.;
0122       m_max = -999.;
0123       m_w_mean = 0.;
0124       m_w_rms = 0.;
0125       m_chi2 = -1.;
0126       m_ndf = 0.;
0127       m_ks = 9999.;
0128     }
0129 
0130     double getMean() { return m_mean; }
0131     double getWeightedMean() { return m_w_mean; }
0132     double getRMS() { return m_rms; }
0133     double getWeightedRMS() { return m_w_rms; }
0134     double getMin() { return m_min; }
0135     double getMax() { return m_max; }
0136     double getChi2() { return m_chi2; }
0137     double getNDF() { return m_ndf; }
0138     double getNormChi2() { return double(m_chi2) / double(m_ndf); }
0139     double getChi2Prob() { return TMath::Prob(m_chi2, m_ndf); }
0140     double getKSScore() { return m_ks; }
0141 
0142   private:
0143     double m_mean;
0144     double m_min;
0145     double m_max;
0146     double m_rms;
0147     double m_w_mean;
0148     double m_w_rms;
0149     double m_chi2;
0150     int m_ndf;
0151     double m_ks;
0152   };
0153 
0154   /*! \struct wrappedTrends
0155    *  \brief Structure wrappedTrends
0156    *         Contains the ensemble vs run number of the alignmentTrend characterization
0157    *
0158    * @param mean:             alignmentTrend of the mean value of the profile points
0159    * @param low:              alignmentTrend of the lowest value of the profle points
0160    * @param high:             alignmentTrend of the highest value of the profile points
0161    * @param lowerr:           alignmentTrend of the difference between the lowest value and the mean of the profile points
0162    * @param higherr:          alignmentTrend of the difference between the highest value and the mean of the profile points
0163    * @param chi2:             alignmentTrend of the chi2 value of a linear fit to the profile points
0164    * @param KS:               alignmentTrend of the Kolmogorow-Smirnov score of the comarison of the profile points to a flat line
0165    */
0166 
0167   struct wrappedTrends {
0168     /*! \fn wrappedTrends
0169      *  \brief Constructor of structure wrappedTrends, initialising all members from DMRs directly (with split)
0170      */
0171 
0172     wrappedTrends(alignmentTrend mean,
0173                   alignmentTrend low,
0174                   alignmentTrend high,
0175                   alignmentTrend lowerr,
0176                   alignmentTrend higherr,
0177                   alignmentTrend chi2,
0178                   alignmentTrend KS) {
0179       logInfo << "pv::wrappedTrends c'tor" << std::endl;
0180 
0181       m_mean = mean;
0182       m_low = low;
0183       m_high = high;
0184       m_lowerr = lowerr;
0185       m_higherr = higherr;
0186       m_chi2 = chi2;
0187       m_KS = KS;
0188     }
0189 
0190     alignmentTrend getMean() const { return m_mean; }
0191     alignmentTrend getLow() const { return m_low; }
0192     alignmentTrend getHigh() const { return m_high; }
0193     alignmentTrend getLowErr() const { return m_lowerr; }
0194     alignmentTrend getHighErr() const { return m_higherr; }
0195     alignmentTrend getChi2() const { return m_chi2; }
0196     alignmentTrend getKS() const { return m_KS; }
0197 
0198   private:
0199     alignmentTrend m_mean;
0200     alignmentTrend m_low;
0201     alignmentTrend m_high;
0202     alignmentTrend m_lowerr;
0203     alignmentTrend m_higherr;
0204     alignmentTrend m_chi2;
0205     alignmentTrend m_KS;
0206   };
0207 
0208   /*! \struct bundle
0209    *  \brief Structure bundle
0210    *         Contains the ensemble of all the information to build the graphs alignmentTrends
0211    *
0212    * @param nObjects                     int, number of alignments to be considered
0213    * @param dataType                     TString, type of the data to be displayed (time, lumi)
0214    * @param dataTypeLabel                TString, x-axis label
0215    */
0216 
0217   struct bundle {
0218     bundle(int nObjects, const TString &dataType, const TString &dataTypeLabel, const bool &useRMS) {
0219       m_nObjects = nObjects;
0220       m_datatype = dataType.Data();
0221       m_datatypelabel = dataTypeLabel.Data();
0222       m_useRMS = useRMS;
0223 
0224       logInfo << "pv::bundle c'tor: " << dataTypeLabel << " member: " << m_datatypelabel << std::endl;
0225 
0226       logInfo << m_axis_types << std::endl;
0227     }
0228 
0229     int getNObjects() const { return m_nObjects; }
0230     const char *getDataType() const { return m_datatype; }
0231     const char *getDataTypeLabel() const { return m_datatypelabel; }
0232     bool isUsingRMS() const { return m_useRMS; }
0233     void printAll() {
0234       logInfo << "dataType      " << m_datatype << std::endl;
0235       logInfo << "dataTypeLabel " << m_datatypelabel << std::endl;
0236     }
0237 
0238   private:
0239     int m_nObjects;
0240     const char *m_datatype;
0241     const char *m_datatypelabel;
0242     std::bitset<2> m_axis_types;
0243     bool m_useRMS;
0244   };
0245 
0246 }  // namespace pv
0247 
0248 class PreparePVTrends {
0249 public:
0250   PreparePVTrends(const char *outputFileName, int nWorkers, boost::property_tree::ptree &json);
0251   ~PreparePVTrends() {}
0252 
0253   void setDirsAndLabels(boost::property_tree::ptree &json);
0254 
0255   void multiRunPVValidation(bool useRMS = true, TString lumiInputFile = "", bool doUnitTest = false);
0256 
0257   static pv::biases getBiases(TH1F *hist);
0258   static unrolledHisto getUnrolledHisto(TH1F *hist);
0259   static TH1F *drawConstantWithErr(TH1F *hist, Int_t iter, Double_t theConst);
0260   static outPVtrends processData(size_t iter,
0261                                  std::vector<int> intersection,
0262                                  const Int_t nDirs_,
0263                                  const char *dirs[10],
0264                                  TString LegLabels[10],
0265                                  bool useRMS,
0266                                  const size_t nWorkers,
0267                                  bool doUnitTest);
0268   std::vector<int> list_files(const char *dirname = ".", const char *ext = ".root");
0269   void outputGraphs(const pv::wrappedTrends &allInputs,
0270                     const std::vector<double> &ticks,
0271                     const std::vector<double> &ex_ticks,
0272                     TGraph *&g_mean,
0273                     TGraph *&g_chi2,
0274                     TGraph *&g_KS,
0275                     TGraph *&g_low,
0276                     TGraph *&g_high,
0277                     TGraphAsymmErrors *&g_asym,
0278                     TH1F *h_RMS[],
0279                     const pv::bundle &mybundle,
0280                     const pv::view &theView,
0281                     const int index,
0282                     const TString &label);
0283 
0284 private:
0285   const char *outputFileName_;
0286   const size_t nWorkers_;  //def number of workers
0287   std::vector<std::string> DirList;
0288   std::vector<std::string> LabelList;
0289 };
0290 
0291 #endif  // ALIGNMENT_OFFLINEVALIDATION_PREPAREPVTRENDS_H_