77import java .util .TreeSet ;
88import org .jlab .clara .std .services .EventWriterException ;
99import org .jlab .detector .calib .utils .ConstantsManager ;
10- import org .jlab .detector .decode .CLASDecoder4 ;
11- import org .jlab .detector .helicity .HelicitySequence ;
1210import org .jlab .detector .helicity .HelicitySequenceDelayed ;
13- import org .jlab .detector .helicity .HelicityState ;
14- import org .jlab .detector .scalers .DaqScalersSequence ;
11+ import org .jlab .detector .serial .SerialHoncho ;
1512import org .jlab .detector .serial .PostProcessor ;
1613import org .jlab .jnp .hipo4 .data .Bank ;
1714import org .jlab .jnp .hipo4 .data .Event ;
3330 */
3431public class Clas12Writer extends HipoToHipoWriter {
3532
36- static final String [] TAG1BANKS = {"RUN::scaler" ,"HEL::scaler" ,"RAW::scaler" ,"RAW::epics" ,"HEL::flip" ,"COAT::config" };
37-
38- Bank [] tag1banks ;
33+ SerialHoncho serial ;
3934 Bank runConfig ;
40- Bank helicityAdc ;
4135 ConstantsManager conman ;
42- TreeMap <Integer ,Integer > eventUnix ;
43- TreeSet <HelicityState > helicities ;
44- DaqScalersSequence scalers ;
4536 SchemaFactory fullSchema ;
4637 boolean postprocess ;
4738
4839 private void init (JSONObject opts ) {
4940 fullSchema = new SchemaFactory ();
5041 fullSchema .initFromDirectory (FileUtils .getEnvironmentPath ("CLAS12DIR" ,"etc/bankdefs/hipo4" ));
42+ serial = new SerialHoncho (fullSchema );
5143 runConfig = new Bank (fullSchema .getSchema ("RUN::config" ));
52- helicityAdc = new Bank (fullSchema .getSchema ("HEL::adc" ));
53- helicities = new TreeSet <>();
54- scalers = new DaqScalersSequence (fullSchema );
5544 conman = new ConstantsManager ();
56- eventUnix = new TreeMap <>();
5745 conman .init ("/runcontrol/hwp" ,"/runcontrol/helicity" );
5846 postprocess = opts .optBoolean ("postprocess" , false );
5947 if (opts .has ("variation" )) conman .setVariation (opts .getString ("variation" ));
6048 if (opts .has ("timestamp" )) conman .setTimeStamp (opts .getString ("timestamp" ));
61- tag1banks = new Bank [TAG1BANKS .length ];
62- for (int i =0 ; i <tag1banks .length ; ++i )
63- tag1banks [i ] = new Bank (fullSchema .getSchema (TAG1BANKS [i ]));
6449 }
6550
6651 @ Override
@@ -78,29 +63,17 @@ protected HipoWriterSorted createWriter(Path file, JSONObject opts) throws Event
7863
7964 @ Override
8065 protected void writeEvent (Object event ) throws EventWriterException {
81- scalers .add ((Event )event );
82- ((Event )event ).read (runConfig );
83- ((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- }
89- helicities .add (HelicityState .createFromFadcBank (helicityAdc , runConfig , conman ));
90- Event t = CLASDecoder4 .createTaggedEvent ((Event )event , runConfig , tag1banks );
66+ Event t = serial .read ((Event )event );
9167 if (!t .isEmpty ()) writer .addEvent (t , 1 );
9268 super .writeEvent (event );
9369 }
9470
9571 @ Override
9672 protected void closeWriter () {
97- HelicitySequence .writeFlips (fullSchema , writer , helicities );
98- writer .addEvent (getUnixEvent (runConfig ),1 );
73+ serial .finish (writer );
9974 super .closeWriter ();
10075 if (postprocess ) postprocess ();
101- // keep the latest helicity/scaler reading for the next file:
102- while (helicities .size () > 60 ) helicities .pollFirst ();
103- scalers .clear (10 );
76+ serial .clear ();
10477 }
10578
10679 /**
@@ -120,35 +93,14 @@ private int getRunNumber() {
12093 return 0 ;
12194 }
12295
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-
14496 /**
14597 * Copy helicity/charge tag-1 information to all events.
14698 */
14799 private void postprocess () {
148100 int d = conman .getConstants (getRunNumber (), "/runcontrol/helicity" ).getIntValue ("delay" ,0 ,0 ,0 );
149101 HelicitySequenceDelayed helicity = new HelicitySequenceDelayed (d );
150- helicity .addStream (helicities );
151- PostProcessor p = new PostProcessor (List .of (filename ), fullSchema , helicity , scalers );
102+ helicity .addStream (serial . getHelicities () );
103+ PostProcessor p = new PostProcessor (List .of (filename ), fullSchema , helicity , serial . getScalers () );
152104 HipoReader r = new HipoReader ();
153105 r .open (filename );
154106 Event e = new Event ();
0 commit comments