Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:56

0001 #ifndef ALIGNMENT_OFFLINEVALIDATION_TREND_H
0002 #define ALIGNMENT_OFFLINEVALIDATION_TREND_H
0003 
0004 #include "boost/filesystem.hpp"
0005 #include "boost/property_tree/ptree.hpp"
0006 #include "boost/property_tree/json_parser.hpp"
0007 
0008 #include "TROOT.h"
0009 #include "TCanvas.h"
0010 #include "TLegend.h"
0011 #include "TGraph.h"
0012 
0013 ////////////////////////////////////////////////////////////////////////////////
0014 /// Functor to obtain delivered luminosity corresponding to a given range.
0015 ///
0016 /// The constructor extracts the luminosity from an 2-column file (see constructor),
0017 /// and the operator() returns the accumulated luminosity for any subrange.
0018 ///
0019 /// Another overload of the operator() allows the conversion of a `TGraphErrors`
0020 /// as a function of the run number to another `TGraphErrors` as a function of the
0021 /// luminosity.
0022 struct Run2Lumi {
0023   const int firstRun,  //!< first run, starting at lumi = 0 on the x-axis of the trend
0024       lastRun;         //!< last run (excluded!), starting at the max lumi on the x-axis of the trend
0025   const float convertUnit;
0026 
0027 private:
0028   std::map<int, float> runs;  //!< couples of run and corresponding luminosity
0029 
0030 public:
0031   ////////////////////////////////////////////////////////////////////////////////
0032   /// Constructor, takes as input a 2-column file that will populate the map `runs`
0033   /// as well as the global range.
0034   ///
0035   /// (Note: in principle, the global range could be extracted from the 2-column file,
0036   /// but the choice here is to have one standard lumi file, leaving as only freedom
0037   /// to the user the possibility to change the global range depending on the analysis.)
0038   Run2Lumi(boost::filesystem::path file,  //!< path to a 2-column file with 6-digit run number and lumi in /pb
0039            int first,                     //!< 6-digit run number (included)
0040            int last,                      //!< 6-digit run number (excluded)
0041            float convertUnit              //!< default is from pb to fb
0042   );
0043 
0044   ////////////////////////////////////////////////////////////////////////////////
0045   /// Sums luminosity for [run1, run2[ and returns it in /fb.
0046   float operator()(int run1, int run2) const;
0047 
0048   ////////////////////////////////////////////////////////////////////////////////
0049   /// Sums luminosity for [firstRun, run[
0050   float operator()(int run) const;
0051 
0052   ////////////////////////////////////////////////////////////////////////////////
0053   /// Sums luminosity for [firstRun, lastRun[
0054   float operator()() const;
0055 
0056   ////////////////////////////////////////////////////////////////////////////////
0057   /// Converts a `TGraph` given as a function of the run number
0058   /// to another `TGraph` as a function of the luminosity
0059   /// in the global subrange.
0060   TGraph* operator()(TGraph* gIn) const;
0061 
0062   ////////////////////////////////////////////////////////////////////////////////
0063   /// Converts a `TH1` given as a function of the run number
0064   /// to another `TH1` as a function of the luminosity
0065   /// in the global subrange.
0066   ///
0067   /// Note: the run is expected to be on the low edge of each bin
0068   TH1* operator()(TH1* hIn) const;
0069 };
0070 
0071 ////////////////////////////////////////////////////////////////////////////////
0072 /// Standard canvas for TkAl trends as a function of delivered lumi.
0073 ///
0074 /// The constructor and the destructor take care of the style,
0075 /// The operator() is overloaded and should be given a trend as a function of
0076 /// the run number (either `TGraphErrors` or `TH1`).
0077 /// The verticles lines (e.g. pixel templates) are given via a JSON file.
0078 struct Trend {
0079   TString CMS = "#scale[1.1]{#bf{CMS}} #it{Internal}";  //!< top left label
0080   TString lumi =
0081       "#scale[0.8]{pp collisions (2016+2017+2018)}";  //!< top right label (not necessarily the lumi, just following the convention from `CMS_lumi.h`)
0082   std::string plotUnit = "fb";
0083 
0084   float fontsize = 0.04;
0085 
0086   TCanvas c;
0087   const char* outputDir;  //directory for plots
0088   TLegend lgd;
0089 
0090   const boost::property_tree::ptree JSON;  //!< contains coordinate for vertical lines
0091   const Run2Lumi& GetLumi;                 //!< functor to get luminosity for given subrange
0092   const char* lumiType;                    //specify whether luminosity is recorded or delivered
0093 
0094   Trend                                    //!< constructor, prepares canvas and frame
0095       (const char* name,                   //!< TCanvas name, also used for output PDF
0096        const char* dir,                    //directory for plots
0097        const char* title,                  //!< TCanvas title, also used for output PDF (but not shown on the canvas)
0098        const char* ytitle,                 //!< y-axis title
0099        float ymin,                         //!< y-axis minimum
0100        float ymax,                         //!< y-axis maximum
0101        boost::property_tree::ptree& json,  //!< vertical lines from JSON
0102        const Run2Lumi& GetLumiFunctor,     //!< functor
0103        const char* lumiAxisType            //specify whether luminosity is recorded or delivered
0104       );
0105 
0106   ////////////////////////////////////////////////////////////////////////////////
0107   /// Operator overloading to plot a trend (given as a function of run number)
0108   /// as a function of the luminosity.
0109   void operator()(TObject* obj,          //!< e.g. graph
0110                   TString drawOpt,       //!< e.g. option for `TGraph::Draw()`
0111                   TString lgdOpt,        //!< option for `TLegend::Draw()`
0112                   bool fullRange = true  //!< flag to force the graph to touch the right edge
0113   );
0114 
0115   ////////////////////////////////////////////////////////////////////////////////
0116   /// Destructor applies fine tuning of the plots, e.g. legend, labels, etc.
0117   /// and finally prints to PDF.
0118   ~Trend();
0119 };
0120 
0121 template <typename T, typename... Args>
0122 inline T* Get(Args... args) {
0123   return dynamic_cast<T*>(gDirectory->Get(args...));
0124 }
0125 
0126 #endif  // ALIGNMENT_OFFLINEVALIDATION_TREND_H