Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Z
ZX_PostProcessingPlotter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christian Weber
ZX_PostProcessingPlotter
Commits
5a8c0987
Commit
5a8c0987
authored
4 years ago
by
Christian Weber
Browse files
Options
Downloads
Patches
Plain Diff
Add function to convert between python list and TGraph, and vice versa
parent
5abebf73
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
functions/tGraphHelpers.py
+89
-0
89 additions, 0 deletions
functions/tGraphHelpers.py
with
89 additions
and
0 deletions
functions/tGraphHelpers.py
+
89
−
0
View file @
5a8c0987
...
@@ -29,8 +29,11 @@ def createNamedTGraphAsymmErrors( objectName):
...
@@ -29,8 +29,11 @@ def createNamedTGraphAsymmErrors( objectName):
return
graph
return
graph
def
fillTGraphWithRooRealVar
(
graph
,
xFill
,
yFill
):
def
fillTGraphWithRooRealVar
(
graph
,
xFill
,
yFill
):
# bestEstimate
def
numberToRooRealVar
(
number
):
return
ROOT
.
RooRealVar
(
"
tempRooRealVar
"
,
"
tempRooRealVar
"
,
number
)
if
yFill
is
None
:
return
graph
if
yFill
is
None
:
return
graph
elif
not
isinstance
(
yFill
,
ROOT
.
RooRealVar
):
yFill
=
numberToRooRealVar
(
yFill
)
pointNr
=
graph
.
GetN
()
pointNr
=
graph
.
GetN
()
...
@@ -67,6 +70,49 @@ def fillTGraphWithTuple(graph, xFill, yFill):
...
@@ -67,6 +70,49 @@ def fillTGraphWithTuple(graph, xFill, yFill):
return
graph
return
graph
def
tGraphToList
(
TGraph
,
ySetpoint
=
"
median
"
):
xCopy
=
ROOT
.
Double
()
# use these for pass by reference
yCopy
=
ROOT
.
Double
()
# use these for pass by reference
xList
=
[]
yList
=
[]
for
n
in
xrange
(
0
,
TGraph
.
GetN
()
):
TGraph
.
GetPoint
(
n
,
xCopy
,
yCopy
)
if
ySetpoint
==
"
yHigh
"
:
yCopy
=
ROOT
.
Double
(
yCopy
+
TGraph
.
GetErrorYhigh
(
n
)
)
elif
ySetpoint
==
"
yLow
"
:
yCopy
=
ROOT
.
Double
(
yCopy
-
TGraph
.
GetErrorYlow
(
n
)
)
xList
.
append
(
float
(
xCopy
))
yList
.
append
(
float
(
yCopy
))
return
xList
,
yList
def
listToTGraph
(
xList
,
yList
,
yLowList
=
None
,
yHighList
=
None
):
doErrors
=
True
if
yLowList
is
None
and
yHighList
is
None
:
doErrors
=
False
elif
yLowList
is
None
:
yLowList
=
[]
for
x
in
xrange
(
len
(
yList
)
):
yLowList
.
append
(
yList
[
x
]
+
(
yList
[
x
]
-
yHighList
[
x
]
)
)
elif
yHighList
is
None
:
yHighList
=
[]
for
x
in
xrange
(
len
(
yList
)
):
yHighList
.
append
(
yList
[
x
]
+
(
yList
[
x
]
-
yLowList
[
x
])
)
if
doErrors
:
tGraph
=
ROOT
.
TGraphAsymmErrors
()
else
:
tGraph
=
ROOT
.
TGraph
()
for
x
in
xrange
(
len
(
yList
)
):
tGraph
.
SetPoint
(
x
,
xList
[
x
],
yList
[
x
])
if
doErrors
:
tGraph
.
SetPointError
(
x
,
0
,
0
,
yList
[
x
]
-
yLowList
[
x
],
yHighList
[
x
]
-
yList
[
x
]
)
return
tGraph
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
import
numpy
as
np
import
numpy
as
np
...
@@ -164,4 +210,47 @@ if __name__ == '__main__':
...
@@ -164,4 +210,47 @@ if __name__ == '__main__':
canv
.
Update
()
#"a3" also seems to work https://root.cern/doc/master/classTGraphPainter
canv
.
Update
()
#"a3" also seems to work https://root.cern/doc/master/classTGraphPainter
##########################################
# test tGraphToList and listToTGraph
##########################################
exampleTGraph
=
ROOT
.
TGraphAsymmErrors
()
for
x
in
xrange
(
-
9
,
10
):
n
=
exampleTGraph
.
GetN
()
exampleTGraph
.
SetPoint
(
n
,
x
,
x
**
2
)
exampleTGraph
.
SetPointError
(
n
,
0
,
0
,
1
,
2
)
xList
,
yList
=
tGraphToList
(
exampleTGraph
,
ySetpoint
=
"
median
"
)
_
,
yHighErrorList
=
tGraphToList
(
exampleTGraph
,
ySetpoint
=
"
yHigh
"
)
_
,
yLowError
=
tGraphToList
(
exampleTGraph
,
ySetpoint
=
"
yLow
"
)
exampleTGraph
.
SetLineColor
(
ROOT
.
kBlue
)
#exampleTGraph.Draw() # use 'A' option only for first TGraph apparently
reassembledTGraph
=
listToTGraph
(
xList
,
yList
,
yLowList
=
yLowError
,
yHighList
=
yHighErrorList
)
reassembledTGraph
.
SetLineColor
(
ROOT
.
kRed
)
xList2
,
yList2
=
tGraphToList
(
reassembledTGraph
,
ySetpoint
=
"
median
"
)
_
,
yHighErrorList2
=
tGraphToList
(
reassembledTGraph
,
ySetpoint
=
"
yHigh
"
)
_
,
yLowError2
=
tGraphToList
(
reassembledTGraph
,
ySetpoint
=
"
yLow
"
)
#canv2 = ROOT.TCanvas("canvas", "canvas")
#exampleTGraph.Draw()
#reassembledTGraph.Draw("same")
#canv2.Update()
assert
xList
==
xList2
assert
yList
==
yList2
assert
yHighErrorList
==
yHighErrorList2
assert
yLowError
==
yLowError2
import
pdb
;
pdb
.
set_trace
()
# import the debugger and instruct it to stop here
import
pdb
;
pdb
.
set_trace
()
# import the debugger and instruct it to stop here
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment