Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: configuration file for run-group dependent settings #101

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions config/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
default: # default settings, applies to all run groups
fc_mode: 1
add_engines: # list of engines from `run.groovy`
- out_monitor
- out_CND
- out_CTOF

run_groups: # run-group dependent settings

B: # run group name, likely a single capital letter

# named data data-taking periods (spYY, suYY, faYY, wiYY), together with run ranges
runs:
fa18: [100, 200]
sp19: [400, 700]

# the elements of the following nodes follow a convention:
# - each element should be either:
# - `period`: refers to a named data-taking period; if `name==all` then it will use all runs in this run group
# - `range`: custom run range
# - `list`: list of specific run numbers (or a single run number)
# - `val` is the appropriate value, e.g., beam energy
# - given a run number, the first element that has a run range that includes this run number will be chosen;
# thus if you have specific overrides (e.g., a list of special runs), specify it first

# beam energy, for when it differs from RCDB
beam_energy:
- period: fa18 # specify by named data-taking period
val: 10.6
- range: [400, 500] # specific range of runs
val: 10.2
- range: [501, 700]
val: 10.4

# specify how to get the faraday cup charge
faraday_cup_mode:
- list: [401, 402, 403] # specific overrides first
val: 2
- range: [400, 500]
val: 1
- period: fa18
val: 0

# add additional engines for this run group
add_engines:
- period: all
val: [ out_BAND, dst_mon ]
6 changes: 6 additions & 0 deletions detectors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
<artifactId>gpars</artifactId>
<version>1.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.groovy/groovy-yaml -->
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-yaml</artifactId>
<version>4.0.13</version>
</dependency>
</dependencies>

</project>
122 changes: 122 additions & 0 deletions detectors/src/main/java/org/jlab/clas/timeline/util/Config.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.jlab.clas.timeline.util
import groovy.yaml.YamlSlurper

class Config {

// instance variables -------------------------------------

// config file vars
private String configFileName
private File configFile
private Object configTree
private YamlSlurper slurper

// run group and period
private int runNum
private Range<Integer> runNumRange
private String runGroup
private String runPeriod

// ------------------------------------------------------

/**
* @param configFileName the configuration file name
*/
public Config(String configFileName="config/timelines.yaml") {

// parse config file
try {
this.configFileName = configFileName
configFile = new File(configFileName)
slurper = new YamlSlurper()
configTree = slurper.parse(configFile)
} catch(Exception ex) {
ex.printStackTrace()
System.exit(100)
}

// check the config file
def checkNode = { name, tree, key ->
if(!tree.containsKey(key))
throw new Exception("$configFileName has no node '$key' in $name")
}
["default", "run_groups"].each {
checkNode("top-level", configTree, it)
}
configTree["run_groups"].each { runGroupIt, runGroupConfigTree ->
checkNode("run group '$runGroupIt'", runGroupConfigTree, "runs")
}

// initialize run period variables
resetRunPeriod()

}


/**
* Reset the run period variables
*/
private void resetRunPeriod() {
runNum = 0
runNumRange = 0..0
runGroup = "unknown"
runPeriod = "unknown"
}


/**
* Determine what run-dependent settings to use for specified run
* @param runNum the run number
*/
public void setRun(int runNum) {
this.runNum = runNum
try {
// if runNum is in the same run period as the previous call, do nothing
if(runNumRange.contains(runNum)) return
// reset run period variables
resetRunPeriod()
// search for the run period which contains this runNum
def found = false
configTree["run_groups"].find { runGroupIt, runGroupConfigTree ->
runGroupConfigTree["runs"].find { runPeriodIt, runNumRangeArr ->
Range<Integer> runNumRangeIt = runNumRangeArr[0]..runNumRangeArr[1]
if(runNumRangeIt.contains(runNum)) {
runNumRange = runNumRangeIt
runGroup = runGroupIt
runPeriod = runPeriodIt
found = true
}
return found
}
return found
}
if(!found)
System.err.println("ERROR: cannot find a run period which contains run $runNum")
} catch(Exception ex) {
ex.printStackTrace()
System.exit(100)
}
}


/**
* Return the full configuration tree
*/
public Object getConfigTree() {
return configTree
}


/**
* Print configuration for current run
*/
public void printConfig() {
System.out.println """
Configuration for run $runNum:
Run Group: $runGroup
Run Period: $runPeriod
Run Number Range: ${runNumRange.getFrom()} to ${runNumRange.getTo()}
"""
}

}
5 changes: 5 additions & 0 deletions test-config.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import org.jlab.clas.timeline.util.Config

Config C = new Config("config/template.yaml")
C.setRun(500)
C.printConfig()