Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.TreeMap;
import java.util.TreeSet;
import org.jlab.analysis.postprocess.Processor;
import org.jlab.clara.std.services.EventWriterException;
import org.jlab.detector.calib.utils.ConstantsManager;
import org.jlab.detector.decode.CLASDecoder4;
import org.jlab.detector.helicity.HelicitySequence;
import org.jlab.detector.helicity.HelicitySequenceDelayed;
import org.jlab.detector.helicity.HelicityState;
import org.jlab.detector.scalers.DaqScalersSequence;
import org.jlab.detector.serial.SerialHoncho;
import org.jlab.jnp.hipo4.data.Bank;
import org.jlab.jnp.hipo4.data.Event;
import org.jlab.jnp.hipo4.data.SchemaFactory;
Expand All @@ -33,34 +28,22 @@
*/
public class Clas12Writer extends HipoToHipoWriter {

static final String[] TAG1BANKS = {"RUN::scaler","HEL::scaler","RAW::scaler","RAW::epics","HEL::flip","COAT::config"};

Bank[] tag1banks;
SerialHoncho serial;
Bank runConfig;
Bank helicityAdc;
ConstantsManager conman;
TreeMap<Integer,Integer> eventUnix;
TreeSet<HelicityState> helicities;
DaqScalersSequence scalers;
SchemaFactory fullSchema;
boolean postprocess;

private void init(JSONObject opts) {
fullSchema = new SchemaFactory();
fullSchema.initFromDirectory(FileUtils.getEnvironmentPath("CLAS12DIR","etc/bankdefs/hipo4"));
serial = new SerialHoncho(fullSchema);
runConfig = new Bank(fullSchema.getSchema("RUN::config"));
helicityAdc = new Bank(fullSchema.getSchema("HEL::adc"));
helicities = new TreeSet<>();
scalers = new DaqScalersSequence(fullSchema);
conman = new ConstantsManager();
eventUnix = new TreeMap<>();
conman.init("/runcontrol/hwp","/runcontrol/helicity");
postprocess = opts.optBoolean("postprocess", false);
if (opts.has("variation")) conman.setVariation(opts.getString("variation"));
if (opts.has("timestamp")) conman.setTimeStamp(opts.getString("timestamp"));
tag1banks = new Bank[TAG1BANKS.length];
for (int i=0; i<tag1banks.length; ++i)
tag1banks[i] = new Bank(fullSchema.getSchema(TAG1BANKS[i]));
}

@Override
Expand All @@ -78,29 +61,17 @@ protected HipoWriterSorted createWriter(Path file, JSONObject opts) throws Event

@Override
protected void writeEvent(Object event) throws EventWriterException {
scalers.add((Event)event);
((Event)event).read(runConfig);
((Event)event).read(helicityAdc);
if (runConfig.getRows() > 0) {
int unix = runConfig.getInt("unixtime",0);
int evno = runConfig.getInt("event",0);
if (unix > 0 && evno > 0) eventUnix.put(evno, unix);
}
helicities.add(HelicityState.createFromFadcBank(helicityAdc, runConfig, conman));
Event t = CLASDecoder4.createTaggedEvent((Event)event, runConfig, tag1banks);
Event t = serial.read((Event)event);
if (!t.isEmpty()) writer.addEvent(t, 1);
super.writeEvent(event);
}

@Override
protected void closeWriter() {
HelicitySequence.writeFlips(fullSchema, writer, helicities);
writer.addEvent(getUnixEvent(runConfig),1);
serial.finish(writer);
super.closeWriter();
if (postprocess) postprocess();
// keep the latest helicity/scaler reading for the next file:
while (helicities.size() > 60) helicities.pollFirst();
scalers.clear(10);
serial.clear();
}

/**
Expand All @@ -120,35 +91,14 @@ private int getRunNumber() {
return 0;
}

/**
* Get a new event with a RUN::unix bank containing event-timestamp mapping,
* and the latest RUN::config bank.
* @param config
* @return
*/
private Event getUnixEvent(Bank config) {
Bank unix = new Bank(fullSchema.getSchema("RUN::unix"));
unix.setRows(eventUnix.size());
int row = 0;
for (int evno : eventUnix.keySet()) {
unix.putInt("event", row, evno);
unix.putInt("unixtime",row, eventUnix.get(evno));
row++;
}
Event e = new Event();
e.write(config);
e.write(unix);
return e;
}

/**
* Copy helicity/charge tag-1 information to all events.
*/
private void postprocess() {
int d = conman.getConstants(getRunNumber(), "/runcontrol/helicity").getIntValue("delay",0,0,0);
HelicitySequenceDelayed helicity = new HelicitySequenceDelayed(d);
helicity.addStream(helicities);
Processor p = new Processor(List.of(filename), fullSchema, helicity, scalers);
helicity.addStream(serial.getHelicities());
Processor p = new Processor(List.of(filename), fullSchema, helicity, serial.getScalers());
HipoReader r = new HipoReader();
r.open(filename);
Event e = new Event();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.jlab.detector.serial;

import java.util.TreeMap;
import java.util.TreeSet;
import org.jlab.detector.calib.utils.ConstantsManager;
import org.jlab.detector.decode.CLASDecoder;
import org.jlab.detector.helicity.HelicitySequence;
import org.jlab.detector.helicity.HelicityState;
import org.jlab.detector.scalers.DaqScalersSequence;
import org.jlab.jnp.hipo4.data.Bank;
import org.jlab.jnp.hipo4.data.Event;
import org.jlab.jnp.hipo4.data.SchemaFactory;
import org.jlab.jnp.hipo4.io.HipoWriterSorted;

/**
*
* @author baltzell
*/
public class SerialHoncho {

static final String[] TAG1BANKS = {"RUN::scaler","HEL::scaler","RAW::scaler","RAW::epics","HEL::flip","COAT::config"};

SchemaFactory schema;
Bank[] tag1banks;
Bank runConfig;
Bank helicityAdc;
ConstantsManager conman;
TreeMap<Integer,Integer> eventUnix;
TreeSet<HelicityState> helicities;
DaqScalersSequence scalers;

public SerialHoncho(SchemaFactory schema) {
this.schema = schema;
conman = new ConstantsManager();
conman.init("/runcontrol/hwp","/runcontrol/helicity");
runConfig = new Bank(schema.getSchema("RUN::config"));
helicityAdc = new Bank(schema.getSchema("HEL::adc"));
helicities = new TreeSet<>();
scalers = new DaqScalersSequence(schema);
eventUnix = new TreeMap<>();
tag1banks = new Bank[TAG1BANKS.length];
for (int i=0; i<tag1banks.length; ++i)
tag1banks[i] = new Bank(schema.getSchema(TAG1BANKS[i]));
}

public synchronized Event read(Event event) {
scalers.add(event);
event.read(runConfig);
event.read(helicityAdc);
if (runConfig.getRows() > 0) {
int unix = runConfig.getInt("unixtime",0);
int evno = runConfig.getInt("event",0);
if (unix > 0 && evno > 0) eventUnix.put(evno, unix);
}
helicities.add(HelicityState.createFromFadcBank(helicityAdc, runConfig, conman));
return CLASDecoder.createTaggedEvent(event, runConfig, tag1banks);
}

public void finish(HipoWriterSorted writer) {
writer.addEvent(getUnixEvent(runConfig),1);
HelicitySequence.writeFlips(schema, writer, helicities);
}

public void clear() {
while (helicities.size() > 100) helicities.pollFirst();
scalers.clear(100);
}

Event getUnixEvent(Bank config) {
Bank unix = new Bank(schema.getSchema("RUN::unix"));
unix.setRows(eventUnix.size());
int row = 0;
for (int evno : eventUnix.keySet()) {
unix.putInt("event", row, evno);
unix.putInt("unixtime",row, eventUnix.get(evno));
row++;
}
Event e = new Event();
e.write(config);
e.write(unix);
return e;
}

public DaqScalersSequence getScalers() {
return scalers;
}

public TreeSet<HelicityState> getHelicities() {
return helicities;
}

public ConstantsManager getConstantsManager() {
return conman;
}

public SchemaFactory getSchemaFactory() {
return schema;
}
}
Loading