SND@LHC Software
Loading...
Searching...
No Matches
snd_conDB.py
Go to the documentation of this file.
7from __future__ import print_function, division
8from factory import APIFactory
9import datetime, sys
10
11
12from builtins import range
13import operator
14from argparse import ArgumentParser
15from array import array
16import os,ROOT
17
18
20 Info={}
21 nav = ROOT.gGeoManager.GetCurrentNavigator()
22 nav.cd(n)
23 Info['node'] = nav.GetCurrentNode()
24 Info['path'] = n
25 tmp = Info['node'].GetVolume().GetShape()
26 Info['material'] = Info['node'].GetVolume().GetMaterial().GetName()
27 o = [tmp.GetOrigin()[0],tmp.GetOrigin()[1],tmp.GetOrigin()[2]]
28 Info['locorign'] = o
29 local = array('d',o)
30 globOrigin = array('d',[0,0,0])
31 nav.LocalToMaster(local,globOrigin)
32 Info['origin'] = globOrigin
33 shifts = [ [-tmp.GetDX()+o[0],o[1],o[2]],
34 [tmp.GetDX()+o[0],o[1],o[2]],
35 [o[0],-tmp.GetDY()+o[1],o[2]],
36 [o[0],tmp.GetDY()+o[1],o[2]],
37 [o[0],o[1],-tmp.GetDZ()+o[2]],[o[0],o[1],tmp.GetDZ()+o[2]]
38 ]
39 shifted = []
40 for s in shifts:
41 local = array('d',s)
42 glob = array('d',[0,0,0])
43 nav.LocalToMaster(local,glob)
44 shifted.append([glob[0],glob[1],glob[2]])
45 Info['boundingbox']={}
46 for j in range(3):
47 jmin = 1E30
48 jmax = -1E30
49 for s in shifted:
50 if s[j]<jmin: jmin = s[j]
51 if s[j]>jmax: jmax = s[j]
52 Info['boundingbox'][j]=[jmin,jmax]
53 return Info
54
55def add_info(path, node, level, currentlevel, print_sub_det_info=False):
56 sub_nodes = {}
57 fullInfo = {}
58 conditions_0={}
59 conditions_1={}
60 conditions={}
61 conditions_e0={}
62 conditions_p0={}
63 conditions_mu0={}
64 conditions_Scifi={}
65 for subnode in node.GetNodes():
66 name = subnode.GetName()
67 fullInfo[name] = local2Global(path + '/' + name)
68 sd = path[path.rfind('/')+1:] # want to know if we're inside a subdetector
69 if sd == path[1:]:
70 # it was the first
71 firstsd = True
72 else:
73 firstsd = False
74
75 if sd=="":
76 #print("path=",name,"sd=",sd)
77 conditionsDB.add_detector(name)
78 conditions= {'x':fullInfo[name]['boundingbox'][0],'y':fullInfo[name]['boundingbox'][1],'z':fullInfo[name]['boundingbox'][2]}
79 conditionsDB.add_condition(name, "xyz", "Geo", conditions,None,datetime.datetime.now(), datetime.datetime.max)
80 else:
81 if firstsd == False:
82 conditionsDB.add_detector(name , path[1:] )
83 if sd=="volVetoPlane_0" and name[:10]=="volVetoBar":
84 conditions_0[name]={'x':fullInfo[name]['boundingbox'][0],'y':fullInfo[name]['boundingbox'][1],'z':fullInfo[name]['boundingbox'][2]}
85 if name=="volVetoBar_6" :
86 #print ("adding conditions to",path[1:],"conditions_0",conditions_0)
87 conditionsDB.add_condition(path[1:], "barpositions", "Geo", conditions_0,None,datetime.datetime.now(), datetime.datetime.max)
88 if sd=="volVetoPlane_1" and name[:10]=="volVetoBar":
89 conditions_1[name]={'x':fullInfo[name]['boundingbox'][0],'y':fullInfo[name]['boundingbox'][1],'z':fullInfo[name]['boundingbox'][2]}
90 if name=="volVetoBar_6" :
91 #print ("adding conditions to",path[1:],"conditions_1",conditions_1)
92 conditionsDB.add_condition(path[1:], "barpositions", "Geo", conditions_1,None,datetime.datetime.now(), datetime.datetime.max)
93
94 if sd[:5]=="Brick" and name[:8]=="Emulsion":
95 conditions_e0[name]={'x':fullInfo[name]['boundingbox'][0],'y':fullInfo[name]['boundingbox'][1],'z':fullInfo[name]['boundingbox'][2]}
96 if name=="Emulsion_59" :
97 #print ("emulsion path",path[1:])
98 conditionsDB.add_condition(path[1:], "emulsionpositions", "Geo", conditions_e0,None,datetime.datetime.now(), datetime.datetime.max)
99
100 if sd[:5]=="Brick" and name[:10]=="volPassive":
101 conditions_p0[name]={'x':fullInfo[name]['boundingbox'][0],'y':fullInfo[name]['boundingbox'][1],'z':fullInfo[name]['boundingbox'][2]}
102 if name=="volPassive_58" :
103 #print ("tungsten path",path[1:])
104 conditionsDB.add_condition(path[1:], "tungsten positions", "Geo", conditions_p0,None,datetime.datetime.now(), datetime.datetime.max)
105
106 if sd[:13]=="volUpstreamDet" and name[:16]=="volMuUpstreamBar":
107 conditions_mu0[name]={'x':fullInfo[name]['boundingbox'][0],'y':fullInfo[name]['boundingbox'][1],'z':fullInfo[name]['boundingbox'][2]}
108 if name=="volMuUpstreamBar_hor_1009" :
109 #print ("emulsion path",path[1:])
110 conditionsDB.add_condition(path[1:], "mu upstream", "Geo", conditions_mu0,None,datetime.datetime.now(), datetime.datetime.max)
111
112 if sd[:15]=="volDownstreamDet" and name[:18]=="volMuDownstreamBar":
113 conditions_mu0[name]={'x':fullInfo[name]['boundingbox'][0],'y':fullInfo[name]['boundingbox'][1],'z':fullInfo[name]['boundingbox'][2]}
114 if name=="volMuDownstreamBar_ver_100059" :
115 #print ("emulsion path",path[1:])
116 conditionsDB.add_condition(path[1:], "mu downstream ", "Geo", conditions_mu0,None,datetime.datetime.now(), datetime.datetime.max)
117 if name[:5]=="Scifi":
118 conditions_Scifi[name]={'id':name[7:7]}
119 #print ("SciFi path",path[1:])
120 if name=="Scifi_4":
121 conditionsDB.add_condition(path[1:], "SciFi ", "Geo", conditions_Scifi,None,datetime.datetime.now(), datetime.datetime.max)
122 else:
123 #scifi structure not yet in geofile, add it by hand:
124 #print ("adding name",name,"sd",sd," to condb")
125 conditionsDB.add_detector(name , sd)
126 if name[:5]=="Scifi":
127 conditions_Scifi[name]={'id':name[6:]}
128 for l in range(2):
129 planesd="plane_"+str(l)
130 #print ("planesd",planesd,"name",name)
131 conditionsDB.add_detector( planesd,sd+"/"+name )
132 for m in range(3):
133 boardsd="board_"+str(m)
134 conditionsDB.add_detector(boardsd, sd+"/"+name+"/"+planesd)
135 for n in range(8):
136 tofpetsd="tofpet_"+str(n)
137 conditionsDB.add_detector(tofpetsd, sd+"/"+name+"/"+planesd+"/"+boardsd)
138 for o in range(64):
139 channelsd="channel_"+str(o)
140 conditionsDB.add_detector(channelsd, sd+"/"+name+"/"+planesd+"/"+boardsd+"/"+tofpetsd)
141
142 if name=="Scifi_4":
143 #print ("SciFi conditions=",conditions_Scifi)
144 conditionsDB.add_condition(path[1:], "SciFi ", "Geo", conditions_Scifi,None,datetime.datetime.now(), datetime.datetime.max)
145 else:
146 conditions= {'x':fullInfo[name]['boundingbox'][0],'y':fullInfo[name]['boundingbox'][1],'z':fullInfo[name]['boundingbox'][2]}
147 conditionsDB.add_condition(path[1:]+'/'+name, "xyz", "Geo", conditions,None,datetime.datetime.now(), datetime.datetime.max)
148
149
150 sub_nodes[name] = fullInfo[name]['origin'][2]
151 if currentlevel < level and fullInfo[name]['node'].GetNodes():
152 add_info(fullInfo[name]['path'], fullInfo[name]['node'], level, currentlevel + 1,
153 print_sub_det_info)
154
155 if currentlevel == 0:
156 print_sub_det_info = False
157
158# debug=0: data added to conddb
159# debug=1: data removed from conddb
160debug=0
161
162
163fname="~/snd-soft-23april-2021/sndsw/geofile_full.conical.Pythia8-TGeant4.root"
164fgeom = ROOT.TFile.Open(fname)
165fGeo = fgeom.FAIRGeom
166top = fGeo.GetTopVolume()
167
168currentlevel = 1
169#level=5 to get emulsion
170level = 5
171
172
173
174# Instantiate an API factory
175api_factory = APIFactory()
176# Call construct_DB_API to get an CDB API instance, the path must lead to a valid config.yml file containing the database configuration
177
178conditionsDB = api_factory.construct_DB_API("/home/eric/snd-soft-23april-2021/sndsw/conditionsDatabase/config.yml")
179
180#value_array = {"x": [5, 2, 6, 3, 7]}
181
182if debug == 0:
183 add_info("", top, int(level), currentlevel)
184
185# How to add a main detector to the database:
186#conditionsDB.add_detector("Tunnel_1")
187# How to add a subdetector to a parent detector in the database:
188# Params: (subdetector name, parent detector ID)
189#conditionsDB.add_detector("Veto" , "Tunnel")
190#conditionsDB.add_detector("Target" , "Tunnel")
191#conditionsDB.add_detector("Mufilter" , "Tunnel")
192
193
194
195# Show all main detector names in the database:
196result = conditionsDB.list_detectors()
197j=0
198#print("Level ",j," :",result)
199j+=1
200
201
202if 1==0:
203 results=[]
204 for sd in result:
205 conditions = conditionsDB.get_conditions_by_tag(sd,'Geo')
206 print("conditions of Geo detector",sd," :",conditions)
207 results.append(conditionsDB.list_detectors(sd))
208
209 #print("Level ",j," :",results)
210 j+=1
211
212 for sd in results:
213 resultss=[]
214 for ssd in sd:
215 if conditionsDB.list_detectors(ssd) != [] :
216 resultss.append(conditionsDB.list_detectors(ssd))
217 conditions = conditionsDB.get_conditions_by_tag(ssd,'Geo')
218 print("conditions of Geo sub detector",ssd," :",conditions)
219
220
221
222 print('Brick_0 conditions',result)
223 result = conditionsDB.get_conditions('volTarget_1/Wall_0/Row_0/Brick_1')
224 print('Brick_1 conditions',result)
225 result = conditionsDB.get_conditions('volTarget_1/Wall_0/Row_1/Brick_0')
226 print('Row 1 Brick_0 conditions',result)
227 result = conditionsDB.get_conditions('volTarget_1/Wall_0/Row_1/Brick_1')
228 print('Row_1 Brick 1 conditions',result)
229 result = conditionsDB.get_conditions('volTarget_1/Wall_1/Row_1/Brick_1')
230 print('Wall 1 Row_1 Brick 1 conditions',result)
231
232# Adding a condition to the database
233# Name-Tag and Name-CollectedAt combinations should be unique in the database
234# Dates can be parsed as a string: year-month-day hour:minutes:second OR as a datetime object
235#conditionsDB.add_condition("detector3/subdetector1", "conditionName1", "SampleTag", "2020-03-21 18:14", value_array, "testType", "2020-03-21 18:12", "2020-05-20")
236#conditionsDB.add_condition("detector3/subdetector1", "conditionsName1", "SampleTag2", datetime.datetime(2020,3,22,20,20), value_array, "testType", datetime.datetime(2020,3,23,18,12), datetime.datetime(2020,3,23,18,12))
237# A condition can also be added to the database without specifying the type, valid_since and/or valid_until, they will take the default values: None, datetime.datetime.now(), datetime.datetime.MAX respectively
238#conditionsDB.add_condition("detector3/subdetector1", "conditionsName1", "SampleTag3", datetime.datetime(2020,3,25,20,20), value_array)
239
240# Get detector dictionary by specifying the detectorID: "Detector/subdetector/subdetector ..."
241# This function gets subdetector: "subdetector1" of detector: "detector3"
242#result = conditionsDB.get_detector("detector3/subdetector1")
243#print(result)
244
245# Get a list of all conditions of a detector
246# This function gets a list of conditions in subdetector: "subdetector1" of detector: "detector3"
247#result = conditionsDB.get_conditions("detector3/subdetector1")
248#print(result)
249
250# Get a list of all conditions with a specific tag for the specified detector
251# This function gets a list of conditions in subdetector: "subdetector1" of detector: "detector3" WITH the tag "SampleTag"
252#result = conditionsDB.get_conditions_by_tag("detector3/subdetector1", "SampleTag")
253#print(result)
254
255# Get a condition dictionary by specifying the detectorID, condition name, condition tag
256# This function gets a condition in subdetector: "subdetector1" of detector: "detector3" WITH the name "conditionName1" AND the tag "SampleTag2"
257#result = conditionsDB.get_condition_by_name_and_tag("detector3/subdetector1", "conditionsName1", "SampleTag2")
258#print(result)
259
260# Get a condition dictionary by specifying the detectorID and condition collected_at
261# This function gets a condition in subdetector: "subdetector1" of detector: "detector3" WITH the name "conditionName1" AND the collected_at: 2020-03-21 18:14:00
262# If a certain time accuracy is omitted then defaults are assumed. See API documentation for details
263#conditionsDB.get_condition_by_name_and_collection_date("detector3/subdetector1", "conditionName1", "2020-03-21 18:14")
264
265# Get a list of conditions by specifying the detectorID, condition name, and date where it should be valid
266# This function gets a condition in subdetector: "subdetector1" of detector: "detector3" WITH the name "conditionName1" AND the valid_since <= 2020-03-22 00:00:00 AND the valid_until >= 2020-03-22 00:00:00
267#conditionsDB.get_conditions_by_name_and_validity("detector3/subdetector1", "conditionName1", "2020-03-22")
268
269# Update a condition type, valid_since and valid_until by specifying the detectorID, condition name and condition tag
270# This function sets the following values to the subdetector: "subdetector1" of detector: "detector3" WITH the name "conditionName1" AND the tag "SampleTag"
271# Values updated: Type: "testType2" valid_since: "2020-03-20 18:12:00" valid_until: stays the same
272#conditionsDB.update_condition_by_name_and_tag("detector3/subdetector1", "conditionName1", "SampleTag", "testType2", "2020-03-20 18:12")
273
274# EXECUTE WITH CARE: Removes a subdetector including all of it's values from the database
275# This function removes subdetector: "subdetector1" of detector: "detector3" and all of it's values from the database
276
277if 1==1:
278 if debug==1:
279 conditionsDB.remove_detector("Tunnel_1")
280 conditionsDB.remove_detector("volTarget_1")
281 conditionsDB.remove_detector("volVeto_1")
282 conditionsDB.remove_detector("volMuFilter_1")
This class creates an instance of the specified database API.
Definition factory.py:15
add_info(path, node, level, currentlevel, print_sub_det_info=False)
Definition snd_conDB.py:55