Skip to content

Commit

Permalink
feat: get run group and period for a given run number
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks committed Sep 22, 2023
1 parent ca1ba17 commit 9701a77
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 17 deletions.
86 changes: 73 additions & 13 deletions detectors/src/main/java/org/jlab/clas/timeline/util/Config.groovy
Original file line number Diff line number Diff line change
@@ -1,36 +1,96 @@
package org.jlab.clas.timeline.util
import groovy.yaml.YamlSlurper
import org.rcdb.*

class Config {

private String configFileName
private File configFile
private Object configTree
private YamlSlurper slurper

private int runNum
private String runGroup
private int runNum = 0
private String runGroup = "unknown"
private String runPeriod = "unknown"

/////////////////////////////////////////

/**
* @param configFileName the configuration file name
*/
public Config(String configFileName="config/timelines.yaml") {
configFile = new File(configFileName)
if(!configFile.exists())
throw new Exception("ERROR: config file $configFileName does not exist")
slurper = new YamlSlurper()
configTree = slurper.parse(configFile)

// 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")
}

}

/////////////////////////////////////////

public void setRun(int runNum_) {
runNum = runNum_
/**
* Determine what run-dependent settings to use for specified run
* @param runNum the run number
*/
public void setRun(int runNum) {

// find the run group and run period
this.runNum = runNum
try {
def found = false
configTree["run_groups"].find { runGroupIt, runGroupConfigTree ->
runGroupConfigTree["runs"].find { runPeriodIt, runRange ->
if(runNum >= runRange[0] && runNum <= runRange[1]) {
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"""
}

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

Config C = new Config("config/template.yaml")
println JsonOutput.prettyPrint(
JsonOutput.toJson(C.getConfigTree())
)
C.setRun(500)
C.printConfig()

0 comments on commit 9701a77

Please sign in to comment.