diff --git a/include/ACC.h b/include/ACC.h index 4e23bbb..5751709 100644 --- a/include/ACC.h +++ b/include/ACC.h @@ -50,7 +50,7 @@ class ACC void emptyUsbLine(); //attempting to remove the crashes due to non-empty USB lines at startup. void writeErrorLog(string errorMsg); void writePsecData(ofstream& d, vector boardsReadyForRead); - void writeRawDataToFile(vector buffer, ofstream& d); + void writeRawDataToFile(vector buffer, string rawfn); //-----short usb send functions. found //-----at the end of the cpp file. void setHardwareTrigSrc(int src, unsigned int boardMask = 0xFF); @@ -60,11 +60,45 @@ class ACC stdUSB* getUsbStream(); //returns the private usb object //Set functions for trigger - void setDetectionMode(int in){detectionMode = in;} - void setInvertMode(int in){invertMode = in;} - void setChCoin(unsigned int in){ChCoin = in;} - void setEnableCoin(int in){enableCoin = in;} - void setThreshold(int in){threshold = in;} + void setDetectionMode(int in, int source) + { + if(source==2) + { + ACC_detection_mode = in; + }else if(source==3) + { + ACDC_detection_mode = in; + }else if(source==4) + { + SELF_detection_mode = in; + } + } + + void setSign(int in, int source) + { + if(source==0) + { + ACC_sign = in; + }else if(source==1) + { + ACDC_sign = in; + }else if(source==2) + { + SELF_sign = in; + } + } + + void setNumChCoin(unsigned int in){SELF_number_channel_coincidence = in;} + + void setEnableCoin(int in){SELF_coincidence_onoff = in;} + + void setThreshold(unsigned int in){SELF_threshold = in;} + + void setPsecChipMask(vector in){SELF_psec_channel_mask = in;} + void setPsecChannelMask(vector in){SELF_psec_chip_mask = in;} + + void setValidationWindow(unsigned int in){validation_window=in;} + void setTriggermode(int in){trigMode = in;} int getTriggermode(){return trigMode;} @@ -78,12 +112,19 @@ class ACC vector lastAccBuffer; //most recently received ACC buffer vector alignedAcdcIndices; //number relative to ACC index (RJ45 port) vector acdcs; //a vector of active acdc boards. - int detectionMode; - int invertMode; - int enableCoin; + int ACC_detection_mode; + int ACC_sign; + int ACDC_detection_mode; + int ACDC_sign; + int SELF_detection_mode; + int SELF_sign; + int SELF_coincidence_onoff; int trigMode; - unsigned int ChCoin; - unsigned int threshold; + vector SELF_psec_channel_mask; + vector SELF_psec_chip_mask; + unsigned int SELF_number_channel_coincidence; + unsigned int SELF_threshold; + unsigned int validation_window; map>> map_data; map> map_meta; diff --git a/lib/ACC.cpp b/lib/ACC.cpp index 585f4ee..277b2e2 100644 --- a/lib/ACC.cpp +++ b/lib/ACC.cpp @@ -551,8 +551,7 @@ int ACC::readAcdcBuffers(bool raw, string timestamp) if(raw==true) { string rawfn = outfilename + "Raw_" + timestamp + "_b" + to_string(bi) + ".txt"; - ofstream rawofs(rawfn.c_str(), ios::app); //trunc overwrites - writeRawDataToFile(acdc_buffer, rawofs); + writeRawDataToFile(acdc_buffer, rawfn); return 0; } @@ -662,8 +661,20 @@ int ACC::listenForAcdcData(int trigMode, bool raw, string timestamp) //time the listen fuction now = chrono::steady_clock::now(); if(chrono::duration_cast(now - lastPrint) > printDuration) - { - cout << "Have been waiting for a trigger for " << chrono::duration_cast(now - start).count() << " seconds" << endl; + { + string err_msg = "Have been waiting for a trigger for "; + err_msg += to_string(chrono::duration_cast(now - start).count()); + err_msg += " seconds"; + writeErrorLog(err_msg); + for(int i=0; i(now - start) > timeoutDuration) @@ -683,7 +694,7 @@ int ACC::listenForAcdcData(int trigMode, bool raw, string timestamp) bool usbcheck = usb->sendData(command); if(usbcheck==false) { - cout << "Emptying the usb lines" << endl; + writeErrorLog("Emptying the usb lines"); emptyUsbLine(); } @@ -763,8 +774,7 @@ int ACC::listenForAcdcData(int trigMode, bool raw, string timestamp) if(raw==true) { string rawfn = outfilename + "Raw_" + timestamp + "_b" + to_string(bi) + ".txt"; - ofstream rawofs(rawfn.c_str(), ios::app); //trunc overwrites - writeRawDataToFile(acdc_buffer, rawofs); + writeRawDataToFile(acdc_buffer, rawfn); return 0; } @@ -861,63 +871,43 @@ int ACC::initializeForDataReadout(int trigMode, unsigned int boardMask, int cali setHardwareTrigSrc(trigMode,boardMask); command = 0xFFB30000; - command = (command & (command | (boardMask << 24))) | invertMode; + command = (command & (command | (boardMask << 24))) | ACC_sign; usb->sendData(command); command = 0xFFB31000; - command = (command & (command | (boardMask << 24))) | detectionMode; + command = (command & (command | (boardMask << 24))) | ACC_detection_mode; usb->sendData(command); break; case 3: //SMA trigger ACDC setHardwareTrigSrc(trigMode,boardMask); command = 0xFFB20000; - command = (command & (command | (boardMask << 24))) | invertMode; + command = (command & (command | (boardMask << 24))) | ACDC_sign; usb->sendData(command); command = 0xFFB21000; - command = (command & (command | (boardMask << 24))) | detectionMode; + command = (command & (command | (boardMask << 24))) | ACDC_detection_mode; usb->sendData(command); break; case 4: //Self trigger setHardwareTrigSrc(trigMode,boardMask); - command = 0xFFB10000; - for(int masknum=0; masknum<5; masknum++) - { - command = (command & (command | (boardMask << 24))) | (masknum << 12) | 0xFF; - usb->sendData(command); - } - - command = 0xFFB15000; - command = command | ChCoin; - usb->sendData(command); - - command = 0xFFB16000; - command = command | invertMode; - usb->sendData(command); - - command = 0xFFB17000; - command = command | detectionMode; - usb->sendData(command); - - command = 0xFFB18000; - command = command | enableCoin; - usb->sendData(command); - - command = 0xFFA60000; - command = command | (0x1F << 12) | threshold; - usb->sendData(command); - + goto selfsetup; break; case 5: //Self trigger with SMA validation on ACC setHardwareTrigSrc(trigMode,boardMask); - command = 0xFFB31001; + command = 0xFFB30000; + command = command | ACC_sign; + usb->sendData(command); + + command = 0xFFB31000; + command = command | ACC_detection_mode; usb->sendData(command); command = 0xFFB40000; usb->sendData(command); - command = 0xFFB41280; + command = 0xFFB41000; + command = command | validation_window; usb->sendData(command); goto selfsetup; @@ -925,12 +915,18 @@ int ACC::initializeForDataReadout(int trigMode, unsigned int boardMask, int cali case 6: //Self trigger with SMA validation on ACDC setHardwareTrigSrc(trigMode,boardMask); - command = 0xFFB31001; + command = 0xFFB20000; + command = command | ACDC_sign; + usb->sendData(command); + + command = 0xFFB21000; + command = command | ACDC_detection_mode; usb->sendData(command); command = 0xFFB40000; usb->sendData(command); - command = 0xFFB41280; + command = 0xFFB41000; + command = command | validation_window; usb->sendData(command); goto selfsetup; @@ -940,74 +936,82 @@ int ACC::initializeForDataReadout(int trigMode, unsigned int boardMask, int cali command = 0xFFB40000; usb->sendData(command); - command = 0xFFB41280; + command = 0xFFB41000; + command = command | validation_window; usb->sendData(command); command = 0xFFB20000; - command = (command & (command | (boardMask << 24))) | invertMode; + command = (command & (command | (boardMask << 24))) | ACDC_sign; usb->sendData(command); command = 0xFFB21000; - command = (command & (command | (boardMask << 24))) | detectionMode; + command = (command & (command | (boardMask << 24))) | ACDC_detection_mode; usb->sendData(command); command = 0xFFB30000; - command = (command & (command | (boardMask << 24))) | invertMode; + command = (command & (command | (boardMask << 24))) | ACC_sign; usb->sendData(command); command = 0xFFB31000; - command = (command & (command | (boardMask << 24))) | detectionMode; + command = (command & (command | (boardMask << 24))) | ACC_detection_mode; usb->sendData(command); + break; case 8: setHardwareTrigSrc(trigMode,boardMask); command = 0xFFB40000; usb->sendData(command); - command = 0xFFB41280; + command = 0xFFB41000; + command = command | validation_window; usb->sendData(command); command = 0xFFB20000; - command = (command & (command | (boardMask << 24))) | invertMode; + command = (command & (command | (boardMask << 24))) | ACDC_sign; usb->sendData(command); command = 0xFFB21000; - command = (command & (command | (boardMask << 24))) | detectionMode; + command = (command & (command | (boardMask << 24))) | ACDC_detection_mode; usb->sendData(command); command = 0xFFB30000; - command = (command & (command | (boardMask << 24))) | invertMode; + command = (command & (command | (boardMask << 24))) | ACC_sign; usb->sendData(command); command = 0xFFB31000; - command = (command & (command | (boardMask << 24))) | detectionMode; + command = (command & (command | (boardMask << 24))) | ACC_detection_mode; usb->sendData(command); + break; default: // ERROR case writeErrorLog("Specified trigger is not known!"); break; selfsetup: command = 0xFFB10000; - for(int masknum=0; masknum<5; masknum++) + if(SELF_psec_chip_mask.size()!=SELF_psec_channel_mask.size()) { - command = (command & (command | (boardMask << 24))) | (masknum << 12) | 0xFF; + writeErrorLog("Selftrigger mask are not set correct!"); + } + for(int i=0; i<(int)SELF_psec_chip_mask.size(); i++) + { + command = (command & (command | (boardMask << 24))) | (SELF_psec_chip_mask[i] << 12) | SELF_psec_channel_mask[i]; usb->sendData(command); } - command = 0xFFB15000; - command = command | ChCoin; - usb->sendData(command); - command = 0xFFB16000; - command = command | invertMode; + command = command | SELF_sign; usb->sendData(command); command = 0xFFB17000; - command = command | detectionMode; + command = command | SELF_detection_mode; usb->sendData(command); + command = 0xFFB15000; + command = command | SELF_number_channel_coincidence; + usb->sendData(command); + command = 0xFFB18000; - command = command | enableCoin; + command = command | SELF_coincidence_onoff; usb->sendData(command); command = 0xFFA60000; - command = command | (0x1F << 12) | threshold; + command = command | (0x1F << 12) | SELF_threshold; usb->sendData(command); } @@ -1149,8 +1153,9 @@ void ACC::writeErrorLog(string errorMsg) //writes data from the presently stored event // to file assuming file has header already -void ACC::writeRawDataToFile(vector buffer, ofstream& d) +void ACC::writeRawDataToFile(vector buffer, string rawfn) { + ofstream d(rawfn.c_str(), ios::app); for(unsigned short k: buffer) { d << hex << k << " "; diff --git a/src/listenForData.cpp b/src/listenForData.cpp index dbc2138..83aa2b3 100644 --- a/src/listenForData.cpp +++ b/src/listenForData.cpp @@ -10,6 +10,7 @@ #include #include #include +#include string getTime() { @@ -20,6 +21,24 @@ string getTime() return ss.str(); } +void writeErrorLog(string errorMsg) +{ + std::string err = "errorlog.txt"; + std::cout << "------------------------------------------------------------" << std::endl; + std::cout << errorMsg << endl; + std::cout << "------------------------------------------------------------" << std::endl; + ofstream os_err(err, ios_base::app); + auto now = std::chrono::system_clock::now(); + auto in_time_t = std::chrono::system_clock::to_time_t(now); + std::stringstream ss; + ss << std::put_time(std::localtime(&in_time_t), "%m-%d-%Y %X"); + os_err << "------------------------------------------------------------" << std::endl; + os_err << ss.str() << endl; + os_err << errorMsg << endl; + os_err << "------------------------------------------------------------" << std::endl; + os_err.close(); +} + int main() { int retval; @@ -39,7 +58,16 @@ int main() int oscopeMode; int setup; int threshold; + stringstream ss3; + unsigned int valstr; string timestamp; + int run=0; + int validationWindow; + unsigned int psec_chip; + unsigned int psec_channel; + std::vector vec_psec_chip; + std::vector vec_psec_channel; + ACC acc; @@ -56,8 +84,8 @@ int main() std::cout << "(4) Self-trigger" << std::endl; std::cout << "(5) Self-trigger with validation ACC" << std::endl; std::cout << "(6) Self-trigger with validation ACDC" << std::endl; - std::cout << "(7) SMA ACC with SMA ACDC" << std::endl; - std::cout << "(8) SMA ACDC with SMA ACC" << std::endl; + std::cout << "(7) SMA ACC with validation SMA ACDC" << std::endl; + std::cout << "(8) SMA ACDC with validation SMA ACC" << std::endl; std::cin >> triggermode; cin.ignore(numeric_limits::max(),'\n'); @@ -70,81 +98,135 @@ int main() switch(triggermode) { case 2: - std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACC SMA?" << std::endl; - cin >> invertMode; - cin.ignore(numeric_limits::max(),'\n'); std::cout << "Use edge detect (0) or level detect (1) on ACC SMA?" << std::endl; cin >> detectionMode; cin.ignore(numeric_limits::max(),'\n'); + std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACC SMA?" << std::endl; + cin >> invertMode; + cin.ignore(numeric_limits::max(),'\n'); - acc.setInvertMode(invertMode); - acc.setDetectionMode(detectionMode); + acc.setSign(invertMode, 2); + acc.setDetectionMode(detectionMode, 2); flag = false; break; case 3: - std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACDC SMA?" << std::endl; - cin >> invertMode; - cin.ignore(numeric_limits::max(),'\n'); std::cout << "Use edge detect (0) or level detect (1) on ACDC SMA?" << std::endl; cin >> detectionMode; cin.ignore(numeric_limits::max(),'\n'); + std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACDC SMA?" << std::endl; + cin >> invertMode; + cin.ignore(numeric_limits::max(),'\n'); - acc.setInvertMode(invertMode); - acc.setDetectionMode(detectionMode); + acc.setSign(invertMode, 3); + acc.setDetectionMode(detectionMode, 3); flag = false; break; case 4: goto selfsetup; case 5: - std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACC SMA?" << std::endl; + std::cout << "Use edge detect (0) or level detect (1) on ACC SMA validation?" << std::endl; + cin >> detectionMode; + cin.ignore(numeric_limits::max(),'\n'); + std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACC SMA validation?" << std::endl; cin >> invertMode; cin.ignore(numeric_limits::max(),'\n'); - std::cout << "Use edge detect (0) or level detect (1) on ACC SMA?" << std::endl; - cin >> detectionMode; + std::cout << "How long should the validation window be from 0 to 25us?" << std::endl; + cin >> validationWindow; cin.ignore(numeric_limits::max(),'\n'); + ss3 << validationWindow; + ss3 >> std::hex >> valstr; - acc.setInvertMode(invertMode); - acc.setDetectionMode(detectionMode); + acc.setValidationWindow(valstr); + acc.setSign(invertMode, 2); + acc.setDetectionMode(detectionMode, 2); goto selfsetup; case 6: - std::cout << "Use normal polarity (0) or inverted polarity (1) on ACDC SMA?" << std::endl; + std::cout << "Use edge detect (0) or level detect (1) on ACDC SMA validation?" << std::endl; + cin >> detectionMode; + cin.ignore(numeric_limits::max(),'\n'); + std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACDC SMA validation?" << std::endl; cin >> invertMode; cin.ignore(numeric_limits::max(),'\n'); - std::cout << "Use edge detect (0) or level detect (1) on ACDC SMA?" << std::endl; - cin >> detectionMode; + std::cout << "How long should the validation window be from 0 to 25us?" << std::endl; + cin >> validationWindow; cin.ignore(numeric_limits::max(),'\n'); + ss3 << validationWindow; + ss3 >> std::hex >> valstr; - acc.setInvertMode(invertMode); - acc.setDetectionMode(detectionMode); + acc.setValidationWindow(valstr); + acc.setSign(invertMode, 3); + acc.setDetectionMode(detectionMode, 3); goto selfsetup; case 7: + std::cout << "Use edge detect (0) or level detect (1) on ACC SMA?" << std::endl; + cin >> detectionMode; + cin.ignore(numeric_limits::max(),'\n'); std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACC SMA?" << std::endl; cin >> invertMode; cin.ignore(numeric_limits::max(),'\n'); - std::cout << "Use edge detect (0) or level detect (1) on ACC SMA?" << std::endl; + + acc.setSign(invertMode, 2); + acc.setDetectionMode(detectionMode, 2); + + std::cout << "Use edge detect (0) or level detect (1) on ACDC SMA validation?" << std::endl; cin >> detectionMode; cin.ignore(numeric_limits::max(),'\n'); + std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACDC SMA validation?" << std::endl; + cin >> invertMode; + cin.ignore(numeric_limits::max(),'\n'); + + acc.setSign(invertMode, 3); + acc.setDetectionMode(detectionMode, 3); - acc.setInvertMode(invertMode); - acc.setDetectionMode(detectionMode); + std::cout << "How long should the validation window be from 0 to 25us?" << std::endl; + cin >> validationWindow; + cin.ignore(numeric_limits::max(),'\n'); + ss3 << validationWindow; + ss3 >> std::hex >> valstr; + + acc.setValidationWindow(valstr); flag = false; break; case 8: - std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACC SMA?" << std::endl; + std::cout << "Use edge detect (0) or level detect (1) on ACDC SMA?" << std::endl; + cin >> detectionMode; + cin.ignore(numeric_limits::max(),'\n'); + std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACDC SMA?" << std::endl; cin >> invertMode; cin.ignore(numeric_limits::max(),'\n'); - std::cout << "Use edge detect (0) or level detect (1) on ACC SMA?" << std::endl; + + acc.setSign(invertMode, 3); + acc.setDetectionMode(detectionMode, 3); + + std::cout << "Use edge detect (0) or level detect (1) on ACC SMA validation?" << std::endl; cin >> detectionMode; cin.ignore(numeric_limits::max(),'\n'); + std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) on ACC SMA validation?" << std::endl; + cin >> invertMode; + cin.ignore(numeric_limits::max(),'\n'); - acc.setInvertMode(invertMode); - acc.setDetectionMode(detectionMode); + acc.setSign(invertMode, 2); + acc.setDetectionMode(detectionMode, 2); + + std::cout << "How long should the validation window be from 0 to 25us?" << std::endl; + cin >> validationWindow; + cin.ignore(numeric_limits::max(),'\n'); + ss3 << validationWindow; + ss3 >> std::hex >> valstr; + + acc.setValidationWindow(valstr); flag = false; break; default: std::cout << " Trigger input not found " << std::endl; break; selfsetup: + std::cout << "Use edge detect (0) or level detect (1) for self trigger?" << std::endl; + cin >> detectionMode; + cin.ignore(numeric_limits::max(),'\n'); + std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge) for selftrigger?" << std::endl; + cin >> invertMode; + cin.ignore(numeric_limits::max(),'\n'); std::cout << "Enable coincidence for self trigger? (0/1)" << std::endl; cin >> enableCoin; cin.ignore(numeric_limits::max(),'\n'); @@ -157,14 +239,8 @@ int main() unsigned int hexstr; ss << ChCoin; ss >> std::hex >> hexstr; - acc.setChCoin(hexstr); + acc.setNumChCoin(hexstr); } - std::cout << "Use normal polarity (0, high level or rising edge) or inverted polarity (1, low level or falling edge)?" << std::endl; - cin >> invertMode; - cin.ignore(numeric_limits::max(),'\n'); - std::cout << "Use edge detect (0) or level detect (1)?" << std::endl; - cin >> detectionMode; - cin.ignore(numeric_limits::max(),'\n'); std::cout << "Set the threshold of the self trigger in adc counts from 0 to 4095" << std::endl; cin >> threshold; cin.ignore(numeric_limits::max(),'\n'); @@ -173,10 +249,33 @@ int main() ss2 << threshold; ss2 >> std::hex >> adcstr; + while(run<5) + { + std::cout << "Do you want to enable channels on Psec chip " << run << "? (0/1)" << std::endl; + cin >> psec_chip; + cin.ignore(numeric_limits::max(),'\n'); + if(psec_chip==1) + { + std::cout << "What channels of psec chip" << run << "do you want to enable? (From 0x00 to 0x3F, each bit representing a channel)" << std::endl; + cin >> psec_channel; + cin.ignore(numeric_limits::max(),'\n'); + }else + { + run++; + continue; + } + + vec_psec_chip.push_back(run); + vec_psec_channel.push_back(psec_chip); + run++; + } + acc.setThreshold(adcstr); acc.setEnableCoin(enableCoin); - acc.setInvertMode(invertMode); - acc.setDetectionMode(detectionMode); + acc.setSign(invertMode, 4); + acc.setDetectionMode(detectionMode, 4); + acc.setPsecChipMask(vec_psec_chip); + acc.setPsecChannelMask(vec_psec_channel); flag = false; break; } @@ -277,22 +376,22 @@ int main() failCounter=0; break; case 1: - std::cout << "Successfully found data and but buffer corrupted" << std::endl; + writeErrorLog("Successfully found data and but buffer corrupted"); acc.dumpData(); failCounter++; break; case 2: - std::cout << "No data found" << std::endl; + writeErrorLog("No data found"); acc.dumpData(); failCounter++; break; case 3: - std::cout << "Sigint failure" << std::endl; + writeErrorLog("Sigint failure"); acc.dumpData(); - failCounter++; + failCounter=50; break; default: - std::cout << "Unknown error" << std::endl; + writeErrorLog("Unknown error"); failCounter++; break; }