SND@LHC Software
Loading...
Searching...
No Matches
example_api_usage.py
Go to the documentation of this file.
6from ..factory import APIFactory
7import datetime
8
9
10# Instantiate an API factory
11api_factory = APIFactory()
12# Call construct_DB_API to get an CDB API instance, the path must lead to a valid config.yml file containing the database configuration
13conditionsDB = api_factory.construct_DB_API("/FairShip/conditionsDatabase/config.yml")
14
15value_array = {"x": [5, 2, 6, 3, 7]}
16
17# How to add a main detector to the database:
18conditionsDB.add_detector("detector3")
19# How to add a subdetector to a parent detector in the database:
20# Params: (subdetector name, parent detector ID)
21conditionsDB.add_detector("subdetector1" , "detector3")
22
23# Show all main detector names in the database:
24result = conditionsDB.list_detectors()
25print(result)
26# Show all subdetectors of a specific detector
27result = conditionsDB.list_detectors("detector3")
28print(result)
29
30# Adding a condition to the database
31# Name-Tag and Name-CollectedAt combinations should be unique in the database
32# Dates can be parsed as a string: year-month-day hour:minutes:second OR as a datetime object
33conditionsDB.add_condition("detector3/subdetector1", "conditionName1", "SampleTag", "2020-03-21 18:14", value_array, "testType", "2020-03-21 18:12", "2020-05-20")
34conditionsDB.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))
35# 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
36conditionsDB.add_condition("detector3/subdetector1", "conditionsName1", "SampleTag3", datetime.datetime(2020,3,25,20,20), value_array)
37
38# Get detector dictionary by specifying the detectorID: "Detector/subdetector/subdetector ..."
39# This function gets subdetector: "subdetector1" of detector: "detector3"
40result = conditionsDB.get_detector("detector3/subdetector1")
41print(result)
42
43# Get a list of all conditions of a detector
44# This function gets a list of conditions in subdetector: "subdetector1" of detector: "detector3"
45result = conditionsDB.get_conditions("detector3/subdetector1")
46print(result)
47
48# Get a list of all conditions with a specific tag for the specified detector
49# This function gets a list of conditions in subdetector: "subdetector1" of detector: "detector3" WITH the tag "SampleTag"
50result = conditionsDB.get_conditions_by_tag("detector3/subdetector1", "SampleTag")
51print(result)
52
53# Get a condition dictionary by specifying the detectorID, condition name, condition tag
54# This function gets a condition in subdetector: "subdetector1" of detector: "detector3" WITH the name "conditionName1" AND the tag "SampleTag2"
55result = conditionsDB.get_condition_by_name_and_tag("detector3/subdetector1", "conditionsName1", "SampleTag2")
56print(result)
57
58# Get a condition dictionary by specifying the detectorID and condition collected_at
59# 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
60# If a certain time accuracy is omitted then defaults are assumed. See API documentation for details
61conditionsDB.get_condition_by_name_and_collection_date("detector3/subdetector1", "conditionName1", "2020-03-21 18:14")
62
63# Get a list of conditions by specifying the detectorID, condition name, and date where it should be valid
64# 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
65conditionsDB.get_conditions_by_name_and_validity("detector3/subdetector1", "conditionName1", "2020-03-22")
66
67# Update a condition type, valid_since and valid_until by specifying the detectorID, condition name and condition tag
68# This function sets the following values to the subdetector: "subdetector1" of detector: "detector3" WITH the name "conditionName1" AND the tag "SampleTag"
69# Values updated: Type: "testType2" valid_since: "2020-03-20 18:12:00" valid_until: stays the same
70conditionsDB.update_condition_by_name_and_tag("detector3/subdetector1", "conditionName1", "SampleTag", "testType2", "2020-03-20 18:12")
71
72# EXECUTE WITH CARE: Removes a subdetector including all of it's values from the database
73# This function removes subdetector: "subdetector1" of detector: "detector3" and all of it's values from the database
74conditionsDB.remove_detector("detector3/subdetector1")
This class creates an instance of the specified database API.
Definition factory.py:15