ATLAS Offline Software
LArNewCalib_MergeDB.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 import os,sys
4 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
5 from AthenaConfiguration.ComponentFactory import CompFactory
6 from IOVDbSvc.IOVDbSvcConfig import addFolders
7 
8 def DBConnectionFile(sqlitefile):
9  return "sqlite://;schema="+sqlitefile+";dbname=CONDBR2"
10 
11 def mergeDBCfg(flags, InputKeys=[], InputSQLiteFiles=[]):
12  result=ComponentAccumulator()
13 
14  print("mergeDBCfg",len(InputSQLiteFiles)," SQLite files: ",InputSQLiteFiles)
15  print(len(InputKeys)," InputKeys: ",InputKeys)
16 
17 
18  if not flags.LArCalib.isSC: GroupingType = "ExtendedSubDetector"
19  else: GroupingType = "SuperCells"
20 
21  if ( flags.LArCalib.Output.POOLFile ):
22  if os.path.exists(flags.LArCalib.Output.POOLFile):
23  os.remove(flags.LArCalib.Output.POOLFile)
24 
25  for key in InputKeys:
26 
27  print("Working for: ",key)
28  KeyOutput = key
29  FlagInput = key
30 
31  if 'Pedestal' == key or 'PhysWave' == key or 'MphysOverMcal' == key or 'Ramp' == key or 'Params' in key:
32  KeyInput="LAr"+key
33  if 'Pedestal' == key or 'PhysWave' == key or 'CaliWave' or 'MphysOverMcal' == key or 'Ramp' == key or 'Params' in key:
34  KeyInput="LAr"+key
35  KeyOutput="LAr"+key
36 
37  if 'AutoCorr' in key:
38  KeyInput='LArAutoCorr'
39 
40  ofcphyskey=None
41  if "OFC" in key:
42  KeyInput = "LArOFC"
43  if "Cali" in key:
44  FlagInput = "OFCCali"
45 
46  if "Phys" in key:
47  ofcphyskeytmp = key.split("OFCPhys")[1]
48  if 'Mu' in ofcphyskeytmp:
49  ofcphyskey=ofcphyskeytmp.replace("Mu","")
50  else:
51  ofcphyskey=ofcphyskeytmp
52  FlagInput = "OFCPhys"
53 
54  shapekey=None
55  if "Shape" in key:
56  shapekey = key.split("Shape")[1]
57  FlagInput = "Shape"
58  KeyInput = "LArShape"
59 
60 
61  #if FlagInput in flags._flagdict.keys():
62  if [i for i in flags._flagdict.keys() if FlagInput in i]:
63  try:
64  Folder = flags._get('LArCalib.'+FlagInput+'.Folder')
65  except Exception as e:
66  print(e)
67  print(flags._flagdict)
68  sys.exit(-1)
69  else:
70  print('No folder in flags for '+FlagInput)
71  print(flags._flagdict.keys())
72  sys.exit(-2)
73 
74  if shapekey is not None:
75  if Folder.endswith("/"):
76  Folder+=shapekey
77  else:
78  Folder+="/"+shapekey
79 
80  if ofcphyskey is not None and ofcphyskey != "":
81  print("Folder: ",Folder," ofcphyskey: ",ofcphyskey)
82  if "4samples" in Folder:
83  Folder = Folder.replace("4samples", ofcphyskey)
84  elif Folder.endswith("/"):
85  Folder+=ofcphyskey
86  else:
87  Folder+="/"+ofcphyskey
88 
89  if "OFCPhys" in FlagInput:
90  print("Folder: ",Folder)
91  # Get the available tags
92  from PyCool import cool
93  dbSvc=cool.DatabaseSvcFactory.databaseService()
94  db_string=DBConnectionFile(InputSQLiteFiles[0])
95  try:
96  db=dbSvc.openDatabase(db_string)
97  except Exception as e:
98  print ('Problem opening database',e)
99  print(db_string)
100  sys.exit(-3)
101  cool_folder=db.getFolder(Folder)
102  tags = [ x for x in cool_folder.listTags() ]
103  if len(tags) == 1:
104  TagSpec = str(tags[0])
105  else:
106  if flags.LArCalib.OFC.Ncoll > 0 or 'Mu' in key:
107  TagSpec = [ str(tag) for tag in tags if "-mu-" in str(tag)][0]
108  else:
109  TagSpec = [ str(tag) for tag in tags if "-mu-" not in str(tag)][0]
110  else:
111  from LArCalibProcessing.utils import FolderTagResolver as FolderTagResover
112  FolderTagResover._globalTag=flags.IOVDb.GlobalTag
113  #rs=FolderTagResover(flags.LArCalib.Input.Database2)
114  # until not all folders in COOL:
115  rs=FolderTagResover(DBConnectionFile(InputSQLiteFiles[0]))
116  FolderTag = rs.getFolderTagSuffix(Folder)
117  del rs
118 
119  from LArCalibProcessing.LArCalibConfigFlags import LArCalibFolderTag
120  TagSpec = LArCalibFolderTag(Folder,FolderTag)
121 
122 
123  if 'Wave' in KeyInput:
124  OutputObjectSpec = KeyInput+"Container#"+KeyOutput+"#"+Folder
125  else:
126  OutputObjectSpec = KeyInput+"Complete#"+KeyOutput+"#"+Folder
127  OutputObjectSpecTag = TagSpec
128 
129  print("**",KeyInput,FlagInput,"folder **",Folder)
130  print("** TagSpec: ",TagSpec)
131  print("** keyOutput: ",KeyOutput)
132 
133 
134  inkeys=[]
135  inobjs=[]
136 
137  for sqf in InputSQLiteFiles:
138  thiskey = KeyInput+str(InputSQLiteFiles.index(sqf))
139 
140  if 'PhysAutoCorr' in key: thiskey='Phys'+thiskey
141 
142  if 'OFC' in key and 'Mu' in key:
143  thiskey+="Mu"
144  if 'OFC' in key and 'Cali' in key:
145  thiskey+="Cali"
146 
147  inkeys.append(thiskey)
148  if 'Wave' in KeyInput:
149  inobjs.append(KeyInput+"Container#"+thiskey)
150  else:
151  inobjs.append(KeyInput+"Complete#"+thiskey)
152 
153  if 'Wave' in KeyInput or 'Params' in KeyInput:
154  result.merge(addFolders(flags,Folder,tag=TagSpec,detDb=sqf,modifiers="<key>"+thiskey+"</key>"))
155  else:
156  result.merge(addFolders(flags,Folder,tag=TagSpec,detDb=sqf,modifiers="<key>"+thiskey+"</key>", className=KeyInput+"Complete"))
157  pass
158 
159  pass
160 
161  if 'Wave' in KeyInput:
162  result.addCondAlgo(CompFactory.getComp('ForceLoadCondObj')(KeyOutput,ObjectList=inobjs))
163  result.addCondAlgo(CompFactory.getComp('LArConditionsMergerAlg<LArDAC2uAComplete,'+KeyInput+'Container>')(key+"Merger",
164  GroupingType = GroupingType, WriteKey = KeyOutput, DetStoreReadKeys = inkeys))
165  elif 'Params' in KeyInput:
166  result.addCondAlgo(CompFactory.getComp('ForceLoadCondObj')(KeyOutput,ObjectList=inobjs))
167  result.addCondAlgo(CompFactory.getComp('LArConditionsMergerAlg<LArDAC2uAComplete,'+KeyInput+'Complete>')(key+"Merger",
168  GroupingType = GroupingType, WriteKey = KeyOutput, DetStoreReadKeys = inkeys))
169  else:
170  result.addCondAlgo(CompFactory.getComp('LArConditionsMergerAlg<'+KeyInput+'Complete, LArPhysWaveContainer>')(key+"Merger",
171  GroupingType = GroupingType, WriteKey = KeyOutput, ReadKeys = inkeys))
172 
173  if ( flags.LArCalib.Output.POOLFile ):
174 
175  from RegistrationServices.OutputConditionsAlgConfig import OutputConditionsAlgCfg
176  result.merge(OutputConditionsAlgCfg(flags,name="OutputConditionsAlg"+KeyInput, outputFile=flags.LArCalib.Output.POOLFile,
177  ObjectList=[OutputObjectSpec], IOVTagList=[OutputObjectSpecTag],
178  Run1 = flags.LArCalib.IOVStart, Run2= flags.LArCalib.IOVEnd, WriteIOV=True))
179  pass
180 
181  result.addService(CompFactory.IOVRegistrationSvc(RecreateFolders = True))
182 
183  #MC Event selector since we have no input data file
184  from McEventSelector.McEventSelectorConfig import McEventSelectorCfg
185  result.merge(McEventSelectorCfg(flags,
186  RunNumber = flags.LArCalib.Input.RunNumbers[0],
187  EventsPerRun = 1,
188  FirstEvent = 1,
189  InitialTimeStamp = 0,
190  TimeStampInterval = 1))
191 
192  result.printConfig(withDetails=True, printDefaults=True)
193  return result
194 
195 
196 if __name__=="__main__":
197  import argparse
198 
199  def list_of_strings(arg):
200  return arg.split(',')
201 
202  # now process the CL options and assign defaults
203  parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
204  parser.add_argument('-r','--run', dest='run', default=str(0x7FFFFFFF), help='Run number to query input DB', type=str)
205  parser.add_argument('-i','--insqlite', dest='insql', default="", help='List of input sqlite files, comma separated, if empty all *.db files in current dir.', type=list_of_strings)
206  parser.add_argument('-k','--inkeys', dest='inkeys', default=["LArPedestal"], help='List of input keys to process', type=list_of_strings)
207  parser.add_argument('-o','--outsqlite', dest='outsql', default="freshConstants_merged.db", help='Output sqlite file', type=str)
208  parser.add_argument('-p','--poolfile', dest='poolfile', default="freshConstants_pp.pool.root", help='Output pool file', type=str)
209  parser.add_argument('-d','--dbname', dest='dbname', default="COOLOFL_LAR/CONDBR2", help='DB to query for tags', type=str)
210  parser.add_argument('--iovstart',dest="iovstart", default=0, help="IOV start (run-number)", type=int)
211  parser.add_argument('--isSC', dest='supercells', default=False, help='is SC data ?', action='store_true')
212  parser.add_argument('-c','--poolcat', dest='poolcat', default="freshConstants.xml", help='Catalog of POOL files', type=str)
213  parser.add_argument('--Ncoll',dest='Ncoll', default=60, help='Number of MinBias collision assumed for OFCs folder', type=int)
214  args = parser.parse_args()
215  if help in args and args.help is not None and args.help:
216  parser.print_help()
217  sys.exit(0)
218 
219  if len(args.insql)==0:
220  InputSQLiteFiles = [ db for db in os.listdir(".") if db.endswith(".db") ]
221  else:
222  InputSQLiteFiles = args.insql
223 
224  if len(InputSQLiteFiles) == 1: #Main readout case, copy first input to output
225  import shutil, os
226  shutil.copyfile(InputSQLiteFiles[0],args.outsql)
227 
228  #Import the flag-container that is the arguemnt to the configuration methods
229  from AthenaConfiguration.AllConfigFlags import initConfigFlags
230  from LArCalibProcessing.LArCalibConfigFlags import addLArCalibFlags
232  addLArCalibFlags(flags, args.supercells)
233 
234  #Now we set the flags as required for this particular job:
235  flags.LArCalib.Input.RunNumbers = [int(args.run),]
236  flags.LArCalib.Input.Database = args.insql
237  flags.LArCalib.Input.Database2 = args.dbname
238 
239  flags.LArCalib.Output.POOLFile = args.poolfile
240  #flags.LArCalib.Output.ROOTFile2 ="freshConstants2_pp.pool.root"
241  flags.IOVDb.DBConnection=DBConnectionFile(args.outsql)
242 
243  #The global tag we are working with
244  flags.IOVDb.GlobalTag = "LARCALIB-RUN2-00"
245 
246  flags.Input.Files=[]
247  flags.LArCalib.Input.Files = [ ]
248  flags.LArCalib.OFC.Ncoll = args.Ncoll
249  flags.LArCalib.IOVStart = args.iovstart
250 
251  flags.LAr.doAlign=False
252  flags.Input.RunNumbers=flags.LArCalib.Input.RunNumbers
253 
254  from AthenaConfiguration.TestDefaults import defaultGeometryTags
255  flags.GeoModel.AtlasVersion=defaultGeometryTags.RUN3
256 
257  flags.Debug.DumpDetStore=True
258  flags.Debug.DumpCondStore=True
259 
260  flags.lock()
261 
262  #Import the MainServices (boilerplate)
263  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
264  cfg=MainServicesCfg(flags)
265  from LArGeoAlgsNV.LArGMConfig import LArGMCfg
266  cfg.merge(LArGMCfg(flags))
267  #from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
268  #cfg.merge(PoolReadCfg(flags))
269  from AthenaPoolCnvSvc.PoolCommonConfig import PoolSvcCfg
270  cfg.merge(PoolSvcCfg(flags))
271 
272  cfg.merge(mergeDBCfg(flags,args.inkeys, InputSQLiteFiles))
273 
274  cfg.getService("PoolSvc").ReadCatalog+=["xmlcatalog_file:%s"%args.poolcat,]
275  cfg.getService("PoolSvc").WriteCatalog="xmlcatalog_file:%s"%args.poolcat
276  cfg.getService("IOVDbSvc").DBInstance=""
277 
278  cfg.printConfig(withDetails=True, summariseProps=True, printDefaults=True)
279  #cfg.getService("StoreGateSvc").Dump=True
280  #cfg.getService("StoreGateSvc").OutputLevel=2
281 
282  cfg.run(1)
python.JetAnalysisCommon.ComponentAccumulator
ComponentAccumulator
Definition: JetAnalysisCommon.py:302
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.LArNewCalib_MergeDB.mergeDBCfg
def mergeDBCfg(flags, InputKeys=[], InputSQLiteFiles=[])
Definition: LArNewCalib_MergeDB.py:11
OutputConditionsAlgConfig.OutputConditionsAlgCfg
def OutputConditionsAlgCfg(flags, name="OutputConditionsAlg", outputFile='condobjs.root', **kwargs)
Definition: OutputConditionsAlgConfig.py:5
python.MainServicesConfig.MainServicesCfg
def MainServicesCfg(flags, LoopMgr='AthenaEventLoopMgr')
Definition: MainServicesConfig.py:260
python.McEventSelectorConfig.McEventSelectorCfg
def McEventSelectorCfg(flags, **kwargs)
Definition: McEventSelectorConfig.py:5
python.IOVDbSvcConfig.addFolders
def addFolders(flags, folderStrings, detDb=None, className=None, extensible=False, tag=None, db=None, modifiers='')
Definition: IOVDbSvcConfig.py:72
python.LArNewCalib_MergeDB.DBConnectionFile
def DBConnectionFile(sqlitefile)
Definition: LArNewCalib_MergeDB.py:8
LArGMConfig.LArGMCfg
def LArGMCfg(flags)
Definition: LArGMConfig.py:8
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
python.PoolCommonConfig.PoolSvcCfg
def PoolSvcCfg(flags, withCatalogs=False, **kwargs)
Definition: PoolCommonConfig.py:7
python.LArCalibConfigFlags.LArCalibFolderTag
def LArCalibFolderTag(folder, tag)
Definition: LArCalibConfigFlags.py:99
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
str
Definition: BTagTrackIpAccessor.cxx:11
python.LArCalibConfigFlags.addLArCalibFlags
def addLArCalibFlags(flags, isSC=False)
Definition: LArCalibConfigFlags.py:3
python.LArNewCalib_MergeDB.list_of_strings
def list_of_strings(arg)
Definition: LArNewCalib_MergeDB.py:199