00001 #ifndef HZZ2L2NU_INCLUDE_UTILS_H_
00002 #define HZZ2L2NU_INCLUDE_UTILS_H_
00003
00004 #include <cmath>
00005 #include <filesystem>
00006 #include <map>
00007 #include <memory>
00008 #include <string>
00009 #include <string_view>
00010 #include <vector>
00011
00012 #include <TFile.h>
00013 #include <TH1.h>
00014 #include <TH2.h>
00015 #include <TLorentzVector.h>
00016 #include <TString.h>
00017 #include <TTreeReaderArray.h>
00018 #include <TVector2.h>
00019
00020 #include <HZZException.h>
00021 #include <Options.h>
00022 #include <PhysicsObjects.h>
00023
00024
00025 namespace utils {
00026
00028 inline double DeltaR2(double eta1, double phi1, double eta2, double phi2) {
00029 double const dPhi = TVector2::Phi_mpi_pi(phi1 - phi2);
00030 return std::pow(eta1 - eta2, 2) + std::pow(dPhi, 2);
00031 }
00032
00034 inline double DeltaR2(TLorentzVector const &p1, TLorentzVector const &p2) {
00035 return DeltaR2(p1.Eta(), p1.Phi(), p2.Eta(), p2.Phi());
00036 }
00037
00038 double deltaPhi(TLorentzVector const &v1, TLorentzVector const &v2);
00039
00040 double deltaPhi (float phi1, float phi2);
00041
00042 bool PassVbfCuts(std::vector<Jet> const &jets, TLorentzVector const &boson);
00043
00044 std::map<double, double> TH1toMap(TH1D *h_weight);
00045
00046 std::map<double, std::pair<double, double>> TH1toMap(
00047 std::string const &fileName, std::string const &histoName);
00048
00049 std::map<std::pair<double, double>, std::pair<double, double>> TH2toMap(
00050 std::string const &fileName, std::string const &histoName);
00051
00052 void loadInstrMETWeights(
00053 bool applyNvtxWeights, bool applyPtWeights, bool applyMassLineshape,
00054 std::map<TString, std::map<double, std::pair<double, double>>> &NvtxWeight_map,
00055 std::map<TString, std::map<double, std::pair<double, double>>> &PtWeight_map,
00056 std::map<TString, TH1*> &LineshapeMassWeight_map,
00057 std::vector<std::string> const &v_jetCat,
00058 Options const &options);
00059
00060 void loadMeanWeights(
00061 bool applyMeanWeights,
00062 std::map<TString, std::map<double, double>> &meanWeight_map,
00063 std::vector<std::string> const &v_jetCat,
00064 Options const &options);
00065
00073 template<typename T = TH1>
00074 std::unique_ptr<T> ReadHistogram(
00075 std::filesystem::path const &path, std::string const &name,
00076 bool checkMissing = true) {
00077 TFile inputFile{path.c_str()};
00078 if (inputFile.IsZombie()) {
00079 HZZException exception;
00080 exception << "Could not open file " << path << ".";
00081 throw exception;
00082 }
00083
00084 auto hist = dynamic_cast<T *>(inputFile.Get(name.c_str()));
00085 if (hist)
00086 hist->SetDirectory(nullptr);
00087 inputFile.Close();
00088
00089 if (checkMissing and not hist) {
00090 HZZException exception;
00091 exception << "File " << path << " does not contain required histogram \"" <<
00092 name << "\".";
00093 throw exception;
00094 }
00095
00096 return std::unique_ptr<T>{hist};
00097 }
00098
00099 }
00100
00101 #endif // HZZ2L2NU_INCLUDE_UTILS_H_