-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_runs.py
More file actions
177 lines (143 loc) · 6.37 KB
/
process_runs.py
File metadata and controls
177 lines (143 loc) · 6.37 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#! /usr/bin/env python
import os
import sys
import optparse
import datetime
import subprocess
from glob import glob
from collections import defaultdict
from collections import OrderedDict
from array import array
import time
import re
from ROOT import *
usage = "usage: run from Timing-TOFPET: process_runs.py -r 1,2,3,4,5 [or 1-5] -d myfolder"
parser = optparse.OptionParser(usage)
parser.add_option("-r", "--runs", dest="runs",
help="list of run numbers to be analyzed: 1,2,3,4,5,6 or 1-6")
parser.add_option("-d", "--directory", dest="directory",
help="input/output directory")
(opt, args) = parser.parse_args()
if not opt.runs:
parser.error('Run list not provided')
if not opt.directory:
parser.error('Input/Output directory not provided')
################################################
gROOT.SetBatch(True)
################################################
run_list = []
if ("," in opt.runs) and ("-" not in opt.runs):
run_list = opt.runs.split(",")
print "Runs to be processed ", run_list, " in directory ", opt.directory
if ("-" in opt.runs) and ("," not in opt.runs):
run_list_tmp = opt.runs.split("-")
for ii in range( int(run_list_tmp[0]) , int(run_list_tmp[1])+1 ) :
run_list.append(str(ii))
print "Runs to be processed ", run_list, " in directory ", opt.directory
if ( ("," not in opt.runs) and ("-" not in opt.runs)):
run_list.append(opt.runs)
if len(run_list) == 0:
parser.error('No good run list provided')
list_allfiles = os.listdir(opt.directory)
for irun in run_list:
run = "Run"+irun.zfill(6)
print "Processing ", run, " ....."
input_filename_rawf = ""
input_filename_ini = ""
input_filename_txt = ""
input_filename_temp = ""
list_allfiles = os.listdir(opt.directory)
#print list_allfiles
for thisfile in list_allfiles:
if ("~" in thisfile):
continue
if (run in thisfile and ".rawf" in thisfile):
input_filename_rawf = opt.directory + "/" + thisfile
print input_filename_rawf
if (run in thisfile and ".ini" in thisfile):
input_filename_ini = opt.directory + "/" + thisfile
print input_filename_ini
if (run in thisfile and ".txt" in thisfile):
input_filename_txt = opt.directory + "/" + thisfile
print input_filename_txt
if (run in thisfile and ".temp" in thisfile):
input_filename_temp = opt.directory + "/" + thisfile
print input_filename_temp
if (input_filename_rawf==""):
parser.error('missing rawf file for '+run)
if (input_filename_ini==""):
parser.error('missing ini file for '+run)
if (input_filename_txt==""):
parser.error('missing txt file for '+run)
if (input_filename_temp==""):
parser.error('missing temp file for '+run)
fileNameNoExtension = input_filename_rawf.split(".rawf")[0]
#print fileNameNoExtension
startTime = input_filename_rawf.split("/")[-1].split("_")[1]
#print startTime
year = startTime.split("-")[0]
month = startTime.split("-")[1]
day = startTime.split("-")[2]
hours = startTime.split("-")[3]
minutes = startTime.split("-")[4]
seconds = startTime.split("-")[5]
#print startTime, year, month, day, hours, minutes, seconds
unixTimeStart = long(time.mktime(datetime.datetime.strptime(startTime, "%Y-%m-%d-%H-%M-%S").timetuple())) + 10
#+10 to account for the 10-seconds "sleep" in run_TOF.py
#print unixTimeStart
###############################
### 1) Read configuration file
###############################
cfg = open(input_filename_txt, "r")
convertsinglescript = ""
convertcoincidencescript = ""
for line in cfg:
#skip commented out lines or empty lines
if (line.startswith("#")):
continue
if line in ['\n','\r\n']:
continue
line = line.rstrip('\n')
splitline = line.split()
linetype = splitline[0]
linesize = len(splitline)
if (linetype == "CONVERTSINGLESCRIPT" and linesize==2):
convertsinglescript = splitline[1]
if (linetype == "CONVERTCOINCIDENCESCRIPT" and linesize==2):
convertcoincidencescript = splitline[1]
###############################
### 2) Create file with singles
###############################
print "Creating root file with tree (singles)..."
commandConvertSingles = "./"+convertsinglescript+" --config "+ input_filename_ini +" -i "+fileNameNoExtension+" -o "+fileNameNoExtension+"_singles.root"+" --writeRoot"
print commandConvertSingles
os.system(commandConvertSingles)
print "File created."
print "\n"
## Add branches to root tree (singles)
print "Update root file with tree (singles)..."
inputfilename = fileNameNoExtension+"_singles.root"
gROOT.ProcessLine('o = TString(gSystem->GetMakeSharedLib()); o = o.ReplaceAll(" -c ", " -std=c++11 -c "); gSystem->SetMakeSharedLib(o.Data());')
gROOT.ProcessLine(".L /home/cmsdaq/Workspace/TOFPET/Timing-TOFPET2/analysis/addTemperature.C+")
gROOT.ProcessLine('TFile* f = new TFile("%s","UPDATE");f->cd();'%inputfilename)
gROOT.ProcessLine('TTree* tree; f->GetObject("data",tree);')
gROOT.ProcessLine("addTemperature* addT= new addTemperature(tree);")
gROOT.ProcessLine('addT->out=f');
gROOT.ProcessLine('addT->unixtimeStart=%d;'%unixTimeStart)
gROOT.ProcessLine('addT->tempFile="%s";'%(input_filename_temp))
gBenchmark.Start( 'addTemperature_run%d'%int(irun))
gROOT.ProcessLine("addT->Loop();")
gBenchmark.Show( 'addTemperature_run%d'%int(irun))
gROOT.ProcessLine('f->Close();')
gROOT.ProcessLine('delete addT;delete f;')
print "File updated."
print "\n"
####################################
### 3) Create file with coincidences
####################################
print "Creating root file with tree (N-coincidences) from singles..."
commandConvertCoincidences = "python "+convertcoincidencescript+" -c "+ input_filename_txt +" -i "+ fileNameNoExtension+"_singles.root" +" -o "+fileNameNoExtension+"_coincidences.root"
print commandConvertCoincidences
os.system(commandConvertCoincidences)
print "File created."
print "\n"