Skip to content
Merged
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
7 changes: 7 additions & 0 deletions DataModel/PsecData.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

using namespace std;

struct IDConfigRecord {
int RunNumber;
int ACCID;
int ManufacturerID;
string Position;
};

class PsecData{

friend class boost::serialization::access;
Expand Down
78 changes: 47 additions & 31 deletions UserTools/ANNIEEventTreeMaker/ANNIEEventTreeMaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ bool ANNIEEventTreeMaker::Initialise(std::string configfile, DataModel &data)
fANNIETree->Branch("LAPPD_BG_switchBit1", &fLAPPD_BG_switchBit1);

// load the ID conversion table
string ACCIDConfigFile = "LAPPDIDConfig.csv";
std::string ACCIDConfigFile = "./configfiles/LAPPDProcessedAna/LAPPDIDConfig.csv";
m_variables.Get("ACCIDConfigFile", ACCIDConfigFile);
idConfigRecords = LoadIDConfig(ACCIDConfigFile);

//print the loaded records
if (ANNIEEventTreeMakerVerbosity > 1)
{
Expand All @@ -234,30 +235,22 @@ bool ANNIEEventTreeMaker::Initialise(std::string configfile, DataModel &data)
<< ", Position: " << record.Position << endl;
}

int testRunNum = 4950;
int testACCID = 1; // example ACCID to query
int testRunNum = 6050;
int testACCID = 0; // example ACCID to query
auto result = queryNearestID(idConfigRecords, testRunNum, testACCID);
int nearestManuID = std::get<0>(result);
std::string position = std::get<1>(result);
cout << "Querying nearest ID for RunNumber: " << testRunNum << ", ACCID: " << testACCID << endl;
cout << "Nearest ManufacturerID: " << nearestManuID << ", Position: " << position << endl;

int testRunNum2 = 5000;
int testACCID2 = 2; // example ACCID to query
auto result2 = queryNearestID(idConfigRecords, testRunNum2, testACCID2);
int nearestManuID2 = std::get<0>(result2);
std::string position2 = std::get<1>(result2);
cout << "Querying nearest ID for RunNumber: " << testRunNum2 << ", ACCID: " << testACCID2 << endl;
cout << "Nearest ManufacturerID: " << nearestManuID2 << ", Position: " << position2 << endl;

int testRunNum3 = 5907;
int testManuID3 = 39; // example ManufacturerID to query
auto result3 = queryNearestACCID(idConfigRecords, testRunNum3, testManuID3);
int nearestACCID3 = std::get<0>(result3);
std::string position3 = std::get<1>(result3);
cout << "Querying nearest ACCID for RunNumber: " << testRunNum3 << ", ManufacturerID: " << testManuID3 << endl;
cout << "Nearest ACCID: " << nearestACCID3 << ", Position: " << position3 << endl;
}
testRunNum = 5907;
int testManuID = 39; // example ManufacturerID to query
result= queryNearestACCID(idConfigRecords, testRunNum, testManuID);
int nearestACCID = std::get<0>(result);
position = std::get<1>(result);
cout << "Querying nearest ACCID for RunNumber: " << testRunNum << ", ManufacturerID: " << testManuID << endl;
Comment thread
marc1uk marked this conversation as resolved.
cout << "Nearest ACCID: " << nearestACCID << ", Position: " << position << endl;
}
}

// LAPPD reconstruction information
Expand Down Expand Up @@ -1475,16 +1468,19 @@ void ANNIEEventTreeMaker::FillLAPPDInfo()
// So for those runs, we need to map the ACC ID to Incom ID using the config.
// also for positions, we use string for now, to avoid confusion with int IDs, can be changed later.
string position;

auto config = queryNearestID(idConfigRecords, fRunNumber, LAPPD_IDInit);
position = std::get<1>(config);

if (LAPPD_IDInit < 20)
{
LAPPD_IDInit = std::get<0>(config);
}

if (ANNIEEventTreeMakerVerbosity > 3)
cout << "ANNIEEventTreeMaker: Filling LAPPD Info, Original LAPPD_ID: " << psecData.LAPPD_ID << ", Mapped LAPPD_ID: " << LAPPD_IDInit << ", Position: " << position << ", using run number: " << fRunNumber << endl;

fLAPPD_ID.push_back(psecData.LAPPD_ID);
fLAPPD_ID.push_back(LAPPD_IDInit);
fLAPPD_Position.push_back(position);
fLAPPD_Beamgate_ns.push_back(LAPPDBeamgate_ns[key]);
fLAPPD_Timestamp_ns.push_back(LAPPDTimeStamps_ns[key]);
Expand Down Expand Up @@ -1531,11 +1527,20 @@ void ANNIEEventTreeMaker::FillLAPPDPulse()

vector<LAPPDPulse> pulse0 = stripPulses.at(0);
vector<LAPPDPulse> pulse1 = stripPulses.at(1);

for (int i = 0; i < pulse0.size(); i++)
{
fPulseSide.push_back(0);
LAPPDPulse thisPulse = pulse0.at(i);
fLAPPD_IDs.push_back(thisPulse.GetTubeId());

// mapping to INCOM IDs
int thisPulseID = thisPulse.GetTubeId();
if (thisPulseID < 20) {
auto config = queryNearestID(idConfigRecords, fRunNumber, thisPulseID);
thisPulseID = std::get<0>(config);
}

fLAPPD_IDs.push_back(thisPulseID);
fChannelID.push_back(thisPulse.GetChannelID());
fPulseStripNum.push_back(stripno);
fPulsePeakTime.push_back(thisPulse.GetTime());
Expand All @@ -1553,7 +1558,15 @@ void ANNIEEventTreeMaker::FillLAPPDPulse()
{
fPulseSide.push_back(1);
LAPPDPulse thisPulse = pulse1.at(i);
fLAPPD_IDs.push_back(thisPulse.GetTubeId());

// mapping to INCOM IDs
int thisPulseID = thisPulse.GetTubeId();
if (thisPulseID < 20) {
auto config = queryNearestID(idConfigRecords, fRunNumber, thisPulseID);
thisPulseID = std::get<0>(config);
}

fLAPPD_IDs.push_back(thisPulseID);
fChannelID.push_back(thisPulse.GetChannelID());
fPulseStripNum.push_back(stripno);
fPulsePeakTime.push_back(thisPulse.GetTime());
Expand Down Expand Up @@ -1587,7 +1600,15 @@ void ANNIEEventTreeMaker::FillLAPPDHit()
LAPPDHit thisHit = stripHits.at(i);
LAPPDPulse p1 = thisHit.GetPulse1();
LAPPDPulse p2 = thisHit.GetPulse2();
fLAPPDHit_IDs.push_back(thisHit.GetTubeId());

// mapping to INCOM IDs
int thisHitID = thisHit.GetTubeId();
if (thisHitID < 20) {
auto config = queryNearestID(idConfigRecords, fRunNumber, thisHitID);
thisHitID = std::get<0>(config);
}

fLAPPDHit_IDs.push_back(thisHitID);
fLAPPDHitStrip.push_back(stripno);
fLAPPDHitTime.push_back(thisHit.GetTime());
fLAPPDHitAmp.push_back(thisHit.GetCharge());
Expand Down Expand Up @@ -2731,9 +2752,6 @@ void ANNIEEventTreeMaker::FillLAPPDMCHitInfo()
}
}




vector<IDConfigRecord> ANNIEEventTreeMaker::LoadIDConfig(const string& filename) {
vector<IDConfigRecord> data;
ifstream file(filename);
Expand All @@ -2751,12 +2769,13 @@ vector<IDConfigRecord> ANNIEEventTreeMaker::LoadIDConfig(const string& filename)
stringstream ss(line);
string token;
IDConfigRecord r;

getline(ss, token, ','); r.RunNumber = stoi(token);
getline(ss, token, ','); r.ACCID = stoi(token);
getline(ss, token, ','); r.ManufacturerID = stoi(token);
getline(ss, token, ','); r.Position = token;

data.push_back(r);
data.push_back(r);
}

return data;
Expand All @@ -2782,8 +2801,6 @@ tuple<int, string> ANNIEEventTreeMaker::queryNearestID(const vector<IDConfigReco
return {bestManufacturer, bestPosition};
}



tuple<int, string> ANNIEEventTreeMaker::queryNearestACCID(const vector<IDConfigRecord>& data, int targetRun, int manufacturerID) {
int bestRun = -1;
int bestACCID = -1;
Expand All @@ -2803,4 +2820,3 @@ tuple<int, string> ANNIEEventTreeMaker::queryNearestACCID(const vector<IDConfigR
return {-1, ""};
return {bestACCID, bestPosition};
}

6 changes: 0 additions & 6 deletions UserTools/ANNIEEventTreeMaker/ANNIEEventTreeMaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@
* $Date: 2024/8 $
* Contact: yuef@iastate.edu
*/
struct IDConfigRecord {
int RunNumber;
int ACCID;
int ManufacturerID;
string Position;
};

class ANNIEEventTreeMaker : public Tool
{
Expand Down
126 changes: 123 additions & 3 deletions UserTools/LAPPDLoadStore/LAPPDLoadStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,34 @@ bool LAPPDLoadStore::Initialise(std::string configfile, DataModel &data)
if (LAPPDStoreReadInVerbosity > 11)
debugStoreReadIn.open("debugStoreReadIn.txt");

std::string ACCIDConfigFile;
get_ok = m_variables.Get("ACCIDConfigFile", ACCIDConfigFile);
if(!get_ok) {
Log("LAPPDLoadStore: Missing ACCIDConfigFile in config!",v_error,LAPPDStoreReadInVerbosity);
return false;
}

idConfigRecords = LoadIDConfig(ACCIDConfigFile);
if (LAPPDStoreReadInVerbosity > 1)
{
// print the ACCID config records for debug
cout << "Loaded LAPPD ID Config Records from " << ACCIDConfigFile << ":" << endl;
for (const auto& record : idConfigRecords) {
cout << "RunNumber: " << record.RunNumber
<< ", ACCID: " << record.ACCID
<< ", ManufacturerID: " << record.ManufacturerID
<< ", Position: " << record.Position << endl;
}

int testRunNum = 5907;
int testManuID = 39; // example ManufacturerID to query
auto result= queryNearestACCID(idConfigRecords, testRunNum, testManuID);
int nearestACCID = std::get<0>(result);
std::string position = std::get<1>(result);
Comment thread
marc1uk marked this conversation as resolved.
cout << "Querying nearest ACCID for RunNumber: " << testRunNum << ", ManufacturerID: " << testManuID << endl;
cout << "Nearest ACCID: " << nearestACCID << ", Position: " << position << endl;
}

return true;
}

Expand Down Expand Up @@ -187,6 +215,7 @@ bool LAPPDLoadStore::Execute()

CleanDataObjects();
m_data->CStore.Set("LAPPD_new_event", false);
LoadRunInfo();

if (MultiLAPPDMap)
{
Expand Down Expand Up @@ -274,9 +303,9 @@ bool LAPPDLoadStore::Execute()
if (frametype == num_vector_data && loadPSEC)
{
m_data->CStore.Set("LAPPDanaData", true);
bool parsData = ParsePSECData();
LoadRunInfo();
runInfoLoaded = true;
bool parsData = ParsePSECData();
runInfoLoaded = true;
LAPPDana = parsData;
m_data->CStore.Set("LAPPDana", LAPPDana);
m_data->CStore.Set("LoadingPPS", false);
Expand Down Expand Up @@ -353,6 +382,15 @@ bool LAPPDLoadStore::Execute()
ReadBoards = dat.BoardIndex; // From the data, board index is not related to the LAPPD_ID! WHY use this way?
Raw_buffer = dat.RawWaveform;
LAPPD_ID = dat.LAPPD_ID;

if (LAPPD_ID>20) {
tuple<int, string> queryResult = queryNearestACCID(idConfigRecords, runNumber, LAPPD_ID);
if (LAPPDStoreReadInVerbosity > 2)
cout << "LAPPDLoadStore: Mapped ManufacturerID " << LAPPD_ID << " to ACCID " << get<0>(queryResult) << " for run " << runNumber << endl;

LAPPD_ID = get<0>(queryResult);
}

if (LAPPD_ID != SelectedLAPPD && SelectSingleLAPPD)
continue;

Expand Down Expand Up @@ -877,6 +915,15 @@ bool LAPPDLoadStore::LoadData()
return false;
}
LAPPD_ID = dat.LAPPD_ID;

if (LAPPD_ID>20) {
tuple<int, string> queryResult = queryNearestACCID(idConfigRecords, runNumber, LAPPD_ID);
if (LAPPDStoreReadInVerbosity > 2)
cout << "LAPPDLoadStore: Mapped ManufacturerID " << LAPPD_ID << " to ACCID " << get<0>(queryResult) << " for run " << runNumber << endl;

LAPPD_ID = get<0>(queryResult);
}

if (LAPPD_ID != SelectedLAPPD && SelectSingleLAPPD)
return false;
m_data->CStore.Set("PsecTimestamp", dat.Timestamp);
Expand Down Expand Up @@ -918,6 +965,15 @@ bool LAPPDLoadStore::LoadData()
ReadBoards = dat.BoardIndex;
Raw_buffer = dat.RawWaveform;
LAPPD_ID = dat.LAPPD_ID;

if (LAPPD_ID>20) {
tuple<int, string> queryResult = queryNearestACCID(idConfigRecords, runNumber, LAPPD_ID);
if (LAPPDStoreReadInVerbosity > 2)
cout << "LAPPDLoadStore: Mapped ManufacturerID " << LAPPD_ID << " to ACCID " << get<0>(queryResult) << " for run " << runNumber << endl;

LAPPD_ID = get<0>(queryResult);
}

if (LAPPD_ID != SelectedLAPPD && SelectSingleLAPPD)
return false;
m_data->CStore.Set("PsecTimestamp", dat.Timestamp);
Expand Down Expand Up @@ -1108,7 +1164,10 @@ bool LAPPDLoadStore::ParsePSECData()
// insert the data start with channel number 30*ReadBoards[bi]
// for instance, when bi=0 , LAPPD ID = 2, ReadBoards[bi] = 4, insert to channel number start with 120, to 150
int channelShift = bi * NUM_CH + LAPPD_ID * NUM_CH * 2;
retval = getParsedData(Parse_buffer, channelShift); //(because there are only 2 boards, so it's 0*30 or 1*30). Inserting the channel number start from this then ++ to 30
if (LAPPDStoreReadInVerbosity > 2)
std::cout << "bi= " << bi << ", LAPPD_ID= " << LAPPD_ID << ", NUM_CH= " << NUM_CH << ", channelShift= " << channelShift << std::endl;

retval = getParsedData(Parse_buffer, channelShift); //(because there are only 2 boards, so it's 0*30 or 1*30). Inserting the channel number start from this then ++ to 30
if (retval == 0)
{
if (LAPPDStoreReadInVerbosity > 2)
Expand Down Expand Up @@ -1637,4 +1696,65 @@ void LAPPDLoadStore::LoadRunInfo()
}
if (LAPPDStoreReadInVerbosity > 0)
cout << "LAPPDStoreReadIn, Loaded run info, runNumber: " << runNumber << ", subRunNumber: " << subRunNumber << ", partFileNumber: " << partFileNumber << ", eventNumberInPF: " << eventNumberInPF << endl;

if (runNumber<1) {
if (LAPPDStoreReadInVerbosity > 1)
cout << "LAPPDStoreReadIn, runNumber is "<< runNumber << ", trying to get from ANNIEEvent Store" << endl;

m_data->Stores["ANNIEEvent"]->Get("RunNumber", runNumber);
m_data->Stores["ANNIEEvent"]->Get("SubRunNumber", subRunNumber);
m_data->Stores["ANNIEEvent"]->Get("PartNumber", partFileNumber);

if (LAPPDStoreReadInVerbosity > 1)
cout << "LAPPDStoreReadIn, Got run info from ANNIEEvent Store, runNumber: " << runNumber << ", subRunNumber: " << subRunNumber << ", partFileNumber: " << partFileNumber << endl;
}
}

vector<IDConfigRecord> LAPPDLoadStore::LoadIDConfig(const string& filename) {
vector<IDConfigRecord> data;
ifstream file(filename);
if (!file.is_open()) {
cerr << "LAPPDLoadStore::LoadIDConfig: Can't open file: " << filename << endl;
return data;
}

string line;
getline(file, line); // skip header

while (getline(file, line)) {
if (line.empty()) continue;
replace(line.begin(), line.end(), '\t', ','); // support tab or comma
stringstream ss(line);
string token;
IDConfigRecord r;

getline(ss, token, ','); r.RunNumber = stoi(token);
getline(ss, token, ','); r.ACCID = stoi(token);
getline(ss, token, ','); r.ManufacturerID = stoi(token);
getline(ss, token, ','); r.Position = token;

data.push_back(r);
}

return data;
}

tuple<int, string> LAPPDLoadStore::queryNearestACCID(const vector<IDConfigRecord>& data, int targetRun, int manufacturerID) {
int bestRun = -1;
int bestACCID = -1;
string bestPosition;

for (const auto& r : data) {
if (r.ManufacturerID == manufacturerID && r.RunNumber <= targetRun) {
if (r.RunNumber > bestRun) { // find the nearest run not exceeding targetRun
bestRun = r.RunNumber;
bestACCID = r.ACCID;
bestPosition = r.Position;
}
}
}

if (bestRun == -1)
return {-1, ""};
return {bestACCID, bestPosition};
}
Loading