Skip to content

Commit

Permalink
Merge branch 'development' into DCBetaTimeWalk
Browse files Browse the repository at this point in the history
  • Loading branch information
ziegler committed Sep 5, 2024
2 parents e5ab05d + 47c77eb commit c44a1ff
Show file tree
Hide file tree
Showing 16 changed files with 755 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ private void printOrders() {
public void mergeEvents(DataEvent event, DataEvent bg1, DataEvent bg2) {

if(!event.hasBank("RUN::config") || !bg1.hasBank("RUN::config") || !bg2.hasBank("RUN::config")) {
System.out.println("Missing RUN::config bank");
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package org.jlab.analysis.postprocess;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.PathMatcher;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.jlab.logging.DefaultLogger;

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.HipoReader;
import org.jlab.io.base.DataBank;
import org.jlab.io.base.DataEvent;
import org.jlab.io.hipo.HipoDataBank;

import org.jlab.detector.calib.utils.ConstantsManager;
import org.jlab.detector.scalers.DaqScalers;
import org.jlab.detector.scalers.DaqScalersSequence;
import org.jlab.detector.helicity.HelicityBit;
import org.jlab.detector.helicity.HelicitySequenceDelayed;

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

static final Logger logger = Logger.getLogger(Util.class.getName());

public static final String CCDB_TABLES[] = {"/runcontrol/fcup","/runcontrol/slm",
"/runcontrol/helicity","/daq/config/scalers/dsc1","/runcontrol/hwp"};
public static final String DEF_PRELOAD_GLOB = "*.{hipo,h5}";

private final String outputPrefix = "tmp_";

private boolean initialized;
private ConstantsManager conman = null;
private SchemaFactory schemaFactory = null;
private DaqScalersSequence chargeSequence = null;
private HelicitySequenceDelayed helicitySequence = null;

public Processor(File file, boolean restream) {
configure(restream, Arrays.asList(file.getAbsolutePath()));
}

public Processor(String dir, boolean restream) {
configure(restream, findPreloadFiles(dir,DEF_PRELOAD_GLOB));
}

public Processor(String dir, String glob, boolean restream) {
configure(restream, findPreloadFiles(dir,glob));
}

private void configure(boolean restream, List<String> preloadFiles) {
if (preloadFiles.isEmpty()) {
logger.warning("<<<< No preload files found, postprocessing disabled.");
initialized = false;
} else {
HipoReader r = new HipoReader();
r.open(preloadFiles.get(0));
conman = new ConstantsManager();
conman.init(CCDB_TABLES);
schemaFactory = r.getSchemaFactory();
helicitySequence = Util.getHelicity(preloadFiles, schemaFactory, restream, conman);
chargeSequence = DaqScalersSequence.readSequence(preloadFiles);
r.close();
initialized = true;
}
}

/**
* Get a list of files to preload, from one directory and a glob.
* @param dir
* @param glob
* @return list of preload files
*/
private static List<String> findPreloadFiles(String dir, String glob) {
List<String> ret = new ArrayList<>();
if (dir != null) {
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:"+dir+"/"+glob);
for (File f : (new File(dir)).listFiles()) {
if (matcher.matches(f.toPath()))
ret.add(f.getPath());
}
}
return ret;
}

/**
* Modify REC::Event/HEL::scaler with the delay-corrected helicity
* @param event
* @param runConfig
* @param recEvent
*/
private void processEventHelicity(DataEvent event, DataBank runConfig, DataBank recEvent) {
HelicityBit hb = helicitySequence.search(runConfig.getLong("timestamp", 0));
HelicityBit hbraw = helicitySequence.getHalfWavePlate() ? HelicityBit.getFlipped(hb) : hb;
recEvent.setByte("helicity",0,hb.value());
recEvent.setByte("helicityRaw",0,hbraw.value());
DataBank helScaler = event.getBank("HEL::scaler");
if (helScaler.rows()>0) {
event.removeBank("HEL::scaler");
Util.assignScalerHelicity(runConfig.getLong("timestamp",0), ((HipoDataBank)helScaler).getBank(), helicitySequence);
event.appendBank(helScaler);
}
}

/**
* Modify REC::Event/HEL::scaler with the delay-corrected helicity
* @param event
* @param runConfig
* @param recEvent
*/
private void processEventHelicity(Event event, Bank runConfig, Bank recEvent) {
HelicityBit hb = helicitySequence.search(runConfig.getLong("timestamp", 0));
HelicityBit hbraw = helicitySequence.getHalfWavePlate() ? HelicityBit.getFlipped(hb) : hb;
recEvent.setByte("helicity",0,hb.value());
recEvent.setByte("helicityRaw",0,hbraw.value());
Bank helScaler = new Bank(schemaFactory.getSchema("HEL::scaler"));
event.read(helScaler);
if (helScaler.getRows()>0) {
event.remove(schemaFactory.getSchema("HEL::scaler"));
Util.assignScalerHelicity(runConfig.getLong("timestamp",0), helScaler, helicitySequence);
event.write(helScaler);
}
}

/**
* Modify REC::Event for beam charge and livetime
* @param runConfig
* @param recEvent
*/
private void processEventScalers(DataBank runConfig, DataBank recEvent) {
DaqScalers ds = chargeSequence.get(runConfig.getLong("timestamp", 0));
if (ds != null) {
recEvent.setFloat("beamCharge",0, (float) ds.dsc2.getBeamChargeGated());
recEvent.setDouble("liveTime",0,ds.dsc2.getLivetime());
}
}

/**
* Modify REC::Event for beam charge and livetime
* @param runConfig
* @param recEvent
*/
private void processEventScalers(Bank runConfig, Bank recEvent) {
DaqScalers ds = chargeSequence.get(runConfig.getLong("timestamp", 0));
if (ds != null) {
recEvent.putFloat("beamCharge",0, (float) ds.dsc2.getBeamChargeGated());
recEvent.putDouble("liveTime",0,ds.dsc2.getLivetime());
}
}

/**
* Postprocess one event
* @param e
*/
public void processEvent(DataEvent e) {
if (!initialized) return;
if (!e.hasBank("RUN::config")) return;
if (!e.hasBank("REC::Event")) return;
DataBank runConfig = e.getBank("RUN::config");
DataBank recEvent = e.getBank("REC::Event");
if (runConfig.rows()<1 || recEvent.rows()<1) return;
e.removeBank("REC::Event");
if (helicitySequence != null) processEventHelicity(e, runConfig, recEvent);
if (chargeSequence != null) processEventScalers(runConfig, recEvent);
e.appendBank(recEvent);
}

/**
* Postprocess one event
* @param e
*/
public void processEvent(Event e) {
if (!initialized) return;
if (!e.hasBank(schemaFactory.getSchema("RUN::config"))) return;
if (!e.hasBank(schemaFactory.getSchema("REC::Event"))) return;
Bank runConfig = new Bank(schemaFactory.getSchema("RUN::config"));
Bank recEvent = new Bank(schemaFactory.getSchema("REC::Event"));
e.read(runConfig);
e.read(recEvent);
if (runConfig.getRows()<1 || recEvent.getRows()<1) return;
e.remove(schemaFactory.getSchema("REC::Event"));
if (helicitySequence != null) processEventHelicity(e, runConfig, recEvent);
if (chargeSequence != null) processEventScalers(runConfig, recEvent);
e.write(recEvent);
}

/**
* Create rebuilt files from preload files.
* @param preloadFiles
* @return map of rebuilt:preload files
*/
private Map<String,String> rebuild(String dir, List<String> preloadFiles) {
File d = new File(dir);
if (!d.canWrite()) {
throw new RuntimeException("No write permissions on "+dir);
}
Map<String,String> rebuiltFiles = new HashMap<>();
for (String preloadFile : preloadFiles) {
String rebuiltFile = dir+"/"+outputPrefix+preloadFile.replace(dir+"/","");
Util.rebuildScalers(conman, preloadFile, rebuiltFile);
rebuiltFiles.put(rebuiltFile,preloadFile);
}
return rebuiltFiles;
}

/**
* Replace files with new ones.
* @param files map of new:old filenames
*/
private static void replace(Map<String,String> files) {
for (String rebuiltFile : files.keySet()) {
new File(files.get(rebuiltFile)).delete();
new File(rebuiltFile).renameTo(new File(files.get(rebuiltFile)));
}
}

/**
* Replace preload files with rebuilt ones.
*/
private void rebuildAndReplace(List<String> preloadFiles) {
replace(rebuild(".",preloadFiles));
}

public static void main(String args[]) {
DefaultLogger.debug();
Processor p = new Processor(System.getenv("HOME")+"/tmp","r*.hipo",false);
//p.rebuildAndReplace();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.List;
import org.jlab.detector.calib.utils.ConstantsManager;
import org.jlab.detector.calib.utils.RCDBConstants;
import org.jlab.detector.helicity.HelicitySequence;
import org.jlab.detector.scalers.DaqScalers;
import org.jlab.detector.helicity.HelicitySequenceManager;
import org.jlab.jnp.hipo4.data.Bank;
Expand Down Expand Up @@ -111,7 +110,7 @@ public static void main(String[] args) {
runScalerBank = ds.createRunBank(writer.getSchemaFactory());
helScalerBank = ds.createHelicityBank(writer.getSchemaFactory());

RebuildScalers.assignScalerHelicity(event, helScalerBank, helSeq);
Util.assignScalerHelicity(event, helScalerBank, helSeq);

// put modified HEL/RUN::scaler back in the event:
event.write(runScalerBank);
Expand All @@ -125,65 +124,4 @@ public static void main(String[] args) {
writer.close();
}

/**
* Assign the delay-corrected helicity to the HEL::scaler bank's rows
* @param event the event containing the scaler reading
* @param bank the HEL::scaler bank
* @param seq previously initialized helicity sequence
*/
public static void assignScalerHelicity(Event event, Bank bank, HelicitySequenceManager seq) {

// Struck (helicity) scaler readout is always slightly after the helicity
// state change, i.e., as registered in the FADCs, so its true helicity
// is offset by one state from its event:
final int readoutStateOffset = -1;

// Rows in the HEL::scaler bank correspond to the most recent, consecutive,
// time-ordered, T-stable intervals. The first row is the earliest in
// time, and the last row is the latest. Here we loop over them:
for (int row=0; row<bank.getRows(); ++row) {

// This is the helicity state offset for this HEL::scaler row, where
// the last row has an offset of -1:
final int offset = bank.getRows() - row - 1 + readoutStateOffset;

// Assign delay-corrected helicity to this HEL::scaler row:
bank.putByte("helicity",row,seq.search(event,offset).value());
if (seq.getHalfWavePlate(event))
bank.putByte("helicityRaw",0,(byte)(-1*seq.search(event,offset).value()));
else
bank.putByte("helicityRaw",0,seq.search(event,offset).value());
}
}

/**
* Assign the delay-corrected helicity to the HEL::scaler bank's rows
* @param timestamp event TI timestamp, e.g., from RUN::config bank
* @param bank the HEL::scaler bank
* @param seq previously initialized helicity sequence
*/
public static void assignScalerHelicity(Long timestamp, Bank bank, HelicitySequence seq) {

// Struck (helicity) scaler readout is always slightly after the helicity
// state change, i.e., as registered in the FADCs, so its true helicity
// is offset by one state from its event:
final int readoutStateOffset = -1;

// Rows in the HEL::scaler bank correspond to the most recent, consecutive,
// time-ordered, T-stable intervals. The first row is the earliest in
// time, and the last row is the latest. Here we loop over them:
for (int row=0; row<bank.getRows(); ++row) {

// This is the helicity state offset for this HEL::scaler row, where
// the last row has an offset of -1:
final int offset = bank.getRows() - row - 1 + readoutStateOffset;

// Assign delay-corrected helicity to this HEL::scaler row:
bank.putByte("helicity",row,seq.search(timestamp,offset).value());
if (seq.getHalfWavePlate())
bank.putByte("helicityRaw",0,(byte)(-1*seq.search(timestamp,offset).value()));
else
bank.putByte("helicityRaw",0,seq.search(timestamp,offset).value());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static void main(String[] args) {
LOGGER.info("\n>>> Initializing helicity configuration from CCDB ...\n");
ConstantsManager conman = new ConstantsManager();
conman.init("/runcontrol/hwp","/runcontrol/helicity");
final int run = getRunNumber(parser.getInputList().get(0));
final int run = Util.getRunNumber(parser.getInputList().get(0));
IndexedTable helTable = conman.getConstants(run, "/runcontrol/helicity");

// Initialize the scaler sequence from tag-1 events:
Expand Down Expand Up @@ -137,7 +137,7 @@ public static void main(String[] args) {
if (doHelicityDelay) {
recEventBank.putByte("helicity",0,hb.value());
recEventBank.putByte("helicityRaw",0,hbraw.value());
RebuildScalers.assignScalerHelicity(runConfigBank.getLong("timestamp",0), helScalerBank, helSeq);
Util.assignScalerHelicity(runConfigBank.getLong("timestamp",0), helScalerBank, helSeq);
}

// Write beam charge to REC::Event:
Expand All @@ -154,7 +154,7 @@ public static void main(String[] args) {
writer.addEvent(event, event.getEventTag());

// Copy config banks to new, tag-1 events:
createTag1Events(writer, event, configEvent, configBanks);
Util.createTag1Events(writer, event, configEvent, configBanks);
}

reader.close();
Expand All @@ -171,32 +171,5 @@ public static void main(String[] args) {
LOGGER.info(String.format("Tag1ToEvent: Good Charge Fraction: %.2f%%",100*(float)goodCharge/(goodCharge+badCharge)));
}

private static void createTag1Events(HipoWriterSorted writer, Event source, Event destination, Bank... banks) {
destination.reset();
for (Bank bank : banks) {
source.read(bank);
if (bank.getRows()>0)
destination.write(bank);
}
if (!destination.isEmpty())
writer.addEvent(destination,1);
}

private static int getRunNumber(String... filenames) {
Event event = new Event();
for (String filename : filenames) {
HipoReader reader = new HipoReader();
reader.open(filename);
Bank bank = new Bank(reader.getSchemaFactory().getSchema("RUN::config"));
while (reader.hasNext()) {
reader.nextEvent(event);
event.read(bank);
if (bank.getRows()>0 && bank.getInt("run",0)>0)
return bank.getInt("run",0);
}
}
return -1;
}

}

Loading

0 comments on commit c44a1ff

Please sign in to comment.