Skip to content
Snippets Groups Projects
Commit e1579c2d authored by Arseny Rybnikov's avatar Arseny Rybnikov
Browse files

init with readFile, SetBinContent funcs

parents
No related branches found
No related tags found
No related merge requests found
plot.py 0 → 100644
# ################################################################
# This module intends to simplify ROOT based routines
# ################################################################
# For greenlablib module import use:
# >>> import sys
# >>> sys.path.append('path/to/directory/where/module/is/located')
# >>> from greenlablib import plot as pl
# ################################################################
# Basic includes
import numpy as np
from array import array
# ROOT includes
from ROOT import gROOT
from ROOT import TCanvas
from ROOT import TGraph
from ROOT import TGraphErrors
from ROOT import TH1F
from ROOT import gStyle
from ROOT import gSystem
from ROOT import TBufferJSON
def improveStyles():
'''
Improves default ROOT styles
'''
# Stat box format
gStyle.SetStatFormat("4.3f")
gStyle.SetFitFormat ("4.3f")
gStyle.SetOptStat("emri")
gStyle.SetOptFit(111)
gStyle.SetStatY(0.9)
gStyle.SetStatX(0.9)
gStyle.SetStatW(0.2)
gStyle.SetStatH(0.2)
# Title format
gStyle.SetTitleY(0.98)
gStyle.SetTitleAlign(23)
print("Default ROOT styles are applied")
def readFile(name,ncolumns,delimeter=','): # filename , number of column in the data file
'''
Multiple columns reading from file
Usage of readFile function
D is data array of columns of some variables
Example of an array:
D[0] D[1] D[2] ... D[n]
1 98 12 ... An
2 96 25 ... Bn
.. .. .. ... ..
Z0 Z1 Z2 ... Zn
and D[-1] stores lenght of array
'''
data = [[] for i in range(ncolumns)]
with open(name,'r') as f:
for line in f:
if line.find('#') != -1: # skip '#' comment symbol
continue
for i in range(ncolumns):
val = line.split(delimeter)
if (len(val) == ncolumns):
data[i].append(val[i])
data.append(len(data[0]))
return data
def cd(d): # input parameters d - list - data of the certain column
'''
Preparation of the data for ROOT style presentation
Convertation: python list type -> C++ double type
'''
data = array('d')
for i in range(len(d)):
data.append(float(d[i]))
return data
def drawBinContent(bin_arr,val_arr,name='h',title='histo',nbins=100,bin_first=0,bin_last=100):
'''
Implements the SetBinContent ROOT method for TH1F histograms
'''
bin_arr = cd(bin_arr)
val_arr = cd(val_arr)
h = TH1F(name,title,int(nbins),bin_first,bin_last)
h.GetYaxis().SetMaxDigits(3);
for b,v in zip(bin_arr,val_arr):
h.SetBinContent(int(b),v)
h.SetLineWidth(2)
h.Draw("")
return h
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment