diff --git a/.gitignore b/.gitignore index 82c501e6c418d..a5f7f7e2321a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ __init__.py +*.png +*.pdf +*.root *.pyc .*.swp .#* diff --git a/framework/RDFprocessor.py b/framework/RDFprocessor.py index 2d24adb3cf075..f49af845a4fe9 100644 --- a/framework/RDFprocessor.py +++ b/framework/RDFprocessor.py @@ -1,35 +1,14 @@ -import ROOT -import os -import time - -# Begin code for casting - -# this injection can be replaced by properly having this in a header -# included in the interpreter at framework startup -ROOT.gInterpreter.Declare(''' -template -class NodeCaster { - public: - static ROOT::RDF::RNode Cast(T rdf) - { - return ROOT::RDF::RNode(rdf); - } -}; -''') - -def CastToRNode(node): - return ROOT.NodeCaster(node.__cppname__).Cast(node) - -# end code for casting +from header import * class RDFprocessor: - def __init__(self, outputFile, inputFiles, histoFile, modules=[], snapshot = False): + def __init__(self, outputFile, inputFiles, histoFile, keepVars='', modules=[], snapshot = False): self.outputFile = outputFile self.inputFiles = inputFiles self.modules = modules self.histoFile = ROOT.TFile(histoFile, "recreate") self.snapshot = snapshot + self.keepVars = keepVars RDF = ROOT.ROOT.RDataFrame self.d = RDF("Events", inputFiles) @@ -52,13 +31,14 @@ def run(self): tmp_th2 = m.getTH2() for obj in tmp_th1: - self.objs.append(obj) + self.objs.append(ROOT.RDF.RResultPtr('TH1D')(obj)) for obj in tmp_th2: - self.objs.append(obj) + self.objs.append(ROOT.RDF.RResultPtr('TH2D')(obj)) if self.snapshot: + """ comment until we understand Snapshot bug ROOT.ROOT.RDataFrame("LuminosityBlocks", self.inputFiles).Snapshot("LuminosityBlocks",self.outputFile, "") print time.time()-t0, "first snapshot" @@ -77,7 +57,15 @@ def run(self): #ROOT.ROOT.RDataFrame("ParameterSets", self.inputFiles).Snapshot("ParameterSets", self.outputFile, "", opts) #print time.time()-t0, "fourth loop" - self.d.Snapshot("Events", self.outputFile) + print self.keepVars + if not self.keepVars == '': + print "qui" + Parser = parser(self.keepVars) + self.d.Snapshot("Events", self.outputFile, Parser.parse()) + else: + print "i'm snapshotting!" + self.d.Snapshot("Events", self.outputFile) + print time.time()-start, "events snapshot" self.histoFile.cd() diff --git a/framework/foo.py b/framework/foo.py new file mode 100644 index 0000000000000..aaedd87925173 --- /dev/null +++ b/framework/foo.py @@ -0,0 +1,20 @@ +import ROOT + +class parser: + + def __init__(self, file): + + self.file = file + + self.f = open(self.file) + + def parse(self): + + list = [x.strip() for x in self.f.readlines()] + branchList = ROOT.vector('string')() + + for l in list: + branchList.push_back(l) + + return branchList + diff --git a/framework/header.py b/framework/header.py new file mode 100644 index 0000000000000..21523e4a5b47a --- /dev/null +++ b/framework/header.py @@ -0,0 +1,26 @@ +# Built-in/Generic Imports +import os +import sys +import time +import ROOT +from foo import * + +# Begin code for casting + +# this injection can be replaced by properly having this in a header +# included in the interpreter at framework startup +ROOT.gInterpreter.Declare(''' +template +class NodeCaster { + public: + static ROOT::RDF::RNode Cast(T rdf) + { + return ROOT::RDF::RNode(rdf); + } +}; +''') + +def CastToRNode(node): + return ROOT.NodeCaster(node.__cppname__).Cast(node) + +# end code for casting \ No newline at end of file diff --git a/framework/parser.py b/framework/parser.py new file mode 100644 index 0000000000000..1975be09dd56a --- /dev/null +++ b/framework/parser.py @@ -0,0 +1,13 @@ +class parser: + + def __init__(self, file): + + self.file = file + + self.f = open(self.file) + + def parse(self): + + print [x.strip() for x in self.f.readlines()] + return [x.strip() for x in self.f.readlines()] + diff --git a/wmass/AngCoeffNominal/AngCoeff.cpp b/wmass/AngCoeffNominal/AngCoeff.cpp deleted file mode 100644 index 1aef128ac1aed..0000000000000 --- a/wmass/AngCoeffNominal/AngCoeff.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "AngCoeff.h" - -RNode AngCoeff::defineArmonics(RNode d){ - - // angular coefficients as defined in https://arxiv.org/pdf/1609.02536.pdf - - // A0 - auto d0 = d.Define("P0", [](float cos_theta, float phi) -> float{ return 1./2.*(1.-3.*cos_theta*cos_theta);}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A1 - auto d1 = d0.Define("P1", [](float cos_theta, float phi) -> float{ return (2.*cos_theta*sqrt(1.-cos_theta*cos_theta)*cos(phi));}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A2 - auto d2 = d1.Define("P2", [](float cos_theta, float phi) -> float{ return 1./2. *(1.-cos_theta*cos_theta)*cos(2.*phi);}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A3 - auto d3 = d2.Define("P3", [](float cos_theta, float phi) -> float{ return sqrt(1.-cos_theta*cos_theta)*cos(phi);}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A4 - auto d4 = d3.Define("P4", [](float cos_theta, float phi) -> float{ return cos_theta;}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A5 - auto d5 = d4.Define("P5", [](float cos_theta, float phi) -> float{ return (1.-cos_theta*cos_theta)*sin(2.*phi);}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A6 - auto d6 = d5.Define("P6", [](float cos_theta, float phi) -> float{ return (2.*cos_theta*sqrt(1.-cos_theta*cos_theta)*sin(phi));}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A7 - auto d7 = d6.Define("P7", [](float cos_theta, float phi) -> float{ return (sqrt(1.-cos_theta*cos_theta)*sin(phi));}, {"CStheta_preFSR", "CSphi_preFSR"}); - - auto d8 = d7.Define("PUL", [](float cos_theta, float phi) -> float{ return 1-cos_theta*cos_theta;}, {"CStheta_preFSR", "CSphi_preFSR"}); - - return d8; - -} - -RNode AngCoeff::defineArmonicsSqAndW(RNode d, std::string c){ - - auto sq = [](float a, float w)-> float{ return a*a*w;}; - - auto d2 = d.Define("P"+c+ "sq", sq, {"P"+c, "Generator_weight_norm"}).Define("P"+c+ "w", [](float p, float w){ return p*w;}, {"P"+c, "Generator_weight_norm"}); - - return d2; - - } - -RNode AngCoeff::doSomething(RNode d){ - - auto dw = d.Filter("GenPart_pdgId[GenPart_preFSRMuonIdx]<0").Histo1D("Wrap_preFSR"); - - _h1List.push_back(dw); - - return d; - -} - -std::vector> AngCoeff::getTH1(){ - - return _h1List; -} - -std::vector> AngCoeff::getTH2(){ - - return _h2List; -} - - - - - - - - \ No newline at end of file diff --git a/wmass/AngCoeffNominal/AngCoeff.so b/wmass/AngCoeffNominal/AngCoeff.so deleted file mode 100755 index e48450107f576..0000000000000 Binary files a/wmass/AngCoeffNominal/AngCoeff.so and /dev/null differ diff --git a/wmass/AngCoeffNominal/TemplateBuilder.cpp b/wmass/AngCoeffNominal/TemplateBuilder.cpp deleted file mode 100644 index 533ba7ed566fd..0000000000000 --- a/wmass/AngCoeffNominal/TemplateBuilder.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "TemplateBuilder.h" - - -RNode TemplateBuilder::doSomething(RNode d){ - - return d; - -} - -std::vector> TemplateBuilder::getTH1(){ - - return _h1List; - -} - -std::vector> TemplateBuilder::getTH2(){ - - return _h2List; - - -} diff --git a/wmass/AngCoeffNominal/TemplateBuilder.so b/wmass/AngCoeffNominal/TemplateBuilder.so deleted file mode 100755 index 55b83709e0df7..0000000000000 Binary files a/wmass/AngCoeffNominal/TemplateBuilder.so and /dev/null differ diff --git a/wmass/AngCoeffNominal/TemplateBuilderOK.cpp b/wmass/AngCoeffNominal/TemplateBuilderOK.cpp deleted file mode 100644 index 99c0a64813bc2..0000000000000 --- a/wmass/AngCoeffNominal/TemplateBuilderOK.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "TemplateBuilder.h" - - -RNode TemplateBuilder::doSomething(RNode d){ - - return d; - - /* - - TH2::SetDefaultSumw2(true); - - - float yArr[] = {-3.5, -3.0, -2.5, -2.0, -1.6, -1.2, -0.8, -0.4, 0, 0.4, 0.8, 1.2 ,1.6, 2.0, 2.5, 3.0, 3.5}; - float ptArr[] = {0., 4., 8., 12., 16., 20., 24., 32., 40., 60.}; - - int nBinsY = 16; - int nBinsPt = 9; - - std::vector coeff = {"0", "1", "2", "3", "4", "5", "6", "7", "UL"}; - std::vector> templList; - - auto getFromIdx = [](rvec_f v, int idx){ return v[idx];}; - auto getFromIdxStd = [](std::vector v, int idx){ return v[idx];}; - - auto d1 = d.Define("Mupt_preFSR", getFromIdx, {"GenPart_pt", "GenPart_preFSRMuonIdx"}).Define("Mueta_preFSR", getFromIdx, {"GenPart_eta", "GenPart_preFSRMuonIdx"}); - - - // fake histogram just to define filters - auto h = new TH2F("h", "h", nBinsY, yArr, nBinsPt, ptArr); - - for(int j=1; jGetNbinsY()+1; j++){ // for each pt bin - - float lowEdgePt = h->GetYaxis()->GetBinLowEdge(j+1); - float upEdgePt = h->GetYaxis()->GetBinUpEdge(j+1); - - for(int i=1; iGetNbinsX()+1; i++){ // for each y bin - - float lowEdgeY = h->GetXaxis()->GetBinLowEdge(i+1); - float upEdgeY = h->GetXaxis()->GetBinUpEdge(i+1); - - auto sel = [lowEdgePt, upEdgePt, lowEdgeY, upEdgeY](float pt, float rap) { return (pt >lowEdgePt && ptlowEdgeY && rapcd(); - - for(std::size_t i=0; iGetNbinsY()+1; j++){ // for each pt bin - for(int i=1; iGetNbinsX()+1; i++){ // for each y bin - - std::cout<< int(i/8)+8 << std::endl; - templList[i]->SetBinContent(i,j,templList[i]->GetBinContent(i,j)+templList[int(i/8)*8+8]->GetBinContent(i,j)); - templList[i]->SetBinError(i,j, sqrt(templList[i]->GetBinError(i,j)*templList[i]->GetBinError(i,j) + templList[int(i/8)+8]->GetBinError(i,j)*templList[int(i/8)+8]->GetBinError(i,j))); - } - - } - } - - templList[i]->Write(); - } - - return d; - - */ - -} - -std::vector> TemplateBuilder::getTH1(){ - - return _h1List; - -} - -std::vector> TemplateBuilder::getTH2(){ - - return _h2List; - - -} diff --git a/wmass/AngCoeffNominal/histo.root b/wmass/AngCoeffNominal/histo.root deleted file mode 100644 index 46fa251878459..0000000000000 Binary files a/wmass/AngCoeffNominal/histo.root and /dev/null differ diff --git a/wmass/AngCoeffNominal/templates.root b/wmass/AngCoeffNominal/templates.root deleted file mode 100644 index 9229dd94ce4a4..0000000000000 Binary files a/wmass/AngCoeffNominal/templates.root and /dev/null differ diff --git a/wmass/AngCoeffNominal/AngCoeffOK.cpp b/wmass/AngCoeffTempl/AngCoeff.cpp similarity index 82% rename from wmass/AngCoeffNominal/AngCoeffOK.cpp rename to wmass/AngCoeffTempl/AngCoeff.cpp index 6f6ffb9e28481..b2360e83e66c8 100644 --- a/wmass/AngCoeffNominal/AngCoeffOK.cpp +++ b/wmass/AngCoeffTempl/AngCoeff.cpp @@ -21,7 +21,7 @@ RNode AngCoeff::defineArmonics(RNode d){ // A7 auto d7 = d6.Define("P7", [](float cos_theta, float phi) -> float{ return (sqrt(1.-cos_theta*cos_theta)*sin(phi));}, {"CStheta_preFSR", "CSphi_preFSR"}); - auto d8 = d7.Define("PUL", [](float cos_theta, float phi) -> float{ return 1-cos_theta*cos_theta;}, {"CStheta_preFSR", "CSphi_preFSR"}); + auto d8 = d7.Define("PUL", [](float cos_theta, float phi) -> float{ return 1+cos_theta*cos_theta;}, {"CStheta_preFSR", "CSphi_preFSR"}); return d8; @@ -39,26 +39,17 @@ RNode AngCoeff::defineArmonicsSqAndW(RNode d, std::string c){ RNode AngCoeff::doSomething(RNode d){ - std::vector> test; - auto dw = d.Filter("GenPart_pdgId[GenPart_preFSRMuonIdx]<0").Histo1D("Wrap_preFSR"); - - _h1List.push_back(dw); - - return d; - - - /* // first normalise generator weights auto dw = d.Filter("GenPart_pdgId[GenPart_preFSRMuonIdx]<0").Define("Generator_weight_norm", [](float w)-> float{ return w/abs(w);}, {"Generator_weight"}); auto dArm = defineArmonics(dw); - float yArr[] = {-3.5, -3.0, -2.5, -2.0, -1.6, -1.2, -0.8, -0.4, 0, 0.4, 0.8, 1.2 ,1.6, 2.0, 2.5, 3.0, 3.5}; - float ptArr[] = {0., 4., 8., 12., 16., 20., 24., 32., 40., 60.}; + float yArr[] = {-6.0, -3.0, -2.5, -2.0, -1.6, -1.2, -0.8, -0.4, 0, 0.4, 0.8, 1.2 ,1.6, 2.0, 2.5, 3.0, 6.0}; + float ptArr[] = {0., 4., 8., 12., 16., 20., 24., 32., 40., 60., 100., 200.}; int nBinsY = 16; - int nBinsPt = 9; + int nBinsPt = 11; std::vector coeff = {"0", "1", "2", "3", "4", "5", "6", "7"}; @@ -121,14 +112,13 @@ RNode AngCoeff::doSomething(RNode d){ } - //_h2List.push_back(h2Num[h]); - test.push_back(h2Num[h]); + _h2List.push_back(h2Num[h]); } auto getNorm = [this](float Wpt, float Wrap, float P0, float P1, float P2, float P3, float P4, float P5, float P6, float P7, float PUL){ - float norm = 0.; + float norm = PUL; std::vector arm; arm.push_back(P0); @@ -139,9 +129,8 @@ RNode AngCoeff::doSomething(RNode d){ arm.push_back(P5); arm.push_back(P6); arm.push_back(P7); - arm.push_back(PUL); - + for(unsigned int i=0; i<_h2List.size(); i++){ //loop over angular coefficients int binPt = _h2List[i]->GetYaxis()->FindBin(Wpt); @@ -150,26 +139,22 @@ RNode AngCoeff::doSomething(RNode d){ norm+=_h2List[i]->GetBinContent(binY,binPt)*arm[i]; //sum Ai*Pi } - + return norm; }; - auto getWeight = [this](float norm, float P){ + auto getWeight = [this](float norm, float P, float gen){ - return P/norm; + return (P/norm)*gen; }; - auto dPw = dArm.Define("Pnorm", getNorm, {"Wpt_preFSR", "Wrap_preFSR", "P0", "P1", "P2", "P3", "P4", "P5", "P6", "P7", "PUL"}).Define("Pweight0", getWeight, {"Pnorm", "P0"}).Define("Pweight1", getWeight, {"Pnorm", "P1"}).Define("Pweight2", getWeight, {"Pnorm", "P2"}) - .Define("Pweight3", getWeight, {"Pnorm", "P3"}).Define("Pweight4", getWeight, {"Pnorm", "P4"}).Define("Pweight5", getWeight, {"Pnorm", "P5"}).Define("Pweight6", getWeight, {"Pnorm", "P6"}).Define("Pweight7", getWeight, {"Pnorm", "P7"}).Define("PweightUL", getWeight, {"Pnorm", "PUL"}); + auto dPw = dArm.Define("Pnorm", getNorm, {"Wpt_preFSR", "Wrap_preFSR", "P0", "P1", "P2", "P3", "P4", "P5", "P6", "P7", "PUL"}).Define("Pweight0", getWeight, {"Pnorm", "P0", "Generator_weight_norm"}).Define("Pweight1", getWeight, {"Pnorm", "P1", "Generator_weight_norm"}).Define("Pweight2", getWeight, {"Pnorm", "P2", "Generator_weight_norm"}) + .Define("Pweight3", getWeight, {"Pnorm", "P3", "Generator_weight_norm"}).Define("Pweight4", getWeight, {"Pnorm", "P4", "Generator_weight_norm"}).Define("Pweight5", getWeight, {"Pnorm", "P5", "Generator_weight_norm"}).Define("Pweight6", getWeight, {"Pnorm", "P6", "Generator_weight_norm"}).Define("Pweight7", getWeight, {"Pnorm", "P7", "Generator_weight_norm"}).Define("PweightUL", getWeight, {"Pnorm", "PUL", "Generator_weight_norm"}); - auto prova = dPw.Histo2D(TH2D("hdenom2", "hdenom2", nBinsY, yArr, nBinsPt, ptArr), "Wrap_preFSR", "Wpt_preFSR", "Generator_weight_norm"); - _h2List.push_back(prova); - - return dPw; - */ + } std::vector> AngCoeff::getTH1(){ diff --git a/wmass/AngCoeffNominal/AngCoeff.h b/wmass/AngCoeffTempl/AngCoeff.h similarity index 100% rename from wmass/AngCoeffNominal/AngCoeff.h rename to wmass/AngCoeffTempl/AngCoeff.h diff --git a/wmass/AngCoeffTempl/AngCoeff.so b/wmass/AngCoeffTempl/AngCoeff.so new file mode 100755 index 0000000000000..812619d11e4aa Binary files /dev/null and b/wmass/AngCoeffTempl/AngCoeff.so differ diff --git a/wmass/AngCoeffTempl/GetWeights.cpp b/wmass/AngCoeffTempl/GetWeights.cpp new file mode 100644 index 0000000000000..27d5615f2e23f --- /dev/null +++ b/wmass/AngCoeffTempl/GetWeights.cpp @@ -0,0 +1,41 @@ +#include "GetWeights.h" + + +RNode GetWeights::doSomething(RNode d){ + + float yArr[] = {-6.0, -3.0, -2.5, -2.0, -1.6, -1.2, -0.8, -0.4, 0, 0.4, 0.8, 1.2 ,1.6, 2.0, 2.5, 3.0, 6.0}; + float ptArr[] = {0., 4., 8., 12., 16., 20., 24., 32., 40., 60., 100., 200.}; + + int nBinsY = 16; + int nBinsPt = 11; + + auto h = d.Histo2D(TH2D("hnormy", "hnormy", 16, -6, 6, 100, -20, 20), "Wrap_preFSR", "Pnorm"); + auto h1 = d.Histo2D(TH2D("hnormpt", "hnormpt", 100, 0, 200, 100, -20, 20), "Wpt_preFSR", "Pnorm"); + + _h2List.push_back(h); + _h2List.push_back(h1); + + auto w = d.Histo1D("PweightUL"); + + _h1List.push_back(w); + + return d; +} + +std::vector> GetWeights::getTH1(){ + + return _h1List; +} + +std::vector> GetWeights::getTH2(){ + + return _h2List; +} + + + + + + + + \ No newline at end of file diff --git a/wmass/AngCoeffVariations/AngCoeff.h b/wmass/AngCoeffTempl/GetWeights.h similarity index 65% rename from wmass/AngCoeffVariations/AngCoeff.h rename to wmass/AngCoeffTempl/GetWeights.h index bc9f2006e944b..d09a330eaf5d7 100644 --- a/wmass/AngCoeffVariations/AngCoeff.h +++ b/wmass/AngCoeffTempl/GetWeights.h @@ -1,5 +1,5 @@ -#ifndef ANGCOEFF_H -#define ANGCOEFF_H +#ifndef GETWEIGHTS_H +#define GETWEIGHTS_H #include "ROOT/RDataFrame.hxx" @@ -9,7 +9,7 @@ #include "TH2D.h" #include "TString.h" #include "TMath.h" -#include "../framework/module.h" +#include "../../framework/module.h" using namespace ROOT::VecOps; using RNode = ROOT::RDF::RNode; @@ -17,7 +17,7 @@ using rvec_f = const RVec &; using rvec_i = const RVec &; -class AngCoeff : public Module { +class GetWeights : public Module { private: @@ -26,20 +26,12 @@ class AngCoeff : public Module { public: - AngCoeff() {}; - ~AngCoeff() {}; + GetWeights() {}; + ~GetWeights() {}; RNode doSomething(RNode) override; std::vector> getTH1() override; std::vector> getTH2() override; - // custom functions - RNode defineArmonics(RNode); - RNode defineArmonicsSqAndWscale(RNode, int, int); - RNode defineArmonicsSqAndWPDF(RNode, int, int); }; -#endif - - - - \ No newline at end of file +#endif \ No newline at end of file diff --git a/wmass/AngCoeffTempl/GetWeights.so b/wmass/AngCoeffTempl/GetWeights.so new file mode 100755 index 0000000000000..2b44822e0f2e7 Binary files /dev/null and b/wmass/AngCoeffTempl/GetWeights.so differ diff --git a/wmass/AngCoeffTempl/TemplateBuilder.cpp b/wmass/AngCoeffTempl/TemplateBuilder.cpp new file mode 100644 index 0000000000000..bd72a0664f8fa --- /dev/null +++ b/wmass/AngCoeffTempl/TemplateBuilder.cpp @@ -0,0 +1,94 @@ +#include "TemplateBuilder.h" + + +RNode TemplateBuilder::doSomething(RNode d){ + + + TH2::SetDefaultSumw2(true); + + float yArr[] = {-6.0, -3.0, -2.5, -2.0, -1.6, -1.2, -0.8, -0.4, 0, 0.4, 0.8, 1.2 ,1.6, 2.0, 2.5, 3.0, 6.0}; + float qtArr[] = {0., 4., 8., 12., 16., 20., 24., 32., 40., 60., 100., 200.}; + + const int nBinsY = 16; + const int nBinsQt = 11; + + const int nBinsEta = 100; + const int nBinsPt = 80; + + float etaArr[nBinsEta+1] = {0}; + float ptArr[nBinsPt+1] = {0}; + + for(int i=0; i coeff = {"0", "1", "2", "3", "4", "5", "6", "7", "UL"}; + std::vector> templList; + + auto getFromIdx = [](rvec_f v, int idx){ return v[idx];}; + + auto d1 = d.Define("Mupt_preFSR", getFromIdx, {"GenPart_pt", "GenPart_preFSRMuonIdx"}).Define("Mueta_preFSR", getFromIdx, {"GenPart_eta", "GenPart_preFSRMuonIdx"}); + + + // fake histogram just to define filters + auto h = new TH2F("h", "h", nBinsY, yArr, nBinsQt, qtArr); + + for(int j=1; jGetNbinsY()+1; j++){ // for each W pt bin + + float lowEdgePt = h->GetYaxis()->GetBinLowEdge(j); + float upEdgePt = h->GetYaxis()->GetBinUpEdge(j); + + + auto sel = [lowEdgePt, upEdgePt](float pt) { return (pt >lowEdgePt && ptcd(); + + for(std::size_t i=0; iWrite(); + + } + +return d; + + +} + +std::vector> TemplateBuilder::getTH1(){ + + return _h1List; + +} + +std::vector> TemplateBuilder::getTH2(){ + + return _h2List; + + +} \ No newline at end of file diff --git a/wmass/AngCoeffNominal/TemplateBuilder.h b/wmass/AngCoeffTempl/TemplateBuilder.h similarity index 100% rename from wmass/AngCoeffNominal/TemplateBuilder.h rename to wmass/AngCoeffTempl/TemplateBuilder.h diff --git a/wmass/AngCoeffTempl/TemplateBuilder.py b/wmass/AngCoeffTempl/TemplateBuilder.py new file mode 100644 index 0000000000000..f664377d214db --- /dev/null +++ b/wmass/AngCoeffTempl/TemplateBuilder.py @@ -0,0 +1,78 @@ +import ROOT +from array import array + +class TemplateBuilder: + def __init__(self): + pass + + self.myTH1 = [] + self.myTH2 = [] + + def doSomething(self,d): + + yArr = [-6.0, -3.0, -2.5, -2.0, -1.6, -1.2, -0.8, -0.4, 0, 0.4, 0.8, 1.2 ,1.6, 2.0, 2.5, 3.0, 6.0] + ptArr = [0., 4., 8., 12., 16., 20., 24., 32., 40., 60., 100., 200.] + + getVector_code =''' + + float getFromIdx(rvec_f v, int idx){ return v[idx];} + + ''' + + ROOT.gInterpreter.Declare(getVector_code) + + d1 = d.Define("Mupt_preFSR", "getFromIdx(GenPart_pt, GenPart_preFSRMuonIdx)").Define("Mueta_preFSR", "getFromIdx(GenPart_eta, GenPart_preFSRMuonIdx)") + + ROOT.TH2.SetDefaultSumw2(True) + + coeff = ["0", "1", "2", "3", "4", "5", "6", "7", "UL"] + templList = [] + + # fake histogram just to define filters + + h = ROOT.TH2F("h", "h", len(yArr)-1, array('f',yArr), len(ptArr)-1, array('f',ptArr)) + + sel_code = ''' + + ROOT::RDF::RResultPtr sel(ROOT::RDF::RNode df, float lowEdgePt, float upEdgePt, std::string c){ + + auto sel(float Wpt_preFSR) { return (Wpt_preFSR >lowEdgePt && Wpt_preFSR < upEdgePt);}; + + return d1.Filter(sel, {"Wpt_preFSR"}).Histo3D(TH3D(Form("A%s_pt%.2f_to_%.2f", c.c_str(), lowEdgePt, upEdgePt), Form("A%s_pt%.2f_to_%.2f", c.c_str(), lowEdgePt, upEdgePt), 50, -2.5, 2.5, 40, 20, 60, 16, -3.5, 3.5), "Mueta_preFSR" ,"Mupt_preFSR", "Wrap_preFSR", "Pweight"+c); + + } + + ''' + + for i in range(1, h.GetNbinsY()+1): #for each pt bin + + lowEdgePt = h.GetYaxis().GetBinLowEdge(i+1) + upEdgePt = h.GetYaxis().GetBinUpEdge(i+1); + + for c in coeff: + + templList.append(sel(CastToRNode(d1), lowEdgePt, upEdgePt, c)) + + + ftempl = ROOT.TFile("templates.root", "recreate") + + ftempl.cd() + + for h in templList: + + h.Write(); + + + def getTH1(self): + + return self.myTH1 + + def getTH2(self): + + return self.myTH2 + + + + + return d; + diff --git a/wmass/AngCoeffTempl/TemplateBuilder.so b/wmass/AngCoeffTempl/TemplateBuilder.so new file mode 100755 index 0000000000000..f5daa564d611c Binary files /dev/null and b/wmass/AngCoeffTempl/TemplateBuilder.so differ diff --git a/wmass/AngCoeffTempl/TemplateBuilderPy.py b/wmass/AngCoeffTempl/TemplateBuilderPy.py new file mode 100644 index 0000000000000..299ca0d736909 --- /dev/null +++ b/wmass/AngCoeffTempl/TemplateBuilderPy.py @@ -0,0 +1,91 @@ +import ROOT +from array import array + +import sys +sys.path.append('../../framework') + +from header import * +import numpy as np + +class TemplateBuilder: + def __init__(self): + pass + + self.myTH1 = [] + self.myTH2 = [] + + def doSomething(self,d): + + yArr = [-6.0, -3.0, -2.5, -2.0, -1.6, -1.2, -0.8, -0.4, 0, 0.4, 0.8, 1.2 ,1.6, 2.0, 2.5, 3.0, 6.0] + ptArr = [0., 4., 8., 12., 16., 20., 24., 32., 40., 60., 100., 200.] + + getVector_code =''' + + float getFromIdx(rvec_f v, int idx){ return v[idx];} + + ''' + + ROOT.gInterpreter.Declare(getVector_code) + + d1 = d.Define("Mupt_preFSR", "getFromIdx(GenPart_pt, GenPart_preFSRMuonIdx)").Define("Mueta_preFSR", "getFromIdx(GenPart_eta, GenPart_preFSRMuonIdx)") + + ROOT.TH3.SetDefaultSumw2(True) + + coeff = ["0", "1", "2", "3", "4", "5", "6", "7", "UL"] + templList = [] + + # fake histogram just to define filters + + h = ROOT.TH2F("h", "h", len(yArr)-1, array('f',yArr), len(ptArr)-1, array('f',ptArr)) + + sel_code = ''' + + ROOT::RDF::RNode sel(ROOT::RDF::RNode df, float lowEdgePt, float upEdgePt, std::string c){ + + auto sel = [=](float Wpt_preFSR) { return (Wpt_preFSR >lowEdgePt && Wpt_preFSR < upEdgePt);}; + + return df.Filter(sel, {"Wpt_preFSR"}); + + } + + ''' + + ROOT.gInterpreter.Declare(sel_code) + + ptBins = np.linspace( 25.0, 65.0, 81 ) + etaBins = np.linspace( -2.5, 2.5, 101 ) + + for i in range(1, h.GetNbinsY()+1): #for each pt bin + + lowEdgePt = h.GetYaxis().GetBinLowEdge(i) + upEdgePt = h.GetYaxis().GetBinUpEdge(i); + + for c in coeff: + + dtmp = ROOT.sel(CastToRNode(d1), lowEdgePt, upEdgePt, c) + htmp = dtmp.Histo3D(("A{}_pt_{:0.1f}_to_{:0.1f}".format(c, lowEdgePt, upEdgePt), "A{}_pt_{:0.1f}_to_{:0.1f}".format(c, lowEdgePt, upEdgePt), len(etaBins)-1, array('f',etaBins), len(ptBins)-1, array('f',ptBins), len(yArr)-1, array('f', yArr)), "Mueta_preFSR" ,"Mupt_preFSR", "Wrap_preFSR", "Pweight"+"{c}".format(c=c)) + + templList.append(htmp) + + ftempl = ROOT.TFile("templates.root", "recreate") + + ftempl.cd() + + for h in templList: + + h.Write() + + + return d1 + + + def getTH1(self): + + return self.myTH1 + + def getTH2(self): + + return self.myTH2 + + + diff --git a/wmass/AngCoeffTempl/TemplateProj.py b/wmass/AngCoeffTempl/TemplateProj.py new file mode 100644 index 0000000000000..64e5d5440932b --- /dev/null +++ b/wmass/AngCoeffTempl/TemplateProj.py @@ -0,0 +1,71 @@ +import ROOT +from array import array + +import sys +sys.path.append('../../framework') + +from header import * + +class TemplateProj: + def __init__(self): + pass + + self.myTH1 = [] + self.myTH2 = [] + + def doSomething(self,d): + + ROOT.TH3.SetDefaultSumw2(True) + ROOT.gROOT.SetBatch() + + ROOT.gStyle.SetPalette(ROOT.kRainBow) + + self.fIn = ROOT.TFile.Open("templates.root") + + for key in self.fIn.GetListOfKeys(): + + th3=ROOT.TH3D + + th3 = self.fIn.Get(key.GetName()) + + name = key.GetName() + + if not 'UL' in name: + + nameUL = name.replace(name.split('_')[0], 'AUL') + + th3UL = self.fIn.Get(nameUL) + + th3.Add(th3, th3UL) + + for i in range(1, th3.GetNbinsZ()+1): #for each y bin + + lowEdgeY = th3.GetZaxis().GetBinLowEdge(i) + upEdgeY = th3.GetZaxis().GetBinUpEdge(i); + + th3.GetZaxis().SetRange(i, i); + + proj = th3.Project3D("_y_{:0.1f}_to_{:0.1f}_yxe".format(lowEdgeY, upEdgeY)) + + + c = ROOT.TCanvas(proj.GetName()) + c.cd() + + proj.Draw("colz") + + c.SaveAs('./Templ_png/{c}.png'.format(c=proj.GetName())) + + + return d + + + def getTH1(self): + + return self.myTH1 + + def getTH2(self): + + return self.myTH2 + + + diff --git a/wmass/AngCoeffTempl/keepVars.txt b/wmass/AngCoeffTempl/keepVars.txt new file mode 100644 index 0000000000000..0d63cfd8ab9c6 --- /dev/null +++ b/wmass/AngCoeffTempl/keepVars.txt @@ -0,0 +1 @@ +GenPart_.* diff --git a/wmass/AngCoeffNominal/module.cpp b/wmass/AngCoeffTempl/module.cpp similarity index 100% rename from wmass/AngCoeffNominal/module.cpp rename to wmass/AngCoeffTempl/module.cpp diff --git a/wmass/AngCoeffNominal/module.h b/wmass/AngCoeffTempl/module.h similarity index 100% rename from wmass/AngCoeffNominal/module.h rename to wmass/AngCoeffTempl/module.h diff --git a/wmass/AngCoeffNominal/test.py b/wmass/AngCoeffTempl/test.py similarity index 77% rename from wmass/AngCoeffNominal/test.py rename to wmass/AngCoeffTempl/test.py index 1c706052489d1..493da30698156 100644 --- a/wmass/AngCoeffNominal/test.py +++ b/wmass/AngCoeffTempl/test.py @@ -4,13 +4,20 @@ os.system("g++ -fPIC -Wall -O3 ../../framework/module.cpp AngCoeff.cpp $(root-config --libs --cflags) -shared -o AngCoeff.so") os.system("g++ -fPIC -Wall -O3 ../../framework/module.cpp TemplateBuilder.cpp $(root-config --libs --cflags) -shared -o TemplateBuilder.so") +#os.system("g++ -fPIC -Wall -O3 ../../framework/module.cpp GetWeights.cpp $(root-config --libs --cflags) -shared -o GetWeights.so") ROOT.gInterpreter.Declare('#include "../../framework/module.h"') ROOT.gInterpreter.Declare('#include "AngCoeff.h"') ROOT.gInterpreter.Declare('#include "TemplateBuilder.h"') +#ROOT.gInterpreter.Declare('#include "GetWeights.h"') + ROOT.gSystem.Load('AngCoeff.so') ROOT.gSystem.Load('TemplateBuilder.so') +#ROOT.gSystem.Load('GetWeights.so') + +#from TemplateBuilderPy import * +from TemplateProj import * sys.path.append('../../framework') @@ -18,7 +25,7 @@ ROOT.ROOT.EnableImplicitMT(20) -p = RDFprocessor(outputFile = "test.root", inputFiles ='/scratch/emanca/WMass/NanoDevelopment/CMSSW_10_2_6/src/PhysicsTools/NanoAODTools/scripts/inputTree.root', modules=[ROOT.AngCoeff(), ROOT.TemplateBuilder()], histoFile = 'histo.root', snapshot=False) +p = RDFprocessor(outputFile = "test.root", inputFiles ='/scratch/emanca/WMass/RDFprocessor/wmass/data/signalTree*.root', modules=[TemplateProj()], histoFile = 'histo.root', snapshot=False) time = p.run() print "elapsed world time", time, "s" diff --git a/wmass/AngCoeffVariations/AngCoeff.cpp b/wmass/AngCoeffVariations/AngCoeff.cpp deleted file mode 100644 index e7064beb328a9..0000000000000 --- a/wmass/AngCoeffVariations/AngCoeff.cpp +++ /dev/null @@ -1,197 +0,0 @@ -#include "AngCoeff.h" - -RNode AngCoeff::defineArmonics(RNode d){ - - // angular coefficients as defined in https://arxiv.org/pdf/1609.02536.pdf - - // A0 - auto d0 = d.Define("P0", [](float cos_theta, float phi) -> float{ return 1./2.*(1.-3.*cos_theta*cos_theta);}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A1 - auto d1 = d0.Define("P1", [](float cos_theta, float phi) -> float{ return (2.*cos_theta*sqrt(1.-cos_theta*cos_theta)*cos(phi));}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A2 - auto d2 = d1.Define("P2", [](float cos_theta, float phi) -> float{ return 1./2. *(1.-cos_theta*cos_theta)*cos(2.*phi);}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A3 - auto d3 = d2.Define("P3", [](float cos_theta, float phi) -> float{ return sqrt(1.-cos_theta*cos_theta)*cos(phi);}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A4 - auto d4 = d3.Define("P4", [](float cos_theta, float phi) -> float{ return cos_theta;}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A5 - auto d5 = d4.Define("P5", [](float cos_theta, float phi) -> float{ return (1.-cos_theta*cos_theta)*sin(2.*phi);}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A6 - auto d6 = d5.Define("P6", [](float cos_theta, float phi) -> float{ return (2.*cos_theta*sqrt(1.-cos_theta*cos_theta)*sin(phi));}, {"CStheta_preFSR", "CSphi_preFSR"}); - // A7 - auto d7 = d6.Define("P7", [](float cos_theta, float phi) -> float{ return (sqrt(1.-cos_theta*cos_theta)*sin(phi));}, {"CStheta_preFSR", "CSphi_preFSR"}); - - return d7; - -} - -RNode AngCoeff::defineArmonicsSqAndWscale(RNode d, int c, int k){ - - auto sq = [k](float a, float w, rvec_f wscale)-> float{ return a*a*w*wscale[k];}; - - auto d2 = d.Define("P"+std::to_string(c)+ "sq"+ "_scale" +std::to_string(k), sq, {"P"+std::to_string(c), "Generator_weight_norm", "LHEScaleWeight"}).Define("P"+std::to_string(c)+ "w"+"_scale" +std::to_string(k), [k](float p, float w, rvec_f wscale){ return p*w*wscale[k];}, {"P"+std::to_string(c), "Generator_weight_norm", "LHEScaleWeight"}); - - return d2; - -} - -RNode AngCoeff::defineArmonicsSqAndWPDF(RNode d, int c, int k){ - - auto sq = [k](float a, float w, rvec_f wscale)-> float{ return a*a*w*wscale[k];}; - - auto d2 = d.Define("P"+std::to_string(c)+ "sq"+ "_PDF"+ std::to_string(k), sq, {"P"+std::to_string(c), "Generator_weight_norm", "LHEPdfWeight"}).Define("P"+std::to_string(c)+ "w"+ "_PDF" + std::to_string(k), [k](float p, float w, rvec_f wscale){ return p*w*wscale[k];}, {"P"+std::to_string(c), "Generator_weight_norm", "LHEPdfWeight"}); - - return d2; - -} - -RNode AngCoeff::doSomething(RNode d){ - - - // first normalise generator weights - - auto dw = d.Filter("GenPart_pdgId[GenPart_preFSRMuonIdx]<0").Define("Generator_weight_norm", [](float w)-> float{ return w/abs(w);}, {"Generator_weight"}); - - auto dArm = defineArmonics(dw); - - float yArr[] = {-3.5, -3.0, -2.5, -2.0, -1.6, -1.2, -0.8, -0.4, 0, 0.4, 0.8, 1.2 ,1.6, 2.0, 2.5, 3.0, 3.5}; - float ptArr[] = {0., 4., 8., 12., 16., 20., 24., 32., 40., 60.}; - - int nBinsY = 16; - int nBinsPt = 9; - - std::vector coeff = {0, 1, 2, 3, 4, 5, 6, 7}; - - std::map>> h2Num; - std::map> h2Den; - std::map>> h2Num2; - - std::vector> temp; - std::vector> temp2; - - - for(unsigned int k=0; k<111; k++){ // loop on QCD scale and PDF variations (8 + 102 variations) - - if(k<9){ - auto hDenominator = dArm.Define("w_scale"+std::to_string(k), [k](float w, rvec_f wscale){ return w*wscale[k];}, {"Generator_weight_norm", "LHEScaleWeight"}).Histo2D(TH2D("hdenom", "hdenom", nBinsY, yArr, nBinsPt, ptArr), "Wrap_preFSR", "Wpt_preFSR", "w_scale"+std::to_string(k)); - h2Den.insert(std::pair>(k, hDenominator)); - } - else{ - auto d = k-9; - auto hDenominator = dArm.Define("w_PDF"+std::to_string(k-9), [d](float w, rvec_f wscale){ return w*wscale[d];}, {"Generator_weight_norm", "LHEPdfWeight"}).Histo2D(TH2D("hdenom", "hdenom", nBinsY, yArr, nBinsPt, ptArr), "Wrap_preFSR", "Wpt_preFSR", "w_PDF"+std::to_string(k-9)); - h2Den.insert(std::pair>(k, hDenominator)); - } - - for(auto c:coeff){ - - if(k<9){ - - auto dArm2 = defineArmonicsSqAndWscale(dArm, c, k); // function depends on coefficient and scale index - auto hNumerator = dArm2.Histo2D(TH2D(Form("A%i_scaleVar%i", c,k), Form("A%i_scaleVar%i", c,k), nBinsY, yArr, nBinsPt, ptArr), "Wrap_preFSR", "Wpt_preFSR", "P"+std::to_string(c)+ "w"+"_scale" +std::to_string(k)); - auto hP2 = dArm2.Histo2D(TH2D(Form("A%i_scaleVar%i", c,k), Form("A%i_scaleVar%i", c,k), nBinsY, yArr, nBinsPt, ptArr), "Wrap_preFSR", "Wpt_preFSR", "P"+std::to_string(c)+ "sq"+ "_scale" +std::to_string(k)); - - temp.push_back(hNumerator); - temp2.push_back(hP2); - - } - else{ - - auto dArm2 = defineArmonicsSqAndWPDF(dArm, c, k-9); // function depends on coefficient and scale index - auto hNumerator = dArm2.Histo2D(TH2D(Form("A%i_PDFVar%i", c,k-9), Form("A%i_PDFVar%i", c,k-9), nBinsY, yArr, nBinsPt, ptArr), "Wrap_preFSR", "Wpt_preFSR", "P"+std::to_string(c)+ "w"+"_PDF" +std::to_string(k-9)); - auto hP2 = dArm2.Histo2D(TH2D(Form("A%i_PDFVar%i", c,k-9), Form("A%i_PDFVar%i", c,k-9), nBinsY, yArr, nBinsPt, ptArr), "Wrap_preFSR", "Wpt_preFSR", "P"+std::to_string(c)+ "sq"+ "_PDF" +std::to_string(k-9)); - - temp.push_back(hNumerator); - temp2.push_back(hP2); - } - - } - - h2Num.insert(std::pair>>(k, temp)); // all histos with variation k - h2Num2.insert(std::pair>>(k, temp2)); - - temp.resize(0); - temp2.resize(0); - -} - -// now iterate over maps - -auto it_h2Num = h2Num.begin(); -auto it_h2Den = h2Den.begin(); -auto it_h2Num2 = h2Num2.begin(); - -while (it_h2Num != h2Num.end()) -{ - - for(std::size_t h=0; h < it_h2Num->second.size(); h++){ - - - for(int j=1; jsecond.at(h)->GetNbinsY()+1; j++){ // for each pt bin - - for(int i=1; isecond.at(h)->GetNbinsX()+1; i++){ // for each y bin - - it_h2Num->second.at(h)->SetBinContent(i,j, it_h2Num->second.at(h)->GetBinContent(i,j)/it_h2Den->second->GetBinContent(i,j)); - - float stdErr2 = it_h2Num2->second.at(h)->GetBinContent(i,j)/it_h2Den->second->GetBinContent(i,j) - it_h2Num->second.at(h)->GetBinContent(i,j)*it_h2Num->second.at(h)->GetBinContent(i,j); - float sqrtneff = it_h2Den->second->GetBinContent(i,j)/it_h2Den->second->GetBinError(i,j); - float coeff_err = TMath::Sqrt(stdErr2)/(sqrtneff); - - it_h2Num->second.at(h)->SetBinError(i,j, coeff_err); - - // rescale with the right coefficient - - float cont = it_h2Num->second.at(h)->GetBinContent(i,j); - float err = it_h2Num->second.at(h)->GetBinError(i,j); - - if(h == 0){ - it_h2Num->second.at(h)->SetBinContent(i,j, 20./3.*cont + 2./3.); - it_h2Num->second.at(h)->SetBinError(i,j, 20./3.*err); - } - else if(h == 1 || h == 5 || h == 6){ - it_h2Num->second.at(h)->SetBinContent(i,j, 5*cont); - it_h2Num->second.at(h)->SetBinError(i,j, 5*err); - } - else if(h == 2){ - it_h2Num->second.at(h)->SetBinContent(i,j, 10*cont); - it_h2Num->second.at(h)->SetBinError(i,j, 10*err); - } - else{ - it_h2Num->second.at(h)->SetBinContent(i,j, 4*cont); - it_h2Num->second.at(h)->SetBinError(i,j, 4*err); - } - - } - } - - - _h2List.push_back(it_h2Num->second.at(h)); - - } - - ++it_h2Den; - ++it_h2Num; - ++it_h2Num2; - -} - - -return d; - -} - -std::vector> AngCoeff::getTH1(){ - - return _h1List; -} - -std::vector> AngCoeff::getTH2(){ - - return _h2List; -} - - - - - - - diff --git a/wmass/AngCoeffVariations/AngCoeff.so b/wmass/AngCoeffVariations/AngCoeff.so deleted file mode 100755 index f00b3e7481507..0000000000000 Binary files a/wmass/AngCoeffVariations/AngCoeff.so and /dev/null differ diff --git a/wmass/AngCoeffVariations/histo.root b/wmass/AngCoeffVariations/histo.root deleted file mode 100644 index a088d9f5fb517..0000000000000 Binary files a/wmass/AngCoeffVariations/histo.root and /dev/null differ