SusyNtuple
SusyNtuple/MCWeighter.h
00001 #ifndef SusyNtuple_MCWeighter_h
00002 #define SusyNtuple_MCWeighter_h
00003 
00004 #include "TChain.h"
00005 #include "TString.h"
00006 
00007 #include "SUSYTools/SUSYCrossSection.h"
00008 #include "SusyNtuple/SusyDefs.h"
00009 
00010 #include <string>
00011 
00012 class TH1F;
00013 namespace Susy{ class Event; }
00014 
00016 
00039 class MCWeighter
00040 {
00041 
00042  public:
00043   struct SumwMapKey {
00044     unsigned int dsid;
00045     int proc;
00046   SumwMapKey(): dsid(0), proc(0) {}
00047   SumwMapKey(unsigned int d, int p): dsid(d), proc(p){}
00048   };
00049 
00050   public:
00051 
00052   //typedef std::pair<unsigned int, int> SumwMapKey;
00053 typedef std::map<SumwMapKey, float> SumwMap;
00054 
00055     //
00056     // Enums to control weighting options
00057     //
00058 
00059     // Sumw method
00060     enum SumwMethod
00061     {
00062       Sumw_NT = 0,      // Use sumw stored in the ntuple
00063       Sumw_MAP          // Use SumwMap (recommended)
00064       //Sumw_FILE       // Use a text file for sumw (not supported)
00065     };
00066 
00067     // Xsec method
00068     enum XsecMethod
00069     {
00070       Xsec_NT = 0,      // Use xsec stored in the ntuple
00071       Xsec_ST           // Use xsec from SUSYTools (recommended)
00072     };
00073 
00074     // Systematic
00075     enum WeightSys
00076     {
00077       Sys_NOM = 0,
00078       Sys_XSEC_UP,
00079       Sys_XSEC_DN,
00080       Sys_PILEUP_UP,
00081       Sys_PILEUP_DN,
00082       Sys_N
00083     };
00084 
00086     struct ProcessValidator {
00087     ProcessValidator() : counts_total(0), counts_invalid(0), max_warnings(4), valid(false), last(0) {}
00088       size_t counts_total;
00089       size_t counts_invalid;
00090       size_t max_warnings;
00091       ProcessValidator& zero_hack(int &value) { value = 0; valid = true; return *this; }
00095       ProcessValidator& validate(int &value);
00096       bool valid; 
00097       int last; 
00098       std::string summary() const;
00100 
00106       static int convertDefaultSusyNt2DefaultSusyTools(const int &v);
00107       static const int defaultSusyNt = -1; 
00108       static const int defaultSusyTools = 0; 
00109 
00110     };
00111 
00112     //
00113     // Initialization and configuration
00114     //
00115 
00116     MCWeighter();
00117     MCWeighter(TTree* tree,
00118                std::string xsecDir = "$ROOTCOREDIR/data/SUSYTools/mc12_8TeV/");
00119     ~MCWeighter();
00120 
00122 
00131     void buildSumwMap(TTree* tree);
00132     void clearAndRebuildSumwMap(TTree* tree) { m_sumwMap.clear(); buildSumwMap(tree); }
00133     void dumpSumwMap() const;
00134     void dumpXsecCache() const;
00135     void dumpXsecDb() const;
00136 
00138     void setUseProcSumw(bool useProcSumw=true) { m_useProcSumw = useProcSumw; }
00139     void setSumwMethod(SumwMethod opt=Sumw_MAP) { m_sumwMethod = opt; }
00140     void setXsecMethod(XsecMethod opt=Xsec_ST) { m_xsecMethod = opt; }
00141 
00143     float getMCWeight(const Susy::Event* evt, const float lumi = LUMI_A_A3, WeightSys sys=Sys_NOM);
00144     bool sumwmapHasKey(SumwMapKey k);
00145 
00147     float total_sumw; 
00148     float getSumw(const Susy::Event* evt);
00150     SUSY::CrossSectionDB::Process getCrossSection(const Susy::Event* evt);
00151     float getXsecTimesEff(const Susy::Event* evt, WeightSys sys=Sys_NOM);
00153     float getPileupWeight(const Susy::Event* evt, WeightSys sys=Sys_NOM);
00155 
00159     MCWeighter& setLabelBinCounter(const std::string &v);
00161 
00165     size_t parseAdditionalXsecFile(const std::string &filename, bool verbose);
00167     size_t parseAdditionalXsecDirectory(const std::string &dir, bool verbose);
00169     MCWeighter& setAllowInvalid(bool v);
00171     MCWeighter& setVerbose(bool v);
00173 
00178     static std::string defaultLabelBinCounter(const unsigned int &dsid, bool verbose);
00180     static void checkHistoHasBin(const TH1F &histo, const std::string &binLabel);
00182     static std::string defaultXsecDir() {
00183       return std::string("$ROOTCOREBIN/data/SUSYTools/mc15_13TeV/");
00184     }
00186     static std::vector<std::string> xsecFilesForSimplifiedModels();
00188     static std::vector<int> dsidsForKnownSimpliedModelSamples(bool verbose);
00190 
00198     static bool isFormattedAsSusyCrossSection(std::string filename, bool verbose);
00200     static std::vector<int> readDsidsFromSusyCrossSectionFile(std::string filename, bool verbose);
00202     static bool readDsidsFromSusyCrossSectionLine(const std::string &line, int &dsid, bool verbose);
00204 
00210     static bool isSimplifiedModel(const unsigned int &dsid, bool verbose);
00212     static const Susy::Event& readFirstEvent(TTree* tree);
00214     static int extractProcessFromCutflowHistoname(const std::string &histoName, const std::string &prefix);
00215  private:
00216     void buildSumwMapFromTree(TTree* tree);
00217     void buildSumwMapFromChain(TChain* chain);
00218 
00219     bool m_useProcSumw;
00220 
00221     // typedefs, for convenience
00222     typedef std::pair<int, int> intpair;
00223     typedef std::map<intpair, SUSY::CrossSectionDB::Process> XSecMap;
00224 
00225     SumwMethod m_sumwMethod;
00226     XsecMethod m_xsecMethod;
00227 
00228     // Map of (MCID, proc) -> sumw
00229     SumwMap m_sumwMap;
00230 
00232     SUSY::CrossSectionDB m_xsecDB;
00233     XSecMap m_xsecCache;
00234 
00235     std::string m_labelBinCounter; 
00236     size_t m_warningCounter;
00237     bool m_allowInvalid; 
00238     ProcessValidator m_procidValidator; 
00239     bool m_verbose; 
00240 };
00241 
00243 inline bool operator<(const MCWeighter::SumwMapKey &a, const MCWeighter::SumwMapKey &b)
00244 { return a.dsid<b.dsid && a.proc<b.proc; }
00245 
00246 #endif
 All Classes Functions Variables Typedefs Enumerations Enumerator