22
33import java .io .File ;
44import java .nio .file .Path ;
5+ import java .util .TreeMap ;
56import java .util .TreeSet ;
67import org .jlab .analysis .postprocess .Processor ;
78import org .jlab .clara .std .services .EventWriterException ;
@@ -38,6 +39,7 @@ public class DecoderWriter extends HipoToHipoWriter {
3839 Bank runConfig ;
3940 Bank helicityAdc ;
4041 ConstantsManager conman ;
42+ TreeMap <Integer ,Integer > eventUnix ;
4143 TreeSet <HelicityState > helicities ;
4244 DaqScalersSequence scalers ;
4345 SchemaFactory fullSchema ;
@@ -51,6 +53,7 @@ private void init(JSONObject opts) {
5153 helicities = new TreeSet <>();
5254 scalers = new DaqScalersSequence (fullSchema );
5355 conman = new ConstantsManager ();
56+ eventUnix = new TreeMap <>();
5457 conman .init ("/runcontrol/hwp" ,"/runcontrol/helicity" );
5558 postprocess = opts .optBoolean ("postprocess" , false );
5659 if (opts .has ("variation" )) conman .setVariation (opts .getString ("variation" ));
@@ -73,37 +76,37 @@ protected HipoWriterSorted createWriter(Path file, JSONObject opts) throws Event
7376 }
7477 }
7578
76- /**
77- * In addition to writing the incoming event, copies all tag-1 banks to new
78- * tag-1 events and writes them, and stores helicity/scaler readings for later.
79- * @param event
80- * @throws EventWriterException
81- */
8279 @ Override
8380 protected void writeEvent (Object event ) throws EventWriterException {
8481 scalers .add ((Event )event );
8582 ((Event )event ).read (runConfig );
8683 ((Event )event ).read (helicityAdc );
84+ if (runConfig .getRows () > 0 ) {
85+ int unix = runConfig .getInt ("unixtime" ,0 );
86+ int evno = runConfig .getInt ("event" ,0 );
87+ if (unix > 0 && evno > 0 ) eventUnix .put (evno , unix );
88+ }
8789 helicities .add (HelicityState .createFromFadcBank (helicityAdc , runConfig , conman ));
8890 Event t = CLASDecoder4 .createTaggedEvent ((Event )event , runConfig , tag1banks );
8991 if (!t .isEmpty ()) writer .addEvent (t , 1 );
9092 super .writeEvent (event );
9193 }
9294
93- /**
94- * In addition to closing the writer, creates and writes tag-1 events with
95- * HEL::flip bnks and clears old scaler/helicity readings.
96- */
9795 @ Override
9896 protected void closeWriter () {
9997 HelicitySequence .writeFlips (fullSchema , writer , helicities );
98+ writer .addEvent (getUnixEvent (runConfig ),1 );
10099 super .closeWriter ();
101100 if (postprocess ) postprocess ();
102101 // keep the latest helicity/scaler reading for the next file:
103102 while (helicities .size () > 60 ) helicities .pollFirst ();
104103 scalers .clear (10 );
105104 }
106-
105+
106+ /**
107+ * Get the first valid run number from a RUN::config bank.
108+ * @return run
109+ */
107110 private int getRunNumber () {
108111 Event e = new Event ();
109112 HipoReader r = new HipoReader ();
@@ -117,6 +120,27 @@ private int getRunNumber() {
117120 return 0 ;
118121 }
119122
123+ /**
124+ * Get a new event with a RUN::unix bank containing event-timestamp mapping,
125+ * and the latest RUN::config bank.
126+ * @param config
127+ * @return
128+ */
129+ private Event getUnixEvent (Bank config ) {
130+ Bank unix = new Bank (fullSchema .getSchema ("RUN::unix" ));
131+ unix .setRows (eventUnix .size ());
132+ int row = 0 ;
133+ for (int evno : eventUnix .keySet ()) {
134+ unix .putInt ("event" , row , evno );
135+ unix .putInt ("unixtime" ,row , eventUnix .get (evno ));
136+ row ++;
137+ }
138+ Event e = new Event ();
139+ e .write (config );
140+ e .write (unix );
141+ return e ;
142+ }
143+
120144 /**
121145 * Copy helicity/charge tag-1 information to all events.
122146 */
0 commit comments