Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jlab.detector.calib.utils.RCDBConstants;
import org.jlab.detector.scalers.DaqScalers;
import org.jlab.detector.helicity.HelicitySequenceManager;
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.io.HipoReader;
Expand Down Expand Up @@ -35,6 +36,7 @@ public static void main(String[] args) {

OptionParser parser = new OptionParser("rebuildscaler");
parser.addRequired("-o","output.hipo");
parser.addOption("-c", "0", "Fix DSC/RUN::scaler clock rollover");
parser.parse(args);
List<String> inputList = parser.getInputList();
if(inputList.isEmpty()==true){
Expand All @@ -46,7 +48,7 @@ public static void main(String[] args) {
HelicitySequenceManager helSeq = new HelicitySequenceManager(8,inputList);

HipoWriterSorted writer = new HipoWriterSorted();
writer.getSchemaFactory().initFromDirectory(ClasUtilsFile.getResourceDir("COATJAVA", "etc/bankdefs/hipo4"));
writer.getSchemaFactory().initFromDirectory(ClasUtilsFile.getResourceDir("CLAS12DIR", "etc/bankdefs/hipo4"));
writer.setCompressionType(2);
writer.open(parser.getOption("-o").stringValue());

Expand All @@ -59,6 +61,13 @@ public static void main(String[] args) {
ConstantsManager conman = new ConstantsManager();
conman.init(Arrays.asList(new String[]{CCDB_FCUP_TABLE,CCDB_SLM_TABLE,CCDB_HEL_TABLE,CCDB_DSC_TABLE}));

DaqScalersSequence seq = null;
if (!parser.getOption("-c").stringValue().equals("0")) {
System.out.println("Correcting clock rollover!");
seq = DaqScalersSequence.rebuildSequence(1, conman, inputList);
seq.fixClockRollover();
}

for (String filename : inputList) {

HipoReader reader = new HipoReader();
Expand Down Expand Up @@ -106,6 +115,11 @@ public static void main(String[] args) {
Date uet = new Date(runConfigBank.getInt("unixtime",0)*1000L);
ds = DaqScalers.create(rawScalerBank, ccdb_fcup, ccdb_slm, ccdb_hel, rst, uet);
}

if (seq != null) {
ds.dsc2.setClock(seq.get(event).dsc2.getClock());
ds.dsc2.setGatedClock(seq.get(event).dsc2.getClock());
}

runScalerBank = ds.createRunBank(writer.getSchemaFactory());
helScalerBank = ds.createHelicityBank(writer.getSchemaFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class DaqScaler {
protected long gatedClock=-1; // counts
protected long gatedSlm=-1; // counts

public final void setClock(long c) { this.clock = c; }
public final void setGatedClock(long c) { this.gatedClock = c; }
public final long getClock() { return this.clock; }
public final long getFcup() { return this.fcup; }
public final long getSlm() { return this.slm; }
Expand Down Expand Up @@ -45,7 +47,7 @@ public class DaqScaler {

@Override
public String toString() {
return String.format("c=%d/%d f=%d/%d s=%d/%d",clock,gatedClock,fcup,gatedFcup,slm,gatedSlm);
return String.format("clock=%d/%d fcup=%d/%d slm=%d/%d",clock,gatedClock,fcup,gatedFcup,slm,gatedSlm);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ public class DaqScalers {

public Dsc2Scaler dsc2=null;
public StruckScalers struck=null;

private long timestamp=0;

public DaqScalers setTimestamp(long timestamp) {
this.timestamp=timestamp;
return this;
}

public long getTimestamp(){ return this.timestamp; }

@Override
public String toString() {
return String.format("%d %s",timestamp,dsc2);
}

/**
* Get seconds between two dates assuming the differ by not more than 24 hours.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jlab.jnp.hipo4.data.Event;
import org.jlab.jnp.hipo4.data.Bank;
import org.jlab.jnp.hipo4.data.SchemaFactory;
import org.jlab.utils.groups.IndexedTable;

/**
* For easy access to most recent scaler readout for any given event.
Expand All @@ -29,7 +30,6 @@ public class DaqScalersSequence implements Comparator<DaqScalers> {

private Bank runConfigBank=null;
private Bank runScalerBank=null;
private Bank rawScalerBank=null;

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

Expand Down Expand Up @@ -263,7 +263,7 @@ public static DaqScalersSequence readSequence(List<String> filenames) {
}

/**
*
* Reads the RAW::scaler bank and rebuilds the RUN::scaler and HEL::scaler banks
* @param tags
* @param conman
* @param filenames
Expand Down Expand Up @@ -293,20 +293,59 @@ public static DaqScalersSequence rebuildSequence(int tags, ConstantsManager conm
}
return seq;
}

/**
* Try to fix clock rollover on the run-integrating DSC2 scaler.
* 1. Assume the first clock readout has no rollover.
* 2. Assume any subsequent clock decrease is a rollover.
*/
public void fixClockRollover() {
boolean modified = true;
while (modified) {
modified = false;
for (int i=this.scalers.size()-1; i>0; --i) {
Dsc2Scaler previous = this.scalers.get(i-1).dsc2;
Dsc2Scaler next = this.scalers.get(i).dsc2;
if (previous.clock > next.clock) {
for (int j=i; j<this.scalers.size(); ++j) {
if (j==i) System.out.print( String.format("FIXING CLOCK ROLLOVER: %d %d -> ",this.scalers.get(j).dsc2.clock,this.scalers.get(j).dsc2.gatedClock));
this.scalers.get(j).dsc2.clock += 2*(long)Integer.MAX_VALUE;
// The gated clock also rolls over (but it's triggered by the ungated clock, not itself!?):
this.scalers.get(j).dsc2.gatedClock += 2*(long)Integer.MAX_VALUE;
if (j==i) System.out.println(String.format("%d %d",this.scalers.get(j).dsc2.clock,this.scalers.get(j).dsc2.gatedClock));
}
modified = true;
break;
}
}
}
}

public static void main(String[] args) {

final String dir="/Users/baltzell/data/CLAS12/rg-a/decoded/6b.2.0/";
final String file="clas_005038.evio.00000-00004.hipo";
//final String dir="/Users/baltzell/data/CLAS12/rg-b/decoded/";
//final String file="clas_006432.evio.00041-00042.hipo";
final String dir = System.getenv("HOME")+"/data/";
//final String file = "rollover-4013.hipo";
final String file = "DVCSWagon_004013.hipo";
//final String file = "clas_004003.evio.00040-00049.hipo";

List<String> filenames=new ArrayList<>();
if (args.length>0) filenames.addAll(Arrays.asList(args));
else filenames.add(dir+file);

ConstantsManager consts = new ConstantsManager();
consts.init("/runcontrol/fcup","/runcontrol/slm","/runcontrol/helicity","/daq/config/scalers/dsc1","/runcontrol/hwp");

// 1!!!1 initialize a sequence from tag=1 events:
DaqScalersSequence seq = DaqScalersSequence.readSequence(filenames);
DaqScalersSequence seq = DaqScalersSequence.rebuildSequence(1, consts, filenames);
//DaqScalersSequence seq = DaqScalersSequence.readSequence(filenames);

//for (DaqScalers ds : seq.scalers) System.out.println(String.format("PRE: %s",ds));

seq.fixClockRollover();

//for (DaqScalers ds : seq.scalers) System.out.println(String.format("POST: %s",ds));

System.exit(1);

long good=0;
long bad=0;
Expand Down Expand Up @@ -341,7 +380,11 @@ public static void main(String[] args) {
else {
good++;
// do something useful with beam charge here:
System.out.println(timestamp+" "+ds.dsc2.getBeamCharge()+" "+ds.dsc2.getBeamChargeGated());
System.out.println(String.format("%d %s %f %f",
timestamp,
ds.dsc2,
ds.dsc2.getBeamCharge(),
ds.dsc2.getBeamChargeGated()));
}
}

Expand Down