-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdfs_root_to_csv_py.py
49 lines (40 loc) · 1.58 KB
/
pdfs_root_to_csv_py.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import sys
import argparse
from ROOT import TFile
from ROOT import TH1F
parser = argparse.ArgumentParser()
parser.add_argument("-i", '--file-in' , type=str, help="input file" , required=True)
parser.add_argument("-o", '--file-out', type=str, help="output file", required=False, default = "SiPMnoisePDF_{run_number}.csv")
parser.add_argument("--override", action="store_true", help="override output file")
CLI = parser.parse_args(sys.argv[1:])
assert os.path.exists(CLI.file_in ), "Input file does not exist"
assert CLI.override or not os.path.exists(CLI.file_out), "Output file already exists"
file_in = TFile(CLI.file_in)
run_number = CLI.file_in.split("_R")[-1].split(".root")[0]
file_out = open(CLI.file_out.format(run_number = run_number), "w")
min_run = run_number
max_run = "NULL"
h = None
buffer = "MinRun, MaxRun, SensorID, BinEnergyPes, Probability\n"
for dice in range(1, 29):
print "DICE", dice
for i_sipm in range(64):
ID = dice*1000 + i_sipm
hname = "PDF1/SiPM_{}".format(ID)
previous_h = h
h = file_in.Get(hname)
try:
h.Scale(1/h.Integral())
except AttributeError:
h = previous_h
h.Reset()
except ZeroDivisionError:
pass
xaxis = h.GetXaxis()
for i in range(1, h.GetNbinsX() + 1):
e = xaxis.GetBinCenter(i)
p = h.GetBinContent(i)
buffer += "{min_run}, {max_run}, {ID}, {e}, {p}\n".format(**locals())
file_out.write(buffer)
buffer = ""