1+ package org .jlab .detector .decode ;
2+
3+ import java .io .File ;
4+ import java .util .ArrayList ;
5+ import java .util .TreeSet ;
6+ import org .jlab .detector .helicity .HelicitySequence ;
7+ import org .jlab .detector .helicity .HelicityState ;
8+ import org .jlab .io .evio .EvioDataEvent ;
9+ import org .jlab .io .evio .EvioSource ;
10+ import org .jlab .jnp .hipo4 .data .Bank ;
11+ import org .jlab .jnp .hipo4 .data .Event ;
12+ import org .jlab .jnp .hipo4 .io .HipoWriterSorted ;
13+ import org .jlab .utils .benchmark .ProgressPrintout ;
14+ import org .jlab .utils .options .OptionParser ;
15+ import org .jlab .utils .system .ClasUtilsFile ;
16+
17+ /**
18+ * Wrapper of everything in CLAS12Decoder4's main method.
19+ *
20+ * @author baltzell
21+ */
22+ public class CLASDecoder4U extends CLASDecoder4 {
23+
24+ private EvioSource reader ;
25+ private HipoWriterSorted writer ;
26+ private Bank config ;
27+ private Bank helicity ;
28+ private ArrayList <String > filenames ;
29+ private TreeSet <HelicityState > helicities ;
30+ private ProgressPrintout progress ;
31+ private Double torus ;
32+ private Double solenoid ;
33+ private int runNumber ;
34+ private int maxEvents ;
35+ private int eventCounter ;
36+
37+ public CLASDecoder4U (OptionParser o ) {
38+ super ();
39+ init (o );
40+ }
41+
42+ public CLASDecoder4U (String ... filename ) {
43+ super ();
44+ OptionParser o = getOptionParser ();
45+ o .parse (filename );
46+ init (o );
47+ }
48+
49+ public static OptionParser getOptionParser () {
50+ OptionParser p = new OptionParser ("decoder" );
51+ p .setDescription ("CLAS12 Data Decoder" );
52+ p .addOption ("-n" , "-1" , "maximum number of events to process" );
53+ p .addOption ("-c" , "2" , "compression type (0-NONE, 1-LZ4 Fast, 2-LZ4 Best, 3-GZIP)" );
54+ p .addOption ("-r" , "-1" ,"run number in the header bank (-1 means use CODA run)" );
55+ p .addOption ("-t" , null ,"torus current in the header bank (null means use RCDB)" );
56+ p .addOption ("-s" , null ,"solenoid current in the header bank (null means use RCDB)" );
57+ p .addOption ("-x" , null ,"CCDB timestamp (MM/DD/YYYY-HH:MM:SS)" );
58+ p .addOption ("-V" ,"default" ,"CCDB variation" );
59+ p .addOption ("-o" ,null ,"output HIPO filename" );
60+ p .setRequiresInputList (true );
61+ return p ;
62+ }
63+
64+ public void disableProgressMeter () { progress =null ; }
65+
66+ /**
67+ * @return whether there's another event to get
68+ */
69+ public boolean hasNext () {
70+ if (maxEvents > 0 && eventCounter > maxEvents ) return false ;
71+ if (reader != null && reader .hasEvent ()) return true ;
72+ return !filenames .isEmpty ();
73+ }
74+
75+ /**
76+ * @return the next event
77+ */
78+ public Event getNext () {
79+ EvioDataEvent evio = getNextEvioEvent ();
80+ Event event = getDecodedEvent (evio , runNumber , eventCounter , torus , solenoid );
81+ if (writer != null ) process (event );
82+ if (progress != null ) progress .updateStatus ();
83+ if (++eventCounter %25000 == 0 ) System .gc ();
84+ return event ;
85+ }
86+
87+ /**
88+ * Close the writer, after writing HEL::flip banks.
89+ */
90+ public void close () {
91+ if (writer != null ) {
92+ HelicitySequence .writeFlips (writer , helicities );
93+ writer .close ();
94+ }
95+ }
96+
97+ /**
98+ * The command-line "decoder" program.
99+ * @param args
100+ */
101+ public static void main (String [] args ) {
102+
103+ // hijack arguments, when run from an IDE:
104+ if (System .console () == null && args .length == 0 ) {
105+ // delete output file, if necessary:
106+ File f = new File ("tmp.hipo" );
107+ if (f .exists ()) f .delete ();
108+ // setup decoder command-line options:
109+ args = new String []{"-o" ,"tmp.hipo" ,System .getenv ("HOME" )+"/data/clas_005038.evio.00001" };
110+ // try to find bankdefs:
111+ System .setProperty ("CLAS12DIR" , System .getenv ("HOME" )+"/sw/coatjava/dev/coatjava" );
112+ }
113+
114+ // parse command-line options:
115+ OptionParser opts = CLASDecoder4U .getOptionParser ();
116+ opts .parse (args );
117+
118+ // run the decoder:
119+ CLASDecoder4U decoder = new CLASDecoder4U (opts );
120+ while (decoder .hasNext ()) decoder .getNext ();
121+ decoder .close ();
122+ }
123+
124+ private EvioDataEvent getNextEvioEvent () {
125+ if (reader == null || !reader .hasEvent ()) {
126+ reader = new EvioSource ();
127+ reader .open (filenames .remove (0 ));
128+ }
129+ return (EvioDataEvent )reader .getNextEvent ();
130+ }
131+
132+ private void process (Event event ) {
133+ event .read (config );
134+ event .read (helicity );
135+ helicities .add (HelicityState .createFromFadcBank (helicity , config , detectorDecoder .scalerManager ));
136+ Event tag = createTaggedEvent (event , "RAW::epics" ,"RAW::scaler" ,"RUN::scaler" ,"HEL::scaler" );
137+ if (!tag .isEmpty ())
138+ writer .addEvent (tag , 1 );
139+ writer .addEvent (event ,0 );
140+ }
141+
142+ private void init (OptionParser o ){
143+ eventCounter = 0 ;
144+ writer = null ;
145+ filenames = new ArrayList <>(o .getInputList ());
146+ config = new Bank (schemaFactory .getSchema ("RUN::config" ));
147+ helicity = new Bank (schemaFactory .getSchema ("HEL::adc" ));
148+ helicities = new TreeSet <>();
149+ progress = new ProgressPrintout ();
150+ torus = o .getOption ("-t" ).getValue ()==null ?null :o .getOption ("-t" ).doubleValue ();
151+ solenoid = o .getOption ("-s" ).getValue ()==null ?null :o .getOption ("-s" ).doubleValue ();
152+ runNumber = o .getOption ("-r" ).intValue ();
153+ maxEvents = o .getOption ("-n" ).intValue ();
154+ if (runNumber > 0 ) setRunNumber (runNumber , true );
155+ if (o .getOption ("-x" ).getValue () != null )
156+ detectorDecoder .setTimestamp (o .getOption ("-x" ).stringValue ());
157+ if (o .getOption ("-v" ).getValue () != null )
158+ detectorDecoder .setVariation (o .getOption ("-v" ).stringValue ());
159+ if (o .getOption ("-o" ).getValue () != null ) {
160+ writer = new HipoWriterSorted ();
161+ writer .setCompressionType (o .getOption ("-c" ).intValue ());
162+ writer .getSchemaFactory ().initFromDirectory (ClasUtilsFile .getResourceDir ("CLAS12DIR" , "etc/bankdefs/hipo4" ));
163+ writer .open (o .getOption ("-o" ).stringValue ());
164+ }
165+ }
166+
167+ }
0 commit comments