Skip to content

Commit

Permalink
Merge pull request #7 from naidoo88/master
Browse files Browse the repository at this point in the history
CND Calibration suite v1.2.0
  • Loading branch information
naidoo88 authored Apr 20, 2020
2 parents f1bb7a1 + f3ff391 commit e9cfe00
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 20 deletions.
34 changes: 22 additions & 12 deletions src/main/java/org/jlab/calib/services/cnd/CNDCalibration.java
Original file line number Diff line number Diff line change
Expand Up @@ -1058,15 +1058,15 @@ public void configure() {
Panel.add(intro1,c1);
c1.gridx = 0;
c1.gridy = 4;
JLabel intro2= new JLabel("- In \"Select Steps\" choose the calibration you want to perform");
JLabel intro2= new JLabel("- In \"Select Step\" choose the calibration you want to perform");
Panel.add(intro2,c1);
c1.gridx = 0;
c1.gridy = 5;
JLabel intro3= new JLabel("- In \"Previous calibration values\" load calibration constants as necessary");
Panel.add(intro3,c1);
c1.gridx = 0;
c1.gridy = 6;
JLabel intro4= new JLabel("- In \"Tracking options\" add some cuts on the match CVT tracks. WARNING these cuts are used for every calibration step.");
JLabel intro4= new JLabel("- In \"Tracking / General\" add some cuts on the match CVT tracks. WARNING these cuts are used for every calibration step.");
Panel.add(intro4,c1);
c1.gridx = 0;
c1.gridy = 7;
Expand All @@ -1090,7 +1090,7 @@ public void configure() {

int gridy=0;
for (int i=0; i< engines.length; i++) {
if(i== HV || i== TIME_OFFSETS_RF || i== TDC_CONV )continue;
if(i== HV || i== TIME_OFFSETS_RF || i== TDC_CONV || i== UTURN_TLOSS || i== ATTENUATION)continue; //added exceptions for attenuation and LR_offset --PN
c.gridx = 0; c.gridy = gridy;
gridy++;
c.anchor = c.WEST;
Expand Down Expand Up @@ -1131,7 +1131,7 @@ public void configure() {



configPane.add("Select steps", stepOuterPanel);
configPane.add("Select Step", stepOuterPanel);

// Previous calibration values
JPanel confOuterPanel = new JPanel(new BorderLayout());
Expand All @@ -1158,9 +1158,19 @@ public void configure() {

for (int i=2; i< 5; i++) {
engPanels[i-2] = new CNDPrevConfigPanel(engines[i]);
// Override the Prev.Calib titles with more instructive labled after changing engine names --PN
if (i-2 == 0) {
engPanels[i-2].setBorder(BorderFactory.createTitledBorder("TimeOffset_LR [Adj. LR-Offset for Global TimeOffset step]"));
}
if (i-2 == 1) {
engPanels[i-2].setBorder(BorderFactory.createTitledBorder("Effective Velocity"));
}
if (i-2 == 2) {
engPanels[i-2].setBorder(BorderFactory.createTitledBorder("Adjusted LR-Offset / Uturn-TimeLoss"));
}
confPanel.add(engPanels[i-2]);
}

}

// Previously:
// for (int i=3; i< engines.length; i++) {
Expand All @@ -1172,7 +1182,7 @@ public void configure() {
confOuterPanel.add(confPanel, BorderLayout.NORTH);
confOuterPanel.add(butPage2, BorderLayout.SOUTH);

configPane.add("Previous calibration values", confOuterPanel);
configPane.add("Previous Calibration Values", confOuterPanel);

// Tracking options
JPanel trOuterPanel = new JPanel(new BorderLayout());
Expand Down Expand Up @@ -1443,10 +1453,10 @@ public void configure() {

JPanel butPage6 = new configButtonPanel(this, true, "Next");
effVOuterPanel.add(butPage6, BorderLayout.SOUTH);
configPane.add("Effective Velocity", effVOuterPanel);

configPane.add("Effective Velocity, Uturn-TimeLoss & Adjusted LR-Offset", effVOuterPanel);

// ut options
//Suppress tabs for uturn' and Attenuation. -- PN
/* // ut options
JPanel utOuterPanel = new JPanel(new BorderLayout());
JPanel utPanel = new JPanel(new GridBagLayout());
utOuterPanel.add(utPanel, BorderLayout.NORTH);
Expand Down Expand Up @@ -1593,7 +1603,7 @@ public void configure() {
JPanel butPage8= new configButtonPanel(this, true, "Next");
attOuterPanel.add(butPage8, BorderLayout.SOUTH);
configPane.add("Attenuation", attOuterPanel);

*/ //Suppress tabs for uturn' and Attenuation. -- PN

//LR offset
JPanel timeOuterPanel = new JPanel(new BorderLayout());
Expand Down Expand Up @@ -1627,7 +1637,7 @@ public void configure() {

JPanel butPagetime = new configButtonPanel(this, true, "Next");
timeOuterPanel.add(butPagetime, BorderLayout.SOUTH);
configPane.add("Global time Offset", timeOuterPanel);
configPane.add("Global TimeOffset", timeOuterPanel);



Expand Down Expand Up @@ -1727,7 +1737,7 @@ public void configure() {

JPanel butPage9 = new configButtonPanel(this, true, "Finish");
energyOuterPanel.add(butPage9, BorderLayout.SOUTH);
configPane.add("Energy", energyOuterPanel);
configPane.add("Attenuation & Energy", energyOuterPanel);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public class CNDCalibrationEngine extends CalibrationEngine {
public String stepName = "Unknown";
public String fileNamePrefix = "Unknown";
public String filename = "Unknown.txt";

public String fileNamePrefix2 = "Unknown"; //--PN
public String filename2 = "Unknown.txt"; // --PN
// configuration
public int calDBSource = 0;
public static final int CAL_DEFAULT = 0;
Expand Down Expand Up @@ -346,6 +347,36 @@ public String nextFileName() {
return filePrefix + "." + newFileNum + ".txt";
}

//added for the purposes of updating name of a second file --PN
public String nextFileName2() {

// Get the next file name
Date today = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String todayString = dateFormat.format(today);
String filePrefix = fileNamePrefix2 + todayString;
int newFileNum = 0;

File dir = new File(".");
File[] filesList = dir.listFiles();

for (File file : filesList) {
if (file.isFile()) {
String fileName = file.getName();
if (fileName.matches(filePrefix + "[.]\\d+[.]txt")) {
String fileNumString = fileName.substring(
fileName.indexOf('.') + 1,
fileName.lastIndexOf('.'));
int fileNum = Integer.parseInt(fileNumString);
if (fileNum >= newFileNum)
newFileNum = fileNum + 1;

}
}
}
return filePrefix + "." + newFileNum + ".txt";
}

public void customFit(int sector, int layer, int paddle) {
// overridden in calibration step class
}
Expand Down
200 changes: 197 additions & 3 deletions src/main/java/org/jlab/calib/services/cnd/CNDEffVEventListener.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.jlab.calib.services.cnd;

import java.awt.BorderLayout;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -45,6 +49,9 @@
import org.jlab.io.task.DataSourceProcessorPane;
import org.jlab.io.task.IDataEventListener;
import org.jlab.utils.groups.IndexedList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
* CND Calibration suite
Expand Down Expand Up @@ -122,13 +129,15 @@ public class CNDEffVEventListener extends CNDCalibrationEngine {

public CNDEffVEventListener() {

stepName = "EffV";
stepName = "EffV, Uturn-Loss & LR-Adj.";
fileNamePrefix = "CND_CALIB_EFFV_";
fileNamePrefix2 = "CND_CALIB_UTURNTLOSS_";
// get file name here so that each timer update overwrites it
filename = nextFileName();
filename2 = nextFileName2(); // --PN

calib = new CalibrationConstants(3,
"veff_L/F:veff_L_err/F:veff_R/F:veff_R_err/F");
"veff_L/F:veff_L_err/F:veff_R/F:veff_R_err/F:uturn_tloss/F:adjusted_LR_offset/F");

// calib = new CalibrationConstants(3,
// "tdc_conv_left/F:tdc_conv_right/F");
Expand Down Expand Up @@ -959,6 +968,184 @@ public Double getVeffRError(int sector, int layer, int paddle){
return veffError;
}

public Double getUturnTloss(int sector, int layer, int component) {

double cLeft = 0.0;
double cRight = 0.0;
double overrideLeftVal = constants.getItem(sector, layer, component)[OVERRIDE_LEFT];
double overrideRightVal = constants.getItem(sector, layer, component)[OVERRIDE_RIGHT];

if (overrideLeftVal != UNDEFINED_OVERRIDE) {
cLeft = overrideLeftVal;
}
else {
double intercept = dataGroups.getItem(sector,layer,component).getF1D("funcL").getParameter(0);
cLeft = -1 * intercept;
}

if (overrideRightVal != UNDEFINED_OVERRIDE) {
cRight = overrideRightVal;
}
else {
double intercept = dataGroups.getItem(sector,layer,component).getF1D("funcR").getParameter(0);
cRight = -1 * intercept;
}

CNDPaddlePair tempPaddlePair = new CNDPaddlePair(sector,layer,1);
double uturnTloss = cLeft + cRight - (tempPaddlePair.paddleLength() * ( (1./getEffVLeft(sector, layer, component))+(1./getEffVRight(sector, layer, component)) ));

if (sector == 1){
System.out.println("cLeft = " + cLeft);
System.out.println("cRight = " + cRight);
System.out.println("tempPaddlePair.paddleLength() = " + tempPaddlePair.paddleLength());
System.out.println("Other term = " + ( (1./getEffVLeft(sector, layer, component))+(1./getEffVRight(sector, layer, component)) ));

}

return uturnTloss;
}

public Double getLRoffsetAdjust(int sector, int layer, int component){

double LRoffsetAdjust = 0.0;

double gradientL = dataGroups.getItem(sector,layer,component).getF1D("funcL")
.getParameter(1);
double gradientR = dataGroups.getItem(sector,layer,component).getF1D("funcR")
.getParameter(1);

double Cleft = dataGroups.getItem(sector,layer,component).getF1D("funcL").getParameter(0);
double CRight = dataGroups.getItem(sector,layer,component).getF1D("funcR").getParameter(0);

CNDPaddlePair tempPaddlePair = new CNDPaddlePair(sector,layer,1);

LRoffsetAdjust = CRight - Cleft;

System.out.println();
System.out.println("CleftErr = " + Cleft);
System.out.println("CRightErr = " + CRight);
System.out.println("utlossError = " + LRoffsetAdjust);

return LRoffsetAdjust+leftRightValues.getDoubleValue("time_offset_LR", sector, layer, component);
}

@Override
public void writeFile(String filename) {

//indexes for what is to be written out in lines of the file --PN
int[] writeCols = {1, 1, 1, 2, 2, 2, 2, 3, 4};

try { //EffV output

// Open the output file
File outputFile = new File(filename);
FileWriter outputFw = new FileWriter(outputFile.getAbsoluteFile());
BufferedWriter outputBw = new BufferedWriter(outputFw);

for (int i=0; i<calib.getRowCount(); i++) {
String line = new String();
for (int j=0; j<calib.getColumnCount(); j++) {
if (writeCols[j] == 1 || writeCols[j] == 2) {
line = line+calib.getValueAt(i, j);
if (j<calib.getColumnCount()-1 && j != 6) { //hard code exception to stop whitespace being added at the end of each row of EffV file --PN
line = line+" ";
}
}
}
outputBw.write(line);
outputBw.newLine();
}

outputBw.close();


//filename2 = nextFileName2(); //increment filename2 after writing (filename incremented by button execution in CNDCalibration.java).
// Open the second output file for uturn --PN
File outputFile2 = new File("uturn_LRad_"+filename2);
FileWriter outputFw2 = new FileWriter(outputFile2.getAbsoluteFile());
BufferedWriter outputBw2 = new BufferedWriter(outputFw2);

for (int i=0; i<calib.getRowCount(); i++) {
String line = new String();
for (int j=0; j<calib.getColumnCount(); j++) {
if (writeCols[j] == 1 || writeCols[j] == 3 || writeCols[j] == 4) {
line = line+calib.getValueAt(i, j);
if (j<calib.getColumnCount()-1) {
line = line+" ";
}
}
}

outputBw2.write(line);
outputBw2.newLine();
}

outputBw2.close();

// Open the second output file for LR_adj --PN
File outputFile3 = new File("LRoffset_adjusted_"+filename2);
FileWriter outputFw3 = new FileWriter(outputFile3.getAbsoluteFile());
BufferedWriter outputBw3 = new BufferedWriter(outputFw3);

for (int i=0; i<calib.getRowCount(); i++) {
String line = new String();
for (int j=0; j<calib.getColumnCount(); j++) {
if(writeCols[j] == 1 || writeCols[j] == 4){
line = line+calib.getValueAt(i, j);
if (j<calib.getColumnCount()-1) {
line = line+" ";
}
}
}
line = line+" 0.0";
outputBw3.write(line);
outputBw3.newLine();
}

outputBw3.close();
filename2 = nextFileName2(); //increment filename2 after writing both files (filename incremented by button execution in CNDCalibration.java).

}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ filename2 + "'");
ex.printStackTrace();
}

}

//added for the purposes of updating name of a second file --PN
@Override
public String nextFileName2() {

// Get the next file name
Date today = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String todayString = dateFormat.format(today);
String filePrefix = fileNamePrefix2 + todayString;
int newFileNum = 0;

File dir = new File(".");
File[] filesList = dir.listFiles();

for (File file : filesList) {
if (file.isFile()) {
String fileName = file.getName();
if (fileName.matches(".*" + filePrefix + "[.]\\d+[.]txt")) {
String fileNumString = fileName.substring(
fileName.indexOf('.') + 1,
fileName.lastIndexOf('.'));
int fileNum = Integer.parseInt(fileNumString);
if (fileNum >= newFileNum)
newFileNum = fileNum + 1;

}
}
}
return filePrefix + "." + newFileNum + ".txt";
}

@Override
public void saveRow(int sector, int layer, int component) {

Expand All @@ -979,8 +1166,15 @@ public void saveRow(int sector, int layer, int component) {
// "veff_R_err", sector, layer, component);
calib.setDoubleValue(getVeffRError(sector, layer, component),
"veff_R_err", sector, layer, component);

calib.setDoubleValue(getUturnTloss(sector, layer, component),
"uturn_tloss", sector, layer, component);
// calib.setDoubleValue(getUturnTlossError(sector, layer, component),
// "uturn_tloss_err", sector, layer, component);

}
calib.setDoubleValue(getLRoffsetAdjust(sector, layer, component),
"adjusted_LR_offset", sector, layer, component);
}

@Override
public boolean isGoodPaddle(int sector, int layer, int paddle) {
Expand Down
Loading

0 comments on commit e9cfe00

Please sign in to comment.