1
1
package org .jlab .io .clara ;
2
2
3
+ import java .io .File ;
3
4
import java .nio .file .Path ;
4
5
import java .util .TreeSet ;
6
+ import org .jlab .analysis .postprocess .Processor ;
5
7
import org .jlab .clara .std .services .EventWriterException ;
6
8
import org .jlab .detector .calib .utils .ConstantsManager ;
7
9
import org .jlab .detector .decode .CLASDecoder4 ;
8
10
import org .jlab .detector .helicity .HelicitySequence ;
11
+ import org .jlab .detector .helicity .HelicitySequenceDelayed ;
9
12
import org .jlab .detector .helicity .HelicityState ;
10
13
import org .jlab .detector .scalers .DaqScalersSequence ;
11
14
import org .jlab .jnp .hipo4 .data .Bank ;
12
15
import org .jlab .jnp .hipo4 .data .Event ;
13
16
import org .jlab .jnp .hipo4 .data .SchemaFactory ;
17
+ import org .jlab .jnp .hipo4 .io .HipoReader ;
14
18
import org .jlab .jnp .hipo4 .io .HipoWriterSorted ;
15
19
import org .jlab .jnp .utils .file .FileUtils ;
16
20
import org .json .JSONObject ;
17
21
18
22
/**
19
- * A port of the standard "decoder" to a CLARA I/O service .
23
+ * Combined with DecoderReader, a port of the standard "decoder" to CLARA.
20
24
*
21
25
* 1. Converts EVIO to HIPO, translation tables, pulse extraction
22
26
* 2. Copies special banks on-the-fly to new tag-1 events
25
29
*
26
30
* @author baltzell
27
31
*/
28
- public class HipoToHipoTagWriter extends HipoToHipoWriter {
32
+ public class DecoderWriter extends HipoToHipoWriter {
29
33
30
- int tag = 1 ;
31
- String [] bankNames = {"RUN::scaler" ,"HEL::scaler" ,"RAW::scaler" ,"RAW::epics" ,"HEL::flip" ,"COAT::config" };
34
+ static final String [] TAG1BANKS = {"RUN::scaler" ,"HEL::scaler" ,"RAW::scaler" ,"RAW::epics" ,"HEL::flip" ,"COAT::config" };
32
35
33
- Bank [] banks ;
36
+ Bank [] tag1banks ;
34
37
Bank runConfig ;
35
38
Bank helicityAdc ;
36
39
ConstantsManager conman ;
37
40
TreeSet <HelicityState > helicities ;
38
41
DaqScalersSequence scalers ;
39
42
SchemaFactory fullSchema ;
40
- int runNumber ;
43
+ boolean postprocess ;
41
44
42
45
private void init (JSONObject opts ) {
43
- runNumber = 0 ;
46
+ postprocess = false ;
44
47
fullSchema = new SchemaFactory ();
45
48
fullSchema .initFromDirectory (FileUtils .getEnvironmentPath ("CLAS12DIR" ,"etc/bankdefs/hipo4" ));
46
49
runConfig = new Bank (fullSchema .getSchema ("RUN::config" ));
@@ -49,13 +52,12 @@ private void init(JSONObject opts) {
49
52
scalers = new DaqScalersSequence (fullSchema );
50
53
conman = new ConstantsManager ();
51
54
conman .init ("/runcontrol/hwp" ,"/runcontrol/helicity" );
55
+ if (opts .has ("postprocess" )) postprocess = opts .getBoolean ("postprocess" );
52
56
if (opts .has ("variation" )) conman .setVariation (opts .getString ("variation" ));
53
57
if (opts .has ("timestamp" )) conman .setTimeStamp (opts .getString ("timestamp" ));
54
- if (opts .has ("tag" )) tag = opts .getInt ("tag" );
55
- if (opts .has ("banks" )) bankNames = opts .getString ("banks" ).split ("," );
56
- banks = new Bank [bankNames .length ];
57
- for (int i =0 ; i <banks .length ; ++i )
58
- banks [i ] = new Bank (fullSchema .getSchema (bankNames [i ]));
58
+ tag1banks = new Bank [TAG1BANKS .length ];
59
+ for (int i =0 ; i <tag1banks .length ; ++i )
60
+ tag1banks [i ] = new Bank (fullSchema .getSchema (TAG1BANKS [i ]));
59
61
}
60
62
61
63
@ Override
@@ -71,31 +73,70 @@ protected HipoWriterSorted createWriter(Path file, JSONObject opts) throws Event
71
73
}
72
74
}
73
75
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
+ */
74
82
@ Override
75
83
protected void writeEvent (Object event ) throws EventWriterException {
76
84
scalers .add ((Event )event );
77
85
((Event )event ).read (runConfig );
78
- if (runConfig .getRows () > 0 ) {
79
- int r = runConfig .getInt ("run" ,0 );
80
- if (r > 0 ) runNumber = r ;
81
- }
82
86
((Event )event ).read (helicityAdc );
83
87
helicities .add (HelicityState .createFromFadcBank (helicityAdc , runConfig , conman ));
84
- Event t = CLASDecoder4 .createTaggedEvent ((Event )event , runConfig , banks );
85
- if (!t .isEmpty ()) writer .addEvent (t , tag );
88
+ Event t = CLASDecoder4 .createTaggedEvent ((Event )event , runConfig , tag1banks );
89
+ if (!t .isEmpty ()) writer .addEvent (t , 1 );
86
90
super .writeEvent (event );
87
91
}
88
92
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
+ */
89
97
@ Override
90
98
protected void closeWriter () {
91
99
HelicitySequence .writeFlips (fullSchema , writer , helicities );
92
- helicities .clear ();
93
- scalers .clear ();
94
100
super .closeWriter ();
101
+ if (postprocess ) postprocess ();
102
+ // keep the latest helicity/scaler reading for the next file:
103
+ while (helicities .size () > 60 ) helicities .pollFirst ();
104
+ scalers .clear (10 );
105
+ }
106
+
107
+ private int getRunNumber () {
108
+ Event e = new Event ();
109
+ HipoReader r = new HipoReader ();
110
+ r .open (filename );
111
+ while (r .hasNext ()) {
112
+ r .nextEvent (e );
113
+ e .read (runConfig );
114
+ if (runConfig .getRows ()>0 && runConfig .getInt ("run" ,0 )>0 )
115
+ return runConfig .getInt ("run" ,0 );
116
+ }
117
+ return 0 ;
95
118
}
96
119
97
- protected void closeRawWriter () {
98
- super .closeWriter ();
120
+ /**
121
+ * Copy helicity/charge tag-1 information to all events.
122
+ */
123
+ private void postprocess () {
124
+ int d = conman .getConstants (getRunNumber (), "/runcontrol/helicity" ).getIntValue ("delay" ,0 ,0 ,0 );
125
+ HelicitySequenceDelayed h = new HelicitySequenceDelayed (d );
126
+ h .addStream (helicities );
127
+ Processor p = new Processor (fullSchema , h , scalers );
128
+ HipoReader r = new HipoReader ();
129
+ r .open (filename );
130
+ Event e = new Event ();
131
+ writer .open ("pp_" +filename );
132
+ while (r .hasNext ()) {
133
+ r .nextEvent (e );
134
+ p .processEvent (e );
135
+ HipoToHipoWriter .writeEvent (writer , e , schemaBankList );
136
+ }
137
+ writer .close ();
138
+ new File (filename ).delete ();
139
+ new File ("pp_" +filename ).renameTo (new File (filename ));
99
140
}
100
141
101
142
}
0 commit comments