Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:40:01

0001 // Common style file for TkAl public plots
0002 // --------------------------------------------------------------
0003 //
0004 // Defines a set of functions to define the plotting style and
0005 // provide commonly used style objects such as histogram title
0006 // and legends. Can be used in compiled code and uncompiled scripts.
0007 //
0008 // Always call TkAlStyle::set(PublicationStatus) in the beginning
0009 // of your plotting script to adjust the gStyle options. Also,
0010 // the behaviour of some other methods of this class depend on
0011 // this, e.g. the histogram title displays "CMS preliminary" etc.
0012 // depending on the PublicationStatus. Call TkAlStyle::set before
0013 // declaring any histograms (or call TH1::UseCurrentStyle()) to
0014 // make sure the style settings are used.
0015 // --------------------------------------------------------------
0016 
0017 #ifndef ALIGNMENT_OFFLINEVALIDATION_TKAL_STYLE_H
0018 #define ALIGNMENT_OFFLINEVALIDATION_TKAL_STYLE_H
0019 
0020 #include "TColor.h"
0021 #include "TError.h"
0022 #include "TLegend.h"
0023 #include "TPaveText.h"
0024 #include "TString.h"
0025 #include "TStyle.h"
0026 #include <iostream>
0027 #include <string>
0028 #include <algorithm>
0029 
0030 // Publication status: determines what is plotted in title
0031 enum PublicationStatus {
0032   NO_STATUS,
0033   INTERNAL,
0034   INTERNAL_SIMULATION,
0035   PRELIMINARY,
0036   PUBLIC,
0037   SIMULATION,
0038   UNPUBLISHED,
0039   CUSTOM
0040 };
0041 
0042 // Data era: determines labels of data-taking periods, e.g. CRUZET
0043 enum Era { NONE, CRUZET15, CRAFT15, COLL0T15 };
0044 
0045 class TkAlStyle {
0046 public:
0047   // Adjusts the gStyle settings and store the PublicationStatus
0048   static void set(const PublicationStatus status,
0049                   const Era era = NONE,
0050                   const TString customTitle = "",
0051                   const TString customRightTitle = "");
0052   static void set(const TString customTitle);
0053   static PublicationStatus status() { return publicationStatus_; }
0054 
0055   // Draws a title "<CMS label> 2015" on the current pad
0056   // dependending on the PublicationStatus
0057   //  INTERNAL    : no extra label (intended for AN-only plots with data)
0058   //  INTERNAL    : show "Simulation" label (intended for AN-only plots, no "CMS")
0059   //  PRELIMINARY : show "CMS preliminary 2015" label
0060   //  PUBLIC      : show "CMS 2015" label
0061   //  SIMULATION  : show "CMS Simulation" label
0062   //  UNPUBLISHED : show "CMS (unpublished)" label (intended for additional material on TWiki)
0063   // Note that this method does not allow for easy memory
0064   // handling. For that, use standardTitle().
0065   static void drawStandardTitle() {
0066     standardTitle()->Draw("same");
0067     standardRightTitle()->Draw("same");
0068   }
0069   static void drawStandardTitle(const Era era) {
0070     standardTitle()->Draw("same");
0071     standardRightTitle(era)->Draw("same");
0072   }
0073   static PublicationStatus toStatus(std::string _status);
0074 
0075   // Returns a TPaveText object that fits as a histogram title
0076   // with the current pad dimensions.
0077   // It has the same text as described in drawStandardTitle().
0078   // The idea of this method is that one has control over the
0079   // TPaveText object and can do proper memory handling.
0080   static TPaveText* standardTitle(PublicationStatus status) { return title(header(status)); }
0081   static TPaveText* standardTitle() { return standardTitle(publicationStatus_); }
0082 
0083   static TPaveText* standardRightTitle(const Era era) { return righttitle(rightheader(era)); }
0084   static TPaveText* standardRightTitle() { return standardRightTitle(era_); }
0085 
0086   // Returns a TPaveText object that fits as a histogram title
0087   // with the current pad dimensions and displays the specified text txt.
0088   static TPaveText* customTitle(const TString& txt) { return title(txt); }
0089   static TPaveText* customRightTitle(const TString& txt) { return righttitle(txt); }
0090 
0091   static TString legendheader;
0092   static TString legendoptions;
0093   static double textSize;
0094   // Returns a TLegend object that fits into the top-right corner
0095   // of the current pad. Its width, relative to the pad size (without
0096   // margins), can be specified. Its height is optimized for nEntries
0097   // entries.
0098   static TLegend* legend(const int nEntries, const double relWidth = 0.5) { return legendTR(nEntries, relWidth); }
0099   static TLegend* legend(TString position, const int nEntries, const double relWidth = 0.5) {
0100     position.ToLower();
0101     if (!(position.Contains("top") || position.Contains("bottom")))
0102       position += "top";
0103     if (!(position.Contains("left") || position.Contains("right")))
0104       position += "right";
0105     TLegend* leg = nullptr;
0106     if (position.Contains("top") && position.Contains("right")) {
0107       leg = legendTR(nEntries, relWidth);
0108     } else if (position.Contains("top") && position.Contains("left")) {
0109       leg = legendTL(nEntries, relWidth);
0110     } else if (position.Contains("bottom") && position.Contains("right")) {
0111       leg = legendBR(nEntries, relWidth);
0112     } else if (position.Contains("bottom") && position.Contains("left")) {
0113       leg = legendBL(nEntries, relWidth);
0114     } else {
0115       leg = legendTR(nEntries, relWidth);
0116     }
0117 
0118     return leg;
0119   }
0120   // Same but explicitly state position on pad
0121   static TLegend* legendTL(const int nEntries, const double relWidth = 0.5) {
0122     return legend(nEntries, relWidth, true, true);
0123   }
0124   static TLegend* legendTR(const int nEntries, const double relWidth = 0.5) {
0125     return legend(nEntries, relWidth, false, true);
0126   }
0127   static TLegend* legendBL(const int nEntries, const double relWidth = 0.5) {
0128     return legend(nEntries, relWidth, true, false);
0129   }
0130   static TLegend* legendBR(const int nEntries, const double relWidth = 0.5) {
0131     return legend(nEntries, relWidth, false, false);
0132   }
0133 
0134   // Returns a TPaveText object that fits into the top-right corner
0135   // of the current pad and that can be used for additional labels.
0136   // Its width, relative to the pad size (without margins), can be
0137   // specified. Its height is optimized for nEntries entries.
0138   static TPaveText* label(const int nEntries, const double relWidth = 0.5) { return labelTR(nEntries, relWidth); }
0139 
0140   static TPaveText* label(TString position, const int nEntries, const double relWidth = 0.5) {
0141     position.ToLower();
0142     if (!(position.Contains("top") || position.Contains("bottom")))
0143       position += "top";
0144     if (!(position.Contains("left") || position.Contains("right")))
0145       position += "right";
0146     TPaveText* label = nullptr;
0147     if (position.Contains("top") && position.Contains("right")) {
0148       label = labelTR(nEntries, relWidth);
0149     } else if (position.Contains("top") && position.Contains("left")) {
0150       label = labelTL(nEntries, relWidth);
0151     } else if (position.Contains("bottom") && position.Contains("right")) {
0152       label = labelBR(nEntries, relWidth);
0153     } else if (position.Contains("bottom") && position.Contains("left")) {
0154       label = labelBL(nEntries, relWidth);
0155     } else {
0156       label = labelTR(nEntries, relWidth);
0157     }
0158 
0159     return label;
0160   }
0161 
0162   // Same but explicitly state position on pad
0163   static TPaveText* labelTL(const int nEntries, const double relWidth = 0.5) {
0164     return label(nEntries, relWidth, true, true);
0165   }
0166   static TPaveText* labelTR(const int nEntries, const double relWidth = 0.5) {
0167     return label(nEntries, relWidth, false, true);
0168   }
0169   static TPaveText* labelBL(const int nEntries, const double relWidth = 0.5) {
0170     return label(nEntries, relWidth, true, false);
0171   }
0172   static TPaveText* labelBR(const int nEntries, const double relWidth = 0.5) {
0173     return label(nEntries, relWidth, false, false);
0174   }
0175 
0176   static double lineHeight() { return lineHeight_; }
0177 
0178 private:
0179   static PublicationStatus publicationStatus_;
0180   static Era era_;
0181   static TString customTitle_;
0182   static TString customRightTitle_;
0183   static double lineHeight_;
0184   static double margin_;
0185 
0186   // creates a title
0187   static TString applyCMS(const TString& txt);
0188   static TPaveText* title(const TString& txt);
0189   static TPaveText* righttitle(const TString& txt);
0190 
0191   // returns the standard-title (CMS label 2015) depending
0192   // on the PublicationStatus
0193   static TString header(const PublicationStatus status);
0194   static TString rightheader(const Era era);
0195 
0196   // NDC coordinates for TPave, TLegend,...
0197   static void setXCoordinatesL(const double relWidth, double& x0, double& x1);
0198   static void setXCoordinatesR(const double relWidth, double& x0, double& x1);
0199   static void setYCoordinatesT(const int nEntries, double& y0, double& y1);
0200   static void setYCoordinatesB(const int nEntries, double& y0, double& y1);
0201 
0202   static TLegend* legend(const int nEntries, const double relWidth, const bool left, const bool top);
0203   static TPaveText* label(const int nEntries, const double relWidth, const bool leftt, const bool top);
0204 };
0205 
0206 #endif