-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get run group and period for a given run number
- Loading branch information
Showing
2 changed files
with
75 additions
and
17 deletions.
There are no files selected for viewing
86 changes: 73 additions & 13 deletions
86
detectors/src/main/java/org/jlab/clas/timeline/util/Config.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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""" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |