Skip to content

Commit 8d51dc3

Browse files
committed
add new class TblSMSNevt
1 parent 7e3f924 commit 8d51dc3

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

atheppy/heppyresult/TblSMSNevt.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Tai Sakuma <[email protected]>
2+
from alphatwirl.misc import mkdir_p
3+
from alphatwirl.misc import list_to_aligned_text
4+
import os
5+
import numpy as np
6+
import ROOT
7+
8+
##__________________________________________________________________||
9+
class TblSMSNevt(object):
10+
def __init__(self, analyzerName, fileName, outPath):
11+
self.analyzerName = analyzerName
12+
self.fileName = fileName
13+
self.outPath = outPath
14+
15+
def begin(self):
16+
self.rows = [('component', 'histname', 'smsmass1', 'smsmass2', 'nevt')]
17+
18+
def read(self, component):
19+
inputPath = os.path.join(getattr(component, self.analyzerName).path, self.fileName)
20+
file_ = ROOT.TFile.Open(inputPath)
21+
22+
histnames = ['h_Gluino_LSP', 'h_Stop_LSP', 'h_Sbottom_LSP', 'h_Squark_LSP']
23+
for histname in histnames:
24+
h = file_.Get(histname)
25+
xaxis_lastbin_lowedge = h.GetXaxis().GetBinLowEdge(h.GetNbinsX()+1)
26+
mass1 = np.concatenate((np.array([1]), np.arange(50, xaxis_lastbin_lowedge+50, step=50, dtype=int)))
27+
yaxis_lastbin_lowedge = h.GetYaxis().GetBinLowEdge(h.GetNbinsY()+1)
28+
mass2 = np.concatenate((np.array([1]), np.arange(50, yaxis_lastbin_lowedge+50, step=50, dtype=int)))
29+
for m1 in mass1:
30+
binx = h.GetXaxis().FindBin(m1)
31+
## binlowedgex = h.GetXaxis().GetBinLowEdge(binx)
32+
for m2 in mass2:
33+
biny = h.GetYaxis().FindBin(m2)
34+
## binlowedgey = h.GetYaxis().GetBinLowEdge(biny)
35+
c = h.GetBinContent(binx, biny)
36+
if c == 0:
37+
continue
38+
self.rows.append(
39+
(component.name, histname,
40+
int(m1), int(m2), int(c))
41+
)
42+
43+
def end(self):
44+
mkdir_p(os.path.dirname(self.outPath))
45+
with open(self.outPath, 'w') as f:
46+
f.write(list_to_aligned_text(self.rows))
47+
48+
##__________________________________________________________________||

atheppy/heppyresult/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
from .EventBuilderConfigMaker import EventBuilderConfigMaker
2525
from .TblBranch import TblBranch
2626
from .TblTree import TblTree
27+
from .TblSMSNevt import TblSMSNevt

tests/heppyresult/test_TblSMSNevt.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Tai Sakuma <[email protected]>
2+
import os
3+
import sys
4+
5+
import pytest
6+
7+
try:
8+
import unittest.mock as mock
9+
except ImportError:
10+
import mock
11+
12+
from atheppy.heppyresult import TblSMSNevt
13+
14+
##__________________________________________________________________||
15+
@pytest.fixture(autouse=True)
16+
def mock_mkdir_p(monkeypatch):
17+
ret = mock.Mock()
18+
module = sys.modules['atheppy.heppyresult.TblSMSNevt']
19+
monkeypatch.setattr(module, 'mkdir_p', ret)
20+
return ret
21+
22+
## @pytest.fixture(autouse=True)
23+
## def mock_open(monkeypatch):
24+
## ret = mock.MagicMock()
25+
## module = sys.modules['atheppy.heppyresult.TblSMSNevt']
26+
## monkeypatch.setitem(module, 'open', ret)
27+
## return ret
28+
29+
@pytest.fixture()
30+
def component_t1tttt():
31+
ret = mock.Mock()
32+
name = 'SMS_T1tttt_madgraphMLM'
33+
thisdir = os.path.dirname(os.path.realpath(__file__))
34+
analyzer_path = os.path.join(
35+
thisdir, 'test_data', name, 'susyParameterScanAnalyzer')
36+
ret.susyParameterScanAnalyzer.path = analyzer_path
37+
ret.name = name
38+
return ret
39+
40+
@pytest.fixture()
41+
def component_t2bb():
42+
ret = mock.Mock()
43+
name = 'SMS_T2bb_madgraphMLM'
44+
thisdir = os.path.dirname(os.path.realpath(__file__))
45+
analyzer_path = os.path.join(
46+
thisdir, 'test_data', name, 'susyParameterScanAnalyzer')
47+
ret.susyParameterScanAnalyzer.path = analyzer_path
48+
ret.name = name
49+
return ret
50+
51+
@pytest.fixture()
52+
def obj():
53+
ret = TblSMSNevt(
54+
analyzerName='susyParameterScanAnalyzer',
55+
fileName='genEvtsPerMass.root',
56+
outPath='tbt_nevt_sms.txt'
57+
)
58+
yield ret
59+
60+
def test_read(obj, component_t1tttt, component_t2bb):
61+
obj.begin()
62+
obj.read(component_t1tttt)
63+
obj.read(component_t2bb)
64+
65+
with mock.patch('__builtin__.open', mock.mock_open()) as mock_file:
66+
obj.end()
67+
open().write.call_args_list
68+
69+
70+
##__________________________________________________________________||

0 commit comments

Comments
 (0)