Skip to content

Commit

Permalink
added functionality to merge background for individual DC regions, pl…
Browse files Browse the repository at this point in the history
…us bug fixed and some cleanup
  • Loading branch information
raffaelladevita authored and baltzell committed Dec 31, 2024
1 parent 86dc443 commit da9f4d8
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ADCTDCMerger {
private EventMergerConstants constants;

private DataEvent event;
private List<DataEvent[]> bgEvents;
private List<DataEvent>[] bgEvents;
private int run;

/**
Expand All @@ -39,13 +39,12 @@ public class ADCTDCMerger {
* @param signal physics events
* @param bgs background events
*/
public ADCTDCMerger(EventMergerConstants constants, DataEvent signal, DataEvent[]... bgs) {
public ADCTDCMerger(EventMergerConstants constants, DataEvent signal, List<DataEvent>... bgs) {
this.constants = constants;
this.event = signal;
this.bgEvents = new ArrayList<>();
this.bgEvents.addAll(Arrays.asList(bgs));
if(!bgEvents.isEmpty() && bgEvents.get(0)[0].hasBank("RUN::config"))
run = bgEvents.get(0)[0].getBank("RUN::config").getInt("run", 0);
this.bgEvents = bgs;
if(bgEvents.length!=0 && !bgEvents[0].isEmpty() && bgEvents[0].get(0).hasBank("RUN::config"))
run = bgEvents[0].get(0).getBank("RUN::config").getInt("run", 0);
}

/**
Expand Down Expand Up @@ -122,16 +121,17 @@ public List<DGTZ> readTDCs(DetectorType detector, DataBank bank) {
}

/**
* Merge ADC banks for data (signal) and background events for selected detector
* Merge ADC banks for data (signal) and background events for the selected detector and layers
* In case of multiple hit on same detector element, only first hit in time is kept
* unless the double-hit suppression flag, suppressDoubleHits, is set to false
*
* @param detector
* @param layers
* @return
*/
public DataBank mergeADCs(DetectorType detector){
public DataBank mergeADCs(DetectorType detector, List<Integer> layers){

DataEvent[] bgs = bgEvents.get(0);
List<DataEvent> bgs = bgEvents[0];

String ADCString = detector.getName()+"::adc";
for(DataEvent bg : bgs) {
Expand All @@ -142,7 +142,10 @@ public DataBank mergeADCs(DetectorType detector){

List<DGTZ> bgADCs = new ArrayList<>();
for(DataEvent bg : bgs) {
bgADCs.addAll(readADCs(detector,bg.getBank(ADCString)));
for(DGTZ dgtz : readADCs(detector,bg.getBank(ADCString))) {
if(layers==null || layers.contains(dgtz.getLayer()))
bgADCs.add((ADC) dgtz);
}
}
List<DGTZ> ADCs = readADCs(detector,event.getBank(ADCString));

Expand All @@ -153,16 +156,17 @@ public DataBank mergeADCs(DetectorType detector){
}

/**
* Merge TDC banks for data (signal) and background events for selected detector
* Merge TDC banks for data (signal) and background events for the selected detector and layers
* Use two background events shifted in time to extend the time range of the backgrounds
* Multiple hits on the same components are kept if time distance exceed the holdoff time
*
* @param detector
* @param layers
* @return
*/
public DataBank mergeTDCs(DetectorType detector){
public DataBank mergeTDCs(DetectorType detector, List<Integer> layers){

DataEvent[] bgs = bgEvents.get(0);
List<DataEvent> bgs = bgEvents[0];

String TDCString = detector+"::tdc";

Expand All @@ -178,19 +182,21 @@ public DataBank mergeTDCs(DetectorType detector){
if(!event.hasBank(TDCString)) bgSize = 1;
// collect bg hits
List<DGTZ> bgTDCs = new ArrayList<>();
for(int i=0; i<Math.min(bgSize, bgEvents.size()); i++) {
for(DataEvent bg : bgEvents.get(i)) {
for(int i=0; i<Math.min(bgSize, bgEvents.length); i++) {
for(DataEvent bg : bgEvents[i]) {
if(bg.hasBank(TDCString)) {
// get TDCs, correct them for jitter and shift them in time
int jitter = this.getTDCJitter(detector, bg);
for(DGTZ dgtz : readTDCs(detector, bg.getBank(TDCString))) {
TDC tdc = (TDC) dgtz;
int layer = tdc.getLayer();
int comp = tdc.getComponent();
int offset = constants.getInt(run, detector, EventMergerEnum.READOUT_WINDOW, 0, layer, comp);
tdc.shift(jitter-i*offset);
bgTDCs.add(tdc);
}
if(layers==null || layers.contains(layer)) {
int offset = constants.getInt(run, detector, EventMergerEnum.READOUT_WINDOW, 0, layer, comp);
tdc.shift(jitter-i*offset);
bgTDCs.add(tdc);
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jlab.detector.banks.RawBank;
Expand Down Expand Up @@ -36,16 +39,17 @@ public class EventMerger {
private boolean preserveHitOrder = true;
private EventMergerConstants constants = new EventMergerConstants();

private List<DetectorType> detectors;
private Map<DetectorType,List<Integer>> detectors;
private OrderType[] orders;

private List<String> bgFileNames;
private boolean reuseBgEvents = false;
private int bgScale = 1;
private int bgFileIndex = 0;
private HipoDataSource bgReader;

public EventMerger() {
detectors = Arrays.asList(DetectorType.DC, DetectorType.FTOF);
detectors = this.getDetectors(DetectorType.DC.getName(), DetectorType.FTOF.getName());
orders = this.getOrders(OrderType.NOMINAL.name(),OrderType.BGADDED_NOMINAL.name(),OrderType.BGREMOVED.name());
printConfiguration();
}
Expand All @@ -58,16 +62,41 @@ public EventMerger(String[] dets, String[] types, boolean dhits, boolean ohits)
printConfiguration();
}

private List<DetectorType> getDetectors(String[] dets) {
List<DetectorType> all = new ArrayList<>();
private Map<DetectorType,List<Integer>> getDetectors(String... dets) {
Map<DetectorType,List<Integer>> all = new HashMap<>();
if(dets.length==1 && dets[0].equals("ALL")) {
all.addAll(EventMergerConstants.ADCs);
for(DetectorType t : EventMergerConstants.TDCs) {
if(!all.contains(t)) all.add(t);
for(DetectorType t : EventMergerConstants.ADCs) {
all.put(t, null);
}
for(DetectorType d : EventMergerConstants.TDCs) {
if(!all.containsKey(d)) all.put(d, null);
}
}
else {
for(String d : dets) all.add(DetectorType.getType(d));
for(String d : dets) {
String[] dn = d.split("(?<=\\D)(?=\\d)");
DetectorType type = dn.length>0 ? DetectorType.getType(dn[0]) : DetectorType.UNDEFINED;
if(type == DetectorType.UNDEFINED) {
throw new IllegalArgumentException("Unknown detector type " + type);
}
else if(dn.length==2 && type==DetectorType.DC && dn[1].matches("[1-3]+")) {
int region = Integer.parseInt(dn[1]);
if(!all.containsKey(DetectorType.DC))
all.put(DetectorType.DC, new ArrayList<>());
for(int il=0; il<12; il++) {
int layer = (region-1)*12+il+1;
if(!all.get(DetectorType.DC).contains(layer))
all.get(DetectorType.DC).add(layer);
}
}
else {
all.put(type, null);
}
}
for(DetectorType type : all.keySet()) {
if(all.get(type)!=null)
Collections.sort(all.get(type));
}
}
return all;
}
Expand All @@ -85,8 +114,8 @@ private OrderType[] getOrders(String... type) {
return null;
}

public boolean setBgFiles(List<String> filenames, boolean reuse) {
bgFileNames = filenames;
public boolean setBgFiles(List<String> filenames, int scale, boolean reuse) {
bgFileNames = new ArrayList<>();
for (String filename : filenames) {
File f = new File(filename);
if (!f.exists() || !f.isFile() || !f.canRead()) {
Expand All @@ -96,6 +125,7 @@ public boolean setBgFiles(List<String> filenames, boolean reuse) {
Logger.getLogger(EventMerger.class.getName()).log(Level.INFO,"Background files: reading {0}",filename);
bgFileNames.add(filename);
}
if(scale>0) bgScale = scale;
reuseBgEvents = reuse;
return true;
}
Expand All @@ -117,14 +147,14 @@ private boolean openNextFile() {
return true;
}

synchronized public DataEvent[] getBackgroundEvents(int n) {
DataEvent[] events = new DataEvent[n];
synchronized public List<DataEvent> getBackgroundEvents(int n) {
List<DataEvent> events = new ArrayList<>();
for(int i=0; i<n; i++) {
if (!bgReader.hasEvent()) {
if (bgReader==null || !bgReader.hasEvent()) {
if(!openNextFile())
return null;
}
events[i] = bgReader.getNextEvent();
events.add(bgReader.getNextEvent());
}
return events;
}
Expand All @@ -138,7 +168,21 @@ private void printConfiguration() {

private void printDetectors() {
System.out.print("\nMerging activated for detectors: ");
for(DetectorType det : detectors) System.out.print(det.getName() + " ");
for(DetectorType det : detectors.keySet()) {
System.out.print(det.getName());
if(detectors.get(det)!=null) {
System.out.print("(layers: ");
for(int il=0; il<detectors.get(det).size(); il++) {
int layer = detectors.get(det).get(il);
if(il<detectors.get(det).size()-1)
System.out.print(layer + ",");
else
System.out.print(layer + ") ");
}
}
else
System.out.print(" ");
}
System.out.println("\n");
}

Expand All @@ -148,35 +192,40 @@ private void printOrders() {
System.out.println("\n");
}

private void mergeEvents(DataEvent event, DataEvent[] bg1, DataEvent[] bg2) {
private void mergeEvents(DataEvent event, List<DataEvent> bgs) {

if(!event.hasBank("RUN::config"))
return;
if(bg1.length != bg2.length)
if(!bgs.isEmpty() && bgs.size()%2==0)
return;
for(int i=0; i<bg1.length; i++)
if(!bg1[i].hasBank("RUN::config") || bg1[i].hasBank("RUN::config"))
for(DataEvent bg : bgs)
if(!bg.hasBank("RUN::config"))
return;

if(event.hasBank("DC::doca")) event.removeBank("DC::doca");

int nbg = bgs.size()/2;
List<DataEvent> bg1 = bgs.subList(1, nbg-1);
List<DataEvent> bg2 = bgs.subList(nbg, 2*nbg-1);
ADCTDCMerger merger = new ADCTDCMerger(constants, event, bg1, bg2);
merger.setSuppressDoubleHits(suppressDoubleHits);
merger.setPreserveHitOrder(preserveHitOrder);
merger.setSelectedOrders(orders);

for(DetectorType det : detectors) {
for(DetectorType det : detectors.keySet()) {

List<DataBank> banks = new ArrayList<>();
List<String> names = new ArrayList<>();

List<Integer> layers = detectors.get(det);

if(EventMergerConstants.ADCs.contains(det)) {
names.add(det.getName()+"::adc");
banks.add(merger.mergeADCs(det));
banks.add(merger.mergeADCs(det, layers));
}
if(EventMergerConstants.TDCs.contains(det)) {
names.add(det.getName()+"::tdc");
banks.add(merger.mergeTDCs(det));
banks.add(merger.mergeTDCs(det, layers));
}
if(banks.isEmpty())
System.out.println("Unknown detector:" + det);
Expand All @@ -191,17 +240,21 @@ private void mergeEvents(DataEvent event, DataEvent[] bg1, DataEvent[] bg2) {
* Append merged banks to hipo event
*
* @param event
* @param nBgEvents
* @return
*/
public boolean mergeEvents(DataEvent event, int nBgEvents) {
public boolean mergeEvents(DataEvent event) {

return this.mergeEvents(event, bgScale);
}


private boolean mergeEvents(DataEvent event, int scale) {

DataEvent[] eventBg1 = this.getBackgroundEvents(nBgEvents);
DataEvent[] eventBg2 = this.getBackgroundEvents(nBgEvents);
List<DataEvent> eventsBg = this.getBackgroundEvents(2*scale);

if(eventBg1==null || eventBg2==null) return false;
if(eventsBg==null) return false;

this.mergeEvents(event, eventBg1, eventBg2);
this.mergeEvents(event,eventsBg);
return true;
}

Expand All @@ -214,7 +267,7 @@ public static void main(String[] args) {
parser.addRequired("-i" ,"signal event file");
parser.setRequiresInputList(true);
parser.addOption("-n" ,"-1", "maximum number of events to process");
parser.addOption("-d" ,"DC,FTOF", "list of detectors, for example \"DC,FTOF,HTCC\" or \"ALL\" for all available detectors");
parser.addOption("-d" ,"DC,FTOF", "list of detectors, for example \"DC,FTOF,HTCC\" or \"ALL\" for all available detectors. Use DC1, DC2 or DC3 to select the DC region");
parser.addOption("-r" ,"1", "reuse background events: 0-false, 1-true");
parser.addOption("-s" ,"1", "suppress double TDC hits on the same component, 0-no suppression, 1-suppression");
parser.addOption("-l" ,"1", "preserve initial hit order (for compatibility with truth matching, 0-false, 1-true");
Expand All @@ -232,13 +285,13 @@ public static void main(String[] args) {
String detectors = parser.getOption("-d").stringValue();
String ordertypes = parser.getOption("-t").stringValue();
boolean doubleHits = (parser.getOption("-s").intValue()==1);
int nBG = parser.getOption("-x").intValue();
int bgScale = parser.getOption("-x").intValue();
boolean reuseBG = (parser.getOption("-r").intValue()==1);
boolean hitOrder = (parser.getOption("-l").intValue()==1);


EventMerger merger = new EventMerger(detectors.split(","),ordertypes.split(","),doubleHits,hitOrder);
if(!merger.setBgFiles(bgFiles, reuseBG))
if(!merger.setBgFiles(bgFiles, bgScale, reuseBG))
System.exit(1);

int counter = 0;
Expand All @@ -260,7 +313,7 @@ public static void main(String[] args) {
//System.out.println("************************************************************* ");
DataEvent eventData = readerData.getNextEvent();

if(merger.mergeEvents(eventData, nBG))
if(merger.mergeEvents(eventData))
writer.writeEvent(eventData);
else
maxEvents = counter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class BackgroundEngine extends ReconstructionEngine {
public static final String CONF_SUPPRESS_DOUBLES = "suppressDoubles";
public static final String CONF_PRESERVE_ORDER = "preserveOrder";
public static final String CONF_REUSE_EVENTS = "reuseEvents";
public static final String CONF_BG_SCALE = "bgScale";

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

Expand All @@ -45,8 +46,9 @@ public boolean init(String... filenames) {
boolean suppressDoubles = Boolean.valueOf(getEngineConfigString(CONF_SUPPRESS_DOUBLES,"true"));
boolean preserveOrder = Boolean.valueOf(getEngineConfigString(CONF_PRESERVE_ORDER,"true"));
boolean reuseEvents = Boolean.valueOf(getEngineConfigString(CONF_REUSE_EVENTS,"false"));
int bgScale = Integer.valueOf(getEngineConfigString(CONF_BG_SCALE,"1"));
bgmerger = new EventMerger(detectors.split(","), orders.split(","), suppressDoubles, preserveOrder);
return bgmerger.setBgFiles(Arrays.asList(filenames), reuseEvents);
return bgmerger.setBgFiles(Arrays.asList(filenames), bgScale, reuseEvents);
}

@Override
Expand Down

0 comments on commit da9f4d8

Please sign in to comment.