Skip to content

Commit

Permalink
Merge pull request #11 from lcorcodilos/test_dev
Browse files Browse the repository at this point in the history
Add initial unit tests with GitHub Action support
  • Loading branch information
lcorcodilos authored Oct 12, 2020
2 parents b0be287 + c9851b8 commit e5893d6
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 14 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-18.04
strategy:
matrix:
python-version: [2.7,3.6] #3.6.9,

steps:
- uses: actions/checkout@v2
- name: Hack to get setup-python to work on act
run: |
if [ ! -f "/etc/lsb-release" ] ; then
echo "DISTRIB_RELEASE=18.04" > /etc/lsb-release
fi
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Setup environment
run: |
python -m pip install --upgrade pip
pip install virtualenv
python -m virtualenv timber-env
source timber-env/bin/activate
pip install pytest gdown
gdown https://drive.google.com/uc?id=11dXffndcD3OkHqJ3x2iaVZj5C8HVLeg_ -O root_v6-22-00_build.tgz
tar -xzf root_v6-22-00_build.tgz
- name: Pytest
run: |
source timber-env/bin/activate
source setup.sh
source root_v6.22.00.build/bin/thisroot.sh
export TIMBERPATH=$TIMBERPATH
pytest test/
7 changes: 2 additions & 5 deletions TIMBER/Analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pprint, time, json, copy, os, sys, subprocess
from collections import OrderedDict
pp = pprint.PrettyPrinter(indent=4)
from Tools.Common import GetHistBinningTuple, CompileCpp
from TIMBER.Tools.Common import GetHistBinningTuple, CompileCpp
from clang import cindex

# For parsing c++ modules
Expand Down Expand Up @@ -722,9 +722,6 @@ def PrintNodeTree(self,outfilename,verbose=False):

dot.render(outfilename)




##############
# Node Class #
##############
Expand Down Expand Up @@ -1184,7 +1181,7 @@ def MakeCall(self,inArgs):

else:
if len(inArgs) != len(self.__funcInfo[self.__mainFunc].keys()):
raise ValueError('Provided number of arguments (%s) does not match required (%s).'%(len(in_args),len(self.__funcInfo[self.__mainFunc].keys())))
raise ValueError('Provided number of arguments (%s) does not match required (%s).'%(len(inArgs),len(self.__funcInfo[self.__mainFunc].keys())))
args_to_use = inArgs

# var_types = [self.__funcInfo[self.__mainFunc][a] for a in self.__funcInfo[self.__mainFunc].keys()]
Expand Down
3 changes: 1 addition & 2 deletions TIMBER/Tools/CMS/CMS_lumi.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,4 @@ def CMS_lumi(pad, iPeriod=4, iPosX=11, sim=False ):
if not sim: latex.DrawLatex(posX_, posY_, extraText)
else: latex.DrawLatex(posX_, posY_, extraText + ' simulation')

pad.Update()

pad.Update()
Empty file added TIMBER/Tools/CMS/__init__.py
Empty file.
3 changes: 1 addition & 2 deletions TIMBER/Tools/CMS/tdrstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,4 @@ def setTDRStyle():
tdrStyle.SetHatchesLineWidth(5)
tdrStyle.SetHatchesSpacing(0.05)

tdrStyle.cd()

tdrStyle.cd()
8 changes: 4 additions & 4 deletions TIMBER/Tools/Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
'''

import json, ROOT, os
import json, ROOT, os, subprocess
from ROOT import RDataFrame
import CMS_lumi, tdrstyle
from TIMBER.Tools.CMS import CMS_lumi, tdrstyle
from contextlib import contextmanager
from collections import OrderedDict
###################
Expand Down Expand Up @@ -181,7 +181,7 @@ def dictToLatexTable(dict2convert,outfilename,roworder=[],columnorder=[]):
column_string = ' &'
for c in columns:
column_string += str(c)+'\t& '
column_string = column_string[:-2]+'\\\ \n'
column_string = column_string[:-2]+'\\\\ \n'
latexout.write(column_string)

latexout.write('\\hline \n')
Expand All @@ -192,7 +192,7 @@ def dictToLatexTable(dict2convert,outfilename,roworder=[],columnorder=[]):
row_string += str(dict2convert[r][c])+'\t& '
else:
row_string += '- \t& '
row_string = row_string[:-2]+'\\\ \n'
row_string = row_string[:-2]+'\\\\ \n'
latexout.write(row_string)

latexout.write('\\hline \n')
Expand Down
2 changes: 1 addition & 1 deletion TIMBER/Tools/Plot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CMS_lumi
from TIMBER.Tools.CMS import CMS_lumi
import ROOT, collections
from collections import OrderedDict
from TIMBER.Analyzer import HistGroup
Expand Down
Binary file added examples/GluGluToHToTauTau.root
Binary file not shown.
1 change: 1 addition & 0 deletions examples/cmsopendata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://opendata.cern.ch/record/12351
73 changes: 73 additions & 0 deletions test/test_Analyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import ROOT
ROOT.gROOT.SetBatch(True)
from TIMBER.Analyzer import analyzer, CutGroup, VarGroup
from TIMBER.Tools.Common import CompileCpp

class TestAnalyzer():
def setup(self):
CompileCpp('TIMBER/Framework/include/common.h') # Compile (via gInterpreter) commonly used c++ code
CompileCpp('examples/example.cc')
self.a = analyzer('examples/GluGluToHToTauTau.root')
self.a.Cut("test_cut1", "Jet_pt.size() > 0")
self.a.Define('lead_vector', 'analyzer::TLvector(Jet_pt[0],Jet_eta[0],Jet_phi[0],Jet_mass[0])')
self.a.Define('sublead_vector','analyzer::TLvector(Jet_pt[1],Jet_eta[1],Jet_phi[1],Jet_mass[1])')
self.a.Define('invariantMass','analyzer::invariantMass(lead_vector,sublead_vector)')

def test_genEventCount_None(self):
'''Test genEventCount is assigned 0 when branch doesn't exist in test file'''
assert self.a.genEventCount == 0

def test_lhaid_None(self):
'''Test lhaid is assigned 0 when branch doesn't exist in test file'''
assert self.a.lhaid == '-1'

def test_GetTrackedNodeNames(self):
'''Test GetTrackNodeNames method after adding a node'''
assert self.a.GetTrackedNodeNames() == ['test_cut1','lead_vector','sublead_vector','invariantMass']

def test_makehist(self):
'''Makes a simple histogram of the defined variable'''
h = self.a.DataFrame.Histo1D(('m_jj','m_{jj}',20,0,200),'invariantMass')
h.Draw("lego")
assert True

def test_snapshot(self,tmp_path):
'''Makes a simple snapshot'''
out_vars = ['nJet','test_define']
self.a.GetActiveNode().Snapshot(out_vars,str(tmp_path)+'/ex1_out.root','test_snapshot',lazy=False,openOption='RECREATE')
assert True

def test_AddCorrection(self):
pass

def test_MakeWeightCols(self):
pass

def test_MakeTemplateHistos(self):
pass

def test_DrawTemplates(self):
pass

def test_Nminus1(self):
pass

def test_PrintNodeTree(self):
pass

def test_Groups():
a = analyzer('examples/GluGluToHToTauTau.root')
CompileCpp('TIMBER/Framework/include/common.h') # Compile (via gInterpreter) commonly used c++ code
CompileCpp('examples/example.cc')

test_vg = VarGroup('test_vg')
test_vg.Add('lead_vector', 'analyzer::TLvector(Jet_pt[0],Jet_eta[0],Jet_phi[0],Jet_mass[0])')
test_vg.Add('sublead_vector','analyzer::TLvector(Jet_pt[1],Jet_eta[1],Jet_phi[1],Jet_mass[1])')
test_vg.Add('invariantMass','analyzer::invariantMass(lead_vector,sublead_vector)')

test_cg = CutGroup('test_cg')
test_cg.Add("test_cut1", "Jet_pt.size() > 0")

a.Apply([test_vg, test_cg])
rep = a.DataFrame.Report()
rep.Print()
17 changes: 17 additions & 0 deletions test/test_Common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from TIMBER.Tools.Common import *

class CommonTest():
def setup(self):
self.a = analyzer('examples/GluGluToHToTauTau.root')

def test_CutflowHist(self):
pass

def test_CutflowTxt(self):
pass

def test_openJSON(self):
pass

def test_GetHistBinningTuple(self):
pass

0 comments on commit e5893d6

Please sign in to comment.