11package org .jlab .clas .reco ;
22
3- import java .io .BufferedReader ;
4- import java .io .IOException ;
5- import java .io .InputStream ;
6- import java .io .InputStreamReader ;
73import java .nio .ByteBuffer ;
84import java .nio .ByteOrder ;
9- import java .nio .charset .StandardCharsets ;
105import java .util .ArrayList ;
116import java .util .Arrays ;
127import java .util .LinkedHashMap ;
1611import java .util .concurrent .ConcurrentLinkedQueue ;
1712import java .util .concurrent .atomic .AtomicBoolean ;
1813import java .util .concurrent .atomic .AtomicInteger ;
19- import java .util .logging .Level ;
2014import java .util .logging .Logger ;
21- import org .jlab .clara .engine .EngineData ;
22- import org .jlab .clara .engine .EngineDataType ;
2315import org .jlab .coda .jevio .EvioException ;
2416import org .jlab .detector .decode .CLASDecoder ;
2517import org .jlab .detector .decode .CLASDecoderPool ;
@@ -118,17 +110,17 @@ void launch(int[] threads, String output, String... input) {
118110
119111 // perform scaling test:
120112 if (threads .length > 1 ) {
121- while (writeEvents < 100 ) sleep (1000 );
113+ while (writeEvents < 100 ) ReconUtil . sleep (1000 );
122114 CompletableFuture .runAsync (() -> { rethread (BENCH_SECONDS ,threads ); }).join ();
123115 reset ();
124116 }
125117
126118 // wait for finish:
127119 while (!writerThread .isDone ()) {
128- sleep (5000 );
129120 for (CompletableFuture f : decoThreads ) if (f .isDone ()) decoThreads .remove (f );
130121 for (CompletableFuture f : procThreads ) if (f .isDone ()) procThreads .remove (f );
131- if (true ) show ();
122+ ReconUtil .sleep (1000 );
123+ //show();
132124 }
133125 }
134126
@@ -151,7 +143,7 @@ void read(int threads, String... input) {
151143 if (reader != null ) {
152144
153145 // sleep instead of overfilling the read queue (100K events, ~2GB):
154- if (readEvents > 1e5 ) sleep (1000 );
146+ if (readEvents > 1e5 ) ReconUtil . sleep (1000 );
155147
156148 // read next event into chunk, and fill queue if chunk full:
157149 else output = read (output );
@@ -189,7 +181,7 @@ void decode(int thread) {
189181 if (input == null ) {
190182 if (decoQueue .isEmpty () && readerThread .isDone () && decoQueue .isEmpty ())
191183 break ;
192- sleep (100 );
184+ ReconUtil . sleep (100 );
193185 }
194186 else {
195187 List <HipoDataEvent > output = new ArrayList <>(input .size ());
@@ -223,7 +215,7 @@ void decode(int thread) {
223215
224216 void updateHelicity () {
225217 paused .set (true );
226- sleep (1000 );
218+ ReconUtil . sleep (1000 );
227219 synchronized (serialLock ) {
228220 serial .updateHelicitySequence ();
229221 }
@@ -246,7 +238,7 @@ void process(int thread) {
246238 if (writeEvents +skipEvents +failEvents >= readEvents +taggedEvents .get ())
247239 break ;
248240 }
249- sleep (100 );
241+ ReconUtil . sleep (100 );
250242 }
251243 else {
252244 //if (rethreadThread != null && !rethreadThread.isDone()) readQueue.offer(o);
@@ -280,15 +272,15 @@ void write(String output) {
280272 close ();
281273 break ;
282274 }
283- sleep (1000 );
275+ ReconUtil . sleep (1000 );
284276 }
285277 else {
286278 for (int i =0 ; i <e .size (); i ++) {
287- while (paused .get ()) sleep (100 );
279+ while (paused .get ()) ReconUtil . sleep (100 );
288280 Benchmark .getInstance ().resume ("post" );
289- synchronized (serialLock ) {
290- serial .process (e .get (i ));
291- }
281+ // synchronized (serialLock) {
282+ // serial.process(e.get(i));
283+ // }
292284 Benchmark .getInstance ().pause ("post" );
293285 Benchmark .getInstance ().resume ("write" );
294286 if (writer != null ) {
@@ -326,8 +318,8 @@ void rethread(int seconds, int... threads) {
326318 final int k = j ;
327319 procThreads .offer (CompletableFuture .runAsync (() -> { process (k ); }));
328320 }
329- while (progress .getNumberOfCalls () < 100 ) sleep (1000 );
330- sleep (seconds *1000 );
321+ while (progress .getNumberOfCalls () < 100 ) ReconUtil . sleep (1000 );
322+ ReconUtil . sleep (seconds *1000 );
331323 System .out .println (String .format ("\n ~~~~~~~~~ Rethreading Count: %d ~~~~~~~~~\n " ,threads [i ]));
332324 System .out .println (progress .getUpdateString ());
333325 System .out .println (Benchmark .getInstance ());
@@ -375,35 +367,15 @@ void open(String filename) {
375367 * @param yaml the configuration
376368 */
377369 HipoWriterSorted open (String filename , ClaraYaml yaml ) {
378- HipoWriterSorted w = new HipoWriterSorted ();
379- w .setCompressionType (2 );
380- String d = ClasUtilsFile .getResourceDir ("CLAS12DIR" , "etc/bankdefs/hipo4" );
381- if (yaml != null && yaml .getSchemaDirectory () != null ) d = yaml .getSchemaDirectory ();
382- if (!parser .getOption ("-S" ).isDefault ()) d = parser .getOption ("-S" ).stringValue ();
383- SchemaFactory s = new SchemaFactory ();
384- s .initFromDirectory (d );
385- if (yaml != null ) {
386- JSONObject json = yaml .filter ("writer" );
387- if (json .has ("wildcard" )) {
388- SchemaFactory s2 = s .reduce (json .getString ("wildcard" ));
389- w .getSchemaFactory ().copy (s2 );
390- }
391- else w .getSchemaFactory ().copy (s );
392- schemaBankList = new ArrayList <>();
393- if (json .has ("wildcard" )) {
394- if (json .optBoolean ("schema_filter" ,true )) {
395- int schemaSize = w .getSchemaFactory ().getSchemaList ().size ();
396- for (int i =0 ; i <schemaSize ; i ++) {
397- Bank dataBank = new Bank (w .getSchemaFactory ().getSchemaList ().get (i ));
398- schemaBankList .add (dataBank );
399- }
400- }
401- }
402- }
403- w .open (filename );
404- return w ;
370+ HipoWriterSorted writer = new HipoWriterSorted ();
371+ writer .setCompressionType (2 );
372+ SchemaFactory s = ReconUtil .getSchemaFactory (parser , yaml );
373+ writer .getSchemaFactory ().copy (s );
374+ schemaBankList = ReconUtil .getBankList (s , yaml );
375+ writer .open (filename );
376+ return writer ;
405377 }
406-
378+
407379 /**
408380 * Read the next event into the chunk, and, if it's full, queue the chunk
409381 * and make a new one.
@@ -422,7 +394,7 @@ List<Object> read(List<Object> chunk) {
422394 }
423395 else {
424396 Event event = new Event ();
425- o = ((HipoReader )reader ).getEvent (event , ++ fileEvents );
397+ o = ((HipoReader )reader ).getEvent (event , fileEvents ++ );
426398 }
427399 if (o != null && (skipEvents < 1 || readEvents > skipEvents )) {
428400 chunk .add (o );
@@ -440,11 +412,11 @@ List<Object> read(List<Object> chunk) {
440412 * Close the output file.
441413 */
442414 void close () {
443- serial .closure (writer );
415+ // serial.closure(writer);
444416 writer .close ();
445417 System .out .println (Benchmark .getInstance ());
446418 System .out .println (String .format ("recon-mutil :: read/write/tagged/diff = %d/%d/%d/%d" ,
447- readEvents , writeEvents , taggedEvents .get (), writeEvents -readEvents ));
419+ readEvents , writeEvents , taggedEvents .get (), writeEvents -readEvents - taggedEvents . get () ));
448420 }
449421
450422 /**
@@ -483,30 +455,22 @@ void init(OptionParser parser) {
483455 yaml = new ClaraYaml (parser .getOption ("-y" ).stringValue ());
484456 for (JSONObject service : yaml .services ()) {
485457 JSONObject cfg = yaml .filter (service .getString ("name" ));
486- if (cfg .length () > 0 ) addEngine (engines , service .getString ("name" ), service .getString ("class" ), cfg );
487- else addEngine (engines , service .getString ("name" ), service .getString ("class" ), null );
458+ if (cfg .length () > 0 ) ReconUtil . addEngine (engines , service .getString ("name" ), service .getString ("class" ), cfg );
459+ else ReconUtil . addEngine (engines , service .getString ("name" ), service .getString ("class" ), null );
488460 }
489461 }
490462 else if (!parser .getOption ("-c" ).isDefault ()) {
491463 for (String clazz : parser .getOption ("-c" ).stringValue ().split ("," ))
492- addEngine (engines , null , clazz , null );
464+ ReconUtil . addEngine (engines , null , clazz , null );
493465 }
494466 else {
495- InputStream is = ReconMutil .class .getClassLoader ().getResourceAsStream ("org/jlab/clas/reco/services.txt" );
496- BufferedReader br = new BufferedReader (new InputStreamReader (is , StandardCharsets .UTF_8 ));
497- try {
498- for (String line ; (line =br .readLine ()) != null ;)
499- addEngine (engines , line .split (" " )[0 ],line .split (" " )[1 ],null );
500- } catch (IOException ex ) {
501- System .getLogger (ReconMutil .class .getName ()).log (System .Logger .Level .ERROR , (String ) null , ex );
502- }
467+ for (String line : ReconUtil .readResourceLines ("org/jlab/clas/reco/services.txt" ))
468+ ReconUtil .addEngine (engines , line .split (" " )[0 ],line .split (" " )[1 ],null );
503469 }
504470 if (!parser .getOption ("-B" ).isDefault ()) {
505- ReconstructionEngine bg = addEngine (engines , "BG" ,"org.jlab.service.bg.BackgroundEngine" ,null );
471+ ReconstructionEngine bg = ReconUtil . addEngine (engines , "BG" ,"org.jlab.service.bg.BackgroundEngine" ,null );
506472 bg .engineConfigMap .put ("filename" ,parser .getOption ("-B" ).stringValue ());
507473 }
508- if (!parser .getOption ("-S" ).isDefault ()) {
509- }
510474 }
511475
512476 /**
@@ -522,45 +486,6 @@ void show() {
522486 System .out .println ("recon-mutil:: " +s1 +" " +s2 +" " +s3 );
523487 }
524488
525- /**
526- * Add a new engine to the list.
527- * @param label display name
528- * @param clazz full class name
529- * @param cfg engine configuration
530- * @return
531- */
532- static ReconstructionEngine addEngine (Map <String ,ReconstructionEngine > engines , String label , String clazz , JSONObject cfg ) {
533- ReconstructionEngine engine = null ;
534- try {
535- Class c = Class .forName (clazz );
536- if (ReconstructionEngine .class .isAssignableFrom (c )==true ){
537- engine = (ReconstructionEngine ) c .newInstance ();
538- if (cfg != null && !cfg .toString ().equals ("null" )) {
539- EngineData input = new EngineData ();
540- input .setData (EngineDataType .JSON .mimeType (), cfg .toString ());
541- engine .configure (input );
542- }
543- else engine .init ();
544- engines .put (label == null ? engine .getName () : label , engine );
545- }
546- else Logger .getLogger (ReconMutil .class .getPackage ().getName ())
547- .log (clazz .contains ("DecoderEngine" ) ? Level .INFO : Level .SEVERE ,
548- "Class is not a reconstruction engine : {0}" , clazz );
549- } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex ) {
550- Logger .getLogger (ReconMutil .class .getPackage ().getName ()).log (Level .SEVERE , null , ex );
551- }
552- return engine ;
553- }
554-
555- /**
556- * Catch interruptions in sleep.
557- * @param milliseconds
558- */
559- static void sleep (int milliseconds ) {
560- try { Thread .sleep (milliseconds ); }
561- catch (InterruptedException ex ) {}
562- }
563-
564489 /**
565490 * The command-line entry-point known as "recon-mutil".
566491 * @param args command-line arguments
0 commit comments