diff --git a/DataModel/PsecData.h b/DataModel/PsecData.h index 621593b99..feb642f3d 100644 --- a/DataModel/PsecData.h +++ b/DataModel/PsecData.h @@ -11,6 +11,13 @@ using namespace std; +struct IDConfigRecord { + int RunNumber; + int ACCID; + int ManufacturerID; + string Position; +}; + class PsecData{ friend class boost::serialization::access; diff --git a/UserTools/ANNIEEventTreeMaker/ANNIEEventTreeMaker.cpp b/UserTools/ANNIEEventTreeMaker/ANNIEEventTreeMaker.cpp index 65e64a530..5c6dc815d 100644 --- a/UserTools/ANNIEEventTreeMaker/ANNIEEventTreeMaker.cpp +++ b/UserTools/ANNIEEventTreeMaker/ANNIEEventTreeMaker.cpp @@ -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) { @@ -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; + cout << "Nearest ACCID: " << nearestACCID << ", Position: " << position << endl; + } } // LAPPD reconstruction information @@ -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]); @@ -1531,11 +1527,20 @@ void ANNIEEventTreeMaker::FillLAPPDPulse() vector pulse0 = stripPulses.at(0); vector 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()); @@ -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()); @@ -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()); @@ -2731,9 +2752,6 @@ void ANNIEEventTreeMaker::FillLAPPDMCHitInfo() } } - - - vector ANNIEEventTreeMaker::LoadIDConfig(const string& filename) { vector data; ifstream file(filename); @@ -2751,12 +2769,13 @@ vector 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; @@ -2782,8 +2801,6 @@ tuple ANNIEEventTreeMaker::queryNearestID(const vector ANNIEEventTreeMaker::queryNearestACCID(const vector& data, int targetRun, int manufacturerID) { int bestRun = -1; int bestACCID = -1; @@ -2803,4 +2820,3 @@ tuple ANNIEEventTreeMaker::queryNearestACCID(const vector 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); + cout << "Querying nearest ACCID for RunNumber: " << testRunNum << ", ManufacturerID: " << testManuID << endl; + cout << "Nearest ACCID: " << nearestACCID << ", Position: " << position << endl; + } + return true; } @@ -187,6 +215,7 @@ bool LAPPDLoadStore::Execute() CleanDataObjects(); m_data->CStore.Set("LAPPD_new_event", false); + LoadRunInfo(); if (MultiLAPPDMap) { @@ -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); @@ -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 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; @@ -877,6 +915,15 @@ bool LAPPDLoadStore::LoadData() return false; } LAPPD_ID = dat.LAPPD_ID; + + if (LAPPD_ID>20) { + tuple 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); @@ -918,6 +965,15 @@ bool LAPPDLoadStore::LoadData() ReadBoards = dat.BoardIndex; Raw_buffer = dat.RawWaveform; LAPPD_ID = dat.LAPPD_ID; + + if (LAPPD_ID>20) { + tuple 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); @@ -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) @@ -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 LAPPDLoadStore::LoadIDConfig(const string& filename) { + vector 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 LAPPDLoadStore::queryNearestACCID(const vector& 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}; } diff --git a/UserTools/LAPPDLoadStore/LAPPDLoadStore.h b/UserTools/LAPPDLoadStore/LAPPDLoadStore.h index 3deb5eea4..6b69bb6ec 100644 --- a/UserTools/LAPPDLoadStore/LAPPDLoadStore.h +++ b/UserTools/LAPPDLoadStore/LAPPDLoadStore.h @@ -46,6 +46,9 @@ class LAPPDLoadStore : public Tool void LoadRunInfo(); void SaveOffsets(); + vector LoadIDConfig(const string& filename); + tuple queryNearestACCID(const vector& data, int targetRun, int manufacturerID); + private: // This tool, control variables (only used in this tool, every thing that is not an data object) // Variables that you get from the config file @@ -143,6 +146,8 @@ class LAPPDLoadStore : public Tool std::vector LAPPDEventIndex_ID; // for each LAPPD ID, count the index of current loaded event // For example, this part file may have a ID=0 event, b ID=1 event, while loading data object c and loaded 3 ID=0 and 4 ID=1, I may have // LAPPDEventIndex_ID = {3, 4} + + vector idConfigRecords; // save PPS info for the second order correction std::map LAPPDBG_PPSBefore; @@ -174,6 +179,14 @@ class LAPPDLoadStore : public Tool int partFileNumber; int eventNumberInPF; std::ofstream debugStoreReadIn; + + // verbosity levels: if 'verbosity' < this level, the message type will be logged. + int v_error=0; + int v_warning=1; + int v_message=2; + int v_debug=3; + std::string logmessage; + int get_ok; }; #endif diff --git a/configfiles/EventBuilderV2/LAPPDLoadStoreConfig b/configfiles/EventBuilderV2/LAPPDLoadStoreConfig index daa420ebe..23507f01b 100644 --- a/configfiles/EventBuilderV2/LAPPDLoadStoreConfig +++ b/configfiles/EventBuilderV2/LAPPDLoadStoreConfig @@ -25,6 +25,7 @@ PSECinputfile /pnfs/annie/persistent/processed/LAPPD40Merged/FinalVersion_withRa Pedinputfile1 ../Pedestals/PEDS_ACDC_board0.txt Pedinputfile2 ../Pedestals/PEDS_ACDC_board1.txt +ACCIDConfigFile ./configfiles/LAPPDProcessedAna/LAPPDIDConfig.csv ReadStore 0 num_vector_data 7795 diff --git a/configfiles/EventBuilderV2_laser/LAPPDLoadStoreConfig b/configfiles/EventBuilderV2_laser/LAPPDLoadStoreConfig index daa420ebe..23507f01b 100644 --- a/configfiles/EventBuilderV2_laser/LAPPDLoadStoreConfig +++ b/configfiles/EventBuilderV2_laser/LAPPDLoadStoreConfig @@ -25,6 +25,7 @@ PSECinputfile /pnfs/annie/persistent/processed/LAPPD40Merged/FinalVersion_withRa Pedinputfile1 ../Pedestals/PEDS_ACDC_board0.txt Pedinputfile2 ../Pedestals/PEDS_ACDC_board1.txt +ACCIDConfigFile ./configfiles/LAPPDProcessedAna/LAPPDIDConfig.csv ReadStore 0 num_vector_data 7795 diff --git a/configfiles/LAPPDProcessedAna/Configs b/configfiles/LAPPDProcessedAna/Configs index e8ad7c851..ffaba8870 100644 --- a/configfiles/LAPPDProcessedAna/Configs +++ b/configfiles/LAPPDProcessedAna/Configs @@ -25,6 +25,7 @@ Nboards 6 #Number of pedestal files to be read in #PedinputfileTXT ../Pedestals/LAPPD40/P PedinputfileTXT /pnfs/annie/persistent/processed/processed_EBV2_LAPPDFiltered/Pedestal/2022_LAPPD40/P +ACCIDConfigFile ./configfiles/LAPPDProcessedAna/LAPPDIDConfig.csv ReadStorePdeFile 0 loadFromStoreDirectly 0 diff --git a/configfiles/LAPPDProcessedAna/LAPPDIDConfig.csv b/configfiles/LAPPDProcessedAna/LAPPDIDConfig.csv new file mode 100644 index 000000000..4e56a89aa --- /dev/null +++ b/configfiles/LAPPDProcessedAna/LAPPDIDConfig.csv @@ -0,0 +1,12 @@ +RunNumber,ACCID,ManufacturerID,Position +3600,0,40,Center +4300,1,63,UpperRight +4300,2,64,LowerLeft +4700,0,64,LowerLeft +4700,1,58,UpperRight +4700,2,39,Center +5600,0,151,LowerLeft +5870,1,58,UpperRight +5870,2,39,Center +6002,0,151,LowerLeft +6002,1,58,UpperRight diff --git a/configfiles/LAPPD_EB/Configs b/configfiles/LAPPD_EB/Configs index 0975b4615..37356ae48 100644 --- a/configfiles/LAPPD_EB/Configs +++ b/configfiles/LAPPD_EB/Configs @@ -23,6 +23,7 @@ Nboards 6 #Number of pedestal files to be read in PedinputfileTXT ../Pedestals/Laser2024Feb/P PSECinputfile /pnfs/annie/persistent/processed/LAPPD40Merged/FinalVersion_withRawTS/FilteredData_PMT_MRDtrack_noveto_15mV_7strips_3xxx_104 +ACCIDConfigFile ./configfiles/LAPPDProcessedAna/LAPPDIDConfig.csv Pedinputfile1 ../Pedestals/PEDS_ACDC_board0.txt Pedinputfile2 ../Pedestals/PEDS_ACDC_board1.txt