-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNucDBExtractors.py
More file actions
149 lines (128 loc) · 4.5 KB
/
NucDBExtractors.py
File metadata and controls
149 lines (128 loc) · 4.5 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
from ROOT import gROOT,gSystem
#gSystem.Load( 'libTreeViewer' )
#gSystem.Load( 'libNucDB' )
#import numpy as np
import sys
import time
import datetime
#import MySQLdb
import math
import os
from ROOT import NucDBManager
from ROOT import NucDBDataPoint
from ROOT import NucDBExperiment,NucDBMeasurement
from ROOT import NucDBBinnedVariable,NucDBDiscreteVariable
from ROOT import NucDBInvariantMassDV,NucDBPhotonEnergyDV
import copy
""" @package NucDBRawDataExtractor
Used to extract experimental results from a raw data text file.
Implementation is specific to raw text file structure.
"""
class RowCut:
""" Simple cut on a variable for a row in columnwise textfile
"""
def __init__(self):
self.columnNumber=0
self.nominalValue=0
self.nominalRange=0
self.currentValue=0
def IsGoodRow(self):
if self.currentValue == self.nominalValue :
return 1
else :
return 0
class NucDBRawDataExtractor:
""" ABC for extracting experimental results from data text files.
Implementation is specific to raw text file structure.
::ParseLine needs to be implemented for each measurement.
The order of things is:
::__init_
::SetMeasurement
::SetInputFile
::Initialize
::ExtractAllValues
"""
def __init__(self):
self._skip_line_ = 0
self.linestoskip = 0
self.currentline = 0
self._line_number = 0
self._lines_read = 0
self.inputfile = 0
self.inputfilename = 0
self.fMeasurement = 0
self.NumberOfLines = 0
self.rowcut = RowCut()
self.fCurrentDataPoint = NucDBDataPoint()
self.fCurrentDataPoint.SetName("A")
#self.fCurrentDataPoint.fDimension=2
def ParseLine(self):
""" Specific to file structure. Should be reimplemented/modified for every extractor."""
values = self.currentline.split()
#for i in range(9):
#print str(i) + " = " + str(values[i])
def SetInputFile(self,filename,nskip=0,nread=0):
"""Set the input file name.
Optionally the number of header lines to skip and/or total number to read.
"""
self.inputfilename = filename
self.linestoskip = nskip
self.NInputLines = nread
self.NumberOfLines = self.FileLen(filename)
if not nread:
self.NInputLines = self.NumberOfLines - nskip
if self.inputfile:
self.inputfile.close()
self.inputfile = open(self.inputfilename, "r")
def FileLen(self,fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
def is_number(self,s):
try:
float(s)
return True
except ValueError:
return False
def SetMeasurement(self, meas):
self.fMeasurement = meas
def Initialize(self):
" "
self._lines_read = 0
self._skip_line = 0
self._line_number = 0
skip = self.linestoskip
for i in range(skip):
self.currentline = self.inputfile.readline() # keep it one line ahead of searchfile
self._line_number = self._line_number + 1
#print self.currentline
#print "Initialization Complete"
#print "NInputLines: " + str(self.NInputLines)
#print "linestoskip: " + str(self.linestoskip)
def GetNextLine(self):
self._skip_line = 0
self.currentline = self.inputfile.readline()
self._line_number = self._line_number + 1
self._lines_read = self._lines_read + 1
if self.currentline.__len__() < 2 :
self._skip_line = 1
def GetNextValue(self):
if not self.fMeasurement :
return
if self._lines_read > self.NInputLines :
return
if self._skip_line :
print " Bad line: " + self.currentline
return
#print "lines_read: " + str(self._lines_read)
self.ParseLine()
if self.rowcut.IsGoodRow():
self.fCurrentDataPoint.SetName("p"+str(self.fMeasurement.GetNDataPoints()));
self.fMeasurement.AddDataPoint(copy.deepcopy(self.fCurrentDataPoint))
def ExtractAllValues(self):
#for currentline in self.inputfile:
for i in range(self.NInputLines):
self.GetNextLine()
self.GetNextValue()
#print "Extraction Complete"