Skip to content

Commit 2dd8ec3

Browse files
committed
Add wmass analysis from Elisabetta
1 parent b6a4db3 commit 2dd8ec3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2751
-0
lines changed

root/tree/dataframe/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ if(rootbench-datafiles)
4141
endif()
4242

4343
add_subdirectory(LoopSUSYFrame)
44+
add_subdirectory(wmass)
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
if(rootbench-datafiles AND ROOT_root7_FOUND) # using ROOT_root7_FOUND as a proxy for "c++ standard >= 14"
2+
ROOT_GENERATE_DICTIONARY(G__SignalAnalysis inc/classes.h
3+
MODULE SignalAnalysis
4+
LINKDEF LinkDef.h)
5+
6+
file(COPY RDFprocessor/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR} FILES_MATCHING PATTERN "*.py")
7+
file(COPY python/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR} FILES_MATCHING PATTERN "*.py")
8+
9+
add_library(SignalAnalysis SHARED
10+
src/TH3weightsHelper.cpp
11+
src/TH2weightsHelper.cpp
12+
src/TH1weightsHelper.cpp
13+
src/getAccMap.cpp
14+
src/getWeights.cpp
15+
src/dataObs.cpp
16+
src/defineSystWeight.cpp
17+
src/templateBuilder.cpp
18+
src/defineHarmonics.cpp
19+
src/module.cpp
20+
src/AngCoeff.cpp
21+
src/getACValues.cpp
22+
G__SignalAnalysis.cxx)
23+
add_dependencies(SignalAnalysis G__SignalAnalysis)
24+
target_include_directories(SignalAnalysis PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
25+
26+
RB_ADD_PYTESTBENCHMARK(wmassAnalysis
27+
runSignalAnalysis.py
28+
LABEL long
29+
DOWNLOAD_DATAFILES wmass.root)
30+
endif()

root/tree/dataframe/wmass/LinkDef.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifdef __CLING__
2+
3+
#pragma link C++ class AngCoeff;
4+
#pragma link C++ class dataObs;
5+
#pragma link C++ class defineHarmonics;
6+
#pragma link C++ class defineSystWeight;
7+
#pragma link C++ class getACValues;
8+
#pragma link C++ class getAccMap;
9+
#pragma link C++ class getWeights;
10+
#pragma link C++ class templateBuilder;
11+
12+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
from header import *
2+
import copy
3+
4+
class RDFtree:
5+
def __init__(self, outputDir, outputFile, inputFile,treeName='Events', pretend=False):
6+
7+
self.outputDir = outputDir # output directory
8+
self.outputFile = outputFile
9+
self.inputFile = inputFile
10+
11+
self.treeName = treeName
12+
13+
RDF = ROOT.ROOT.RDataFrame
14+
15+
self.pretend = pretend
16+
if self.pretend:
17+
18+
ROOT.ROOT.DisableImplicitMT()
19+
self.d = RDF(self.treeName, self.inputFile)
20+
self.d=self.d.Range(10)
21+
else:
22+
23+
self.d = RDF(self.treeName, self.inputFile)
24+
25+
self.entries = self.d.Count() #stores lazily the number of events
26+
27+
self.modules = []
28+
29+
self.objs = {} # objects to be received from modules
30+
31+
self.node = {} # dictionary branchName - RDF
32+
self.node['input'] = self.d # assign input RDF to a branch called 'input'
33+
34+
self.graph = {} # save the graph to write it in the end
35+
36+
if not os.path.exists(self.outputDir):
37+
os.system("mkdir -p " + self.outputDir)
38+
39+
prevdir = os.getcwd()
40+
os.chdir(self.outputDir)
41+
42+
self.fout = ROOT.TFile(self.outputFile, "recreate")
43+
self.fout.Close()
44+
45+
os.chdir(prevdir)
46+
47+
def branch(self,nodeToStart, nodeToEnd, modules=[]):
48+
49+
self.branchDir = nodeToEnd
50+
self.objs[self.branchDir] = []
51+
52+
if nodeToStart in self.graph:
53+
self.graph[nodeToStart].append(nodeToEnd)
54+
else:
55+
self.graph[nodeToStart]=[nodeToEnd]
56+
57+
branchRDF = self.node[nodeToStart]
58+
59+
lenght = len(self.modules)
60+
61+
self.modules.extend(modules)
62+
63+
# modify RDF according to modules
64+
65+
for i, m in enumerate(self.modules[lenght:]):
66+
67+
branchRDF = m.run(ROOT.RDF.AsRNode(branchRDF))
68+
69+
70+
tmp_th1 = m.getTH1()
71+
tmp_th2 = m.getTH2()
72+
tmp_th3 = m.getTH3()
73+
74+
tmp_th1G = m.getGroupTH1()
75+
tmp_th2G = m.getGroupTH2()
76+
tmp_th3G = m.getGroupTH3()
77+
78+
79+
for obj in tmp_th1:
80+
81+
value_type = getValueType(obj)
82+
83+
self.objs[self.branchDir].append(ROOT.RDF.RResultPtr(value_type)(obj))
84+
85+
for obj in tmp_th2:
86+
87+
value_type = getValueType(obj)
88+
89+
self.objs[self.branchDir].append(ROOT.RDF.RResultPtr(value_type)(obj))
90+
91+
for obj in tmp_th3:
92+
93+
value_type = getValueType(obj)
94+
95+
self.objs[self.branchDir].append(ROOT.RDF.RResultPtr(value_type)(obj))
96+
97+
for obj in tmp_th1G:
98+
99+
value_type = getValueType(obj)
100+
101+
self.objs[self.branchDir].append(ROOT.RDF.RResultPtr(value_type)(obj))
102+
103+
for obj in tmp_th2G:
104+
105+
value_type = getValueType(obj)
106+
107+
self.objs[self.branchDir].append(ROOT.RDF.RResultPtr(value_type)(obj))
108+
109+
for obj in tmp_th3G:
110+
111+
value_type = getValueType(obj)
112+
113+
self.objs[self.branchDir].append(ROOT.RDF.RResultPtr(value_type)(obj))
114+
115+
116+
m.reset()
117+
118+
self.node[nodeToEnd] = branchRDF
119+
120+
121+
def takeSnapshot(self, node, blist=[]):
122+
123+
opts = ROOT.ROOT.RDF.RSnapshotOptions()
124+
opts.fLazy = True
125+
126+
branchList = ROOT.vector('string')()
127+
128+
for l in blist:
129+
branchList.push_back(l)
130+
131+
print time.time()-self.start, "before snapshot"
132+
133+
if not len(blist)==0:
134+
out = self.node[node].Snapshot(self.treeName,self.outputFile, branchList, opts)
135+
else:
136+
out = self.node[node].Snapshot(self.treeName,self.outputFile, "", opts)
137+
138+
self.objs[self.branchDir].append(out)
139+
140+
141+
def getOutput(self):
142+
143+
#start analysis
144+
self.start = time.time()
145+
146+
# now write all the outputs together
147+
148+
print "Writing output files in "+ self.outputDir
149+
150+
os.chdir(self.outputDir)
151+
self.fout = ROOT.TFile(self.outputFile, "update")
152+
self.fout.cd()
153+
154+
obj_number = 0
155+
156+
for branchDir, objs in self.objs.iteritems():
157+
158+
if not self.fout.GetDirectory(branchDir): self.fout.mkdir(branchDir)
159+
160+
self.fout.cd(branchDir)
161+
for obj in objs:
162+
163+
if not 'TH' in type(obj).__cpp_name__:
164+
obj.GetValue()
165+
166+
elif 'vector' in type(obj).__cpp_name__:
167+
168+
print "writing group of histos "
169+
170+
for h in obj:
171+
obj_number = obj_number+1
172+
173+
h.Write()
174+
else:
175+
obj_number = obj_number+1
176+
obj.Write()
177+
178+
179+
#self.objs = {} # re-initialise object list
180+
self.fout.Close()
181+
os.chdir("..")
182+
183+
print self.entries.GetValue(), "events processed in "+"{:0.1f}".format(time.time()-self.start), "s", "rate", self.entries.GetValue()/(time.time()-self.start), "histograms written: ", obj_number
184+
185+
def getObjects(self, node):
186+
187+
return self.objs[node]
188+
189+
def saveGraph(self):
190+
191+
print self.graph
192+
193+
from graphviz import Digraph
194+
195+
dot = Digraph(name='my analysis', filename = 'graph.pdf')
196+
197+
for node, nodelist in self.graph.iteritems():
198+
199+
dot.node(node, node)
200+
for n in nodelist:
201+
dot.node(n, n)
202+
dot.edge(node,n)
203+
204+
205+
print(dot.source)
206+
207+
dot.render(view=True)
208+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "TH1weightsHelper.h"
2+
/// This constructor takes all the parameters necessary to build the THnTs. In addition, it requires the names of
3+
/// the columns which will be used.
4+
TH1weightsHelper::TH1weightsHelper(std::string name, std::string title,
5+
int nbinsX, std::vector<float> xbins,
6+
std::vector<std::string> weightNames
7+
)
8+
{
9+
_name = name;
10+
_nbinsX = nbinsX;
11+
_xbins = xbins;
12+
_weightNames = weightNames;
13+
14+
const auto nSlots = ROOT::IsImplicitMTEnabled() ? ROOT::GetImplicitMTPoolSize() : 1;
15+
for (auto slot : ROOT::TSeqU(nSlots)) {
16+
fHistos.emplace_back(std::make_shared<std::vector<TH1D>>());
17+
(void)slot;
18+
19+
std::vector<TH1D>& histos = *fHistos[slot];
20+
auto n_histos = _weightNames.size();
21+
22+
for (unsigned int i = 0; i < n_histos; ++i){
23+
24+
histos.emplace_back(TH1D(std::string(_name+_weightNames[i]).c_str(),
25+
std::string(_name+_weightNames[i]).c_str(),
26+
_nbinsX, _xbins.data()));
27+
28+
histos.back().SetDirectory(nullptr);
29+
}
30+
31+
}
32+
}
33+
34+
std::shared_ptr<std::vector<TH1D>> TH1weightsHelper::GetResultPtr() const { return fHistos[0]; }
35+
void TH1weightsHelper::Initialize() {}
36+
void TH1weightsHelper::InitTask(TTreeReader *, unsigned int) {}
37+
/// This is a method executed at every entry
38+
39+
void TH1weightsHelper::Exec(unsigned int slot, const float &var1, const ROOT::VecOps::RVec<float> &weights)
40+
{
41+
auto& histos = *fHistos[slot];
42+
const auto n_histos = histos.size();
43+
for (unsigned int i = 0; i < n_histos; ++i)
44+
histos[i].Fill(var1, weights[i]);
45+
}
46+
/// This method is called at the end of the event loop. It is used to merge all the internal THnTs which
47+
/// were used in each of the data processing slots.
48+
void TH1weightsHelper::Finalize()
49+
{
50+
auto &res_vec = *fHistos[0];
51+
for (auto slot : ROOT::TSeqU(1, fHistos.size())) {
52+
auto& histo_vec = *fHistos[slot];
53+
for (auto i : ROOT::TSeqU(0, res_vec.size()))
54+
res_vec[i].Add(&histo_vec[i]);
55+
}
56+
}
57+
std::string GetActionName(){
58+
return "TH1weightsHelper";
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef TH1WEIGHTSHELPER_H
2+
#define TH1WEIGHTSHELPER_H
3+
4+
#include "ROOT/RDataFrame.hxx"
5+
#include "ROOT/RVec.hxx"
6+
#include "ROOT/RDF/RInterface.hxx"
7+
8+
class TH1weightsHelper : public ROOT::Detail::RDF::RActionImpl<TH1weightsHelper> {
9+
10+
public:
11+
/// This type is a requirement for every helper.
12+
using Result_t = std::vector<TH1D>;
13+
private:
14+
std::vector<std::shared_ptr<std::vector<TH1D>>> fHistos; // one per data processing slot
15+
std::string _name;
16+
int _nbinsX;
17+
std::vector<float> _xbins;
18+
std::vector<std::string> _weightNames;
19+
20+
21+
public:
22+
/// This constructor takes all the parameters necessary to build the THnTs. In addition, it requires the names of
23+
/// the columns which will be used.
24+
TH1weightsHelper(std::string name, std::string title,
25+
int nbinsX, std::vector<float> xbins,
26+
std::vector<std::string> weightNames
27+
);
28+
29+
TH1weightsHelper(TH1weightsHelper &&) = default;
30+
TH1weightsHelper(const TH1weightsHelper &) = delete;
31+
std::shared_ptr<std::vector<TH1D>> GetResultPtr() const;
32+
void Initialize();
33+
void InitTask(TTreeReader *, unsigned int);
34+
/// This is a method executed at every entry
35+
36+
void Exec(unsigned int slot, const float &var1, const ROOT::VecOps::RVec<float> &weights);
37+
void Finalize();
38+
std::string GetActionName();
39+
};
40+
41+
#endif
42+

0 commit comments

Comments
 (0)