ATLAS Offline Software
integrator_ascii2db.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 #
5 # TileCalibBlobPython_integratorExample
6 # Nils Gollub <nils.gollub@cern.ch>, 2008-02-07
7 # change Yuri Smirnov <iouri.smirnov@cern.ch>, 2014-12-24
8 
9 import sys, re, os.path
10 
11 #=== get a logger
12 from TileCalibBlobPython.TileCalibLogger import getLogger
13 log = getLogger("int_ascii2db")
14 
15 #=== process command line arguments
16 usage = "Usage: integrator_ascii2db.py "
17 usage+= "[-a author] [-c comment] "
18 usage+= "[-from \"yyyy-mm-dd hh:mm:ss\"] "
19 usage+= "file1 [file2 file3 ...]"
20 author = "noAuthor"
21 comment = "noComment"
22 regFrom = -1
23 fileList = []
24 if (len(sys.argv)<2) or ("-h" in sys.argv) or ("--help" in sys.argv):
25  print (usage)
26  sys.exit(0)
27 sys.argv.pop(0)
28 while len(sys.argv)>0:
29  arg = sys.argv.pop(0)
30  if arg=="-a" and len(sys.argv)>0:
31  author = sys.argv.pop(0)
32  elif arg=="-c" and len(sys.argv)>0:
33  comment = sys.argv.pop(0)
34  elif arg=="-from" and len(sys.argv)>0:
35  regFrom = sys.argv.pop(0)
36  else:
37  #=== check if file exists
38  if os.path.exists(arg):
39  fileList.append(arg)
40  else:
41  log.error("File \"%s\" does not exist, ignoring input!", arg)
42 
43 
44 import cppyy
45 from TileCalibBlobPython import TileCalibTools
46 
47 #=== open the database
48 db = TileCalibTools.openDb('SQLITE', 'CONDBR2', 'UPDATE')
49 
50 #=== get a blob writer
51 blobWriter = TileCalibTools.TileBlobWriter(db,"/TILE/ONL01/INTEGRATOR",True,False)
52 
53 #=== declare default vector
54 default = cppyy.gbl.std.vector('float')()
55 default.push_back(0.) # gain 0
56 default.push_back(0.) # sigma(gain) 1
57 default.push_back(0.) # chi2 2
58 default.push_back(0.) # DAC 3
59 default.push_back(0.) # pedestal 4
60 default.push_back(0.) # sigma(pedestal) 5
61 default.push_back(0.) # rms 6
62 default.push_back(0.) # sigma(rms) 7
63 defVec = cppyy.gbl.std.vector('std::vector<float>')()
64 defVec.push_back(default) # gain 1
65 defVec.push_back(default) # gain 2
66 defVec.push_back(default) # gain 3
67 defVec.push_back(default) # gain 4
68 defVec.push_back(default) # gain 5
69 defVec.push_back(default) # gain 6
70 
71 #=== define search patterns
72 re_file = re.compile("int_gains_(\\D{3})(\\d{2})")
73 re_Res = re.compile("Res\\s*channel:\\s*(.*)\\s*gain:\\s*(.*)\\s*" +
74  "gain:\\s*(.*)\\s*\\+\\-\\s*(.*)\\s*" +
75  "chi2/DF:\\s*(.*)" )
76 re_Ped = re.compile("Ped\\s*gain:\\s*(.*)\\s*dac:\\s*(.*)\\s*chan:\\s*(.*)\\s*" +
77  "pedestal:\\s*(.*)\\s*\\+\\-\\s*(.*)\\s*" +
78  "rms:\\s*(.*)\\s*\\+\\-\\s*(.*)\\s*" )
79 
80 #=== define rosId mapping
81 rosId = {'LBA' : 1,
82  'LBC' : 2,
83  'EBA' : 3,
84  'EBC' : 4}
85 
86 #=== loop over all input files
87 for fileName in fileList:
88 
89  #=== extract ros and drawer from fileName
90  result_file = re_file.search(fileName)
91  if result_file:
92  rosIdx = rosId[result_file.groups()[0]]
93  modIdx = int(result_file.groups()[1]) - 1
94  else:
95  log.error("Invalid file name \"%s\", skipping file..." , fileName)
96 
97  log.info("Processing file %s", fileName)
98 
99  #=== get drawer object and initialize
100  flt = blobWriter.getDrawer('Flt',rosIdx,modIdx)
101  flt.init(defVec,48,0)
102 
103  #=== loop through file
104  lines = open(fileName,"r").readlines()
105  resCount=0
106  pedCount=0
107  for line in lines:
108  #=== look for "Res" lines
109  result_Res = re_Res.search(line)
110  if result_Res:
111  pmtIdx = int( result_Res.groups()[0]) - 1 #=== ordered by PMT!!
112  gainIdx = int( result_Res.groups()[1]) - 1
113  gain = float(result_Res.groups()[2])
114  gainErr = float(result_Res.groups()[3])
115  chi2 = float(result_Res.groups()[4])
116  #=== fill into blob
117  flt.setData(pmtIdx,gainIdx,0,gain)
118  flt.setData(pmtIdx,gainIdx,1,gainErr)
119  flt.setData(pmtIdx,gainIdx,2,chi2)
120  resCount += 1
121 
122  #=== look for "Res" lines
123  result_Ped = re_Ped.search(line)
124  if result_Ped:
125  gainIdx = int( result_Ped.groups()[0]) - 1
126  dac = float(result_Ped.groups()[1])
127  pmtIdx = int( result_Ped.groups()[2]) - 1 #=== ordered by PMT!!
128  ped = float(result_Ped.groups()[3])
129  pedErr = float(result_Ped.groups()[4])
130  rms = float(result_Ped.groups()[5])
131  rmsErr = float(result_Ped.groups()[6])
132  flt.setData(pmtIdx,gainIdx,3,dac)
133  flt.setData(pmtIdx,gainIdx,4,ped)
134  flt.setData(pmtIdx,gainIdx,5,pedErr)
135  flt.setData(pmtIdx,gainIdx,6,rms)
136  flt.setData(pmtIdx,gainIdx,7,rmsErr)
137  pedCount += 1
138 
139  log.info("---> %s%s, registered %3i Res and %3i Ped lines", result_file.groups()[0],
140  result_file.groups()[1],
141  resCount, pedCount)
142 
143 #=== register all drawers in the database
144 blobWriter.setComment(author,comment)
145 blobWriter.register(regFrom,-1)
146 
147 #=== close the database connection
148 db.closeDatabase()
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
Trk::open
@ open
Definition: BinningType.h:40
readCCLHist.float
float
Definition: readCCLHist.py:83
python.CaloCondLogger.getLogger
def getLogger(name="CaloCond")
Definition: CaloCondLogger.py:16