#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; namespace RAT { Processor::Result RetriggerCut::DSEvent(DS::Run&, DS::Entry& ds) { bool pass = true; // If any triggered event fails, they all fail for( size_t iEV = 0; iEV < ds.GetEVCount(); iEV++ ) if( Event(ds, ds.GetEV(iEV)) != OKTRUE ) pass = false; return pass ? OKTRUE : OKFALSE; } Processor::Result RetriggerCut::Event(DS::Entry&, DS::EV& ev) { fPassFlag = true; if (foundCut) { if(RetriggerIdentifier(ev)) { fPassFlag = false; fBit = retriggerBit; UpdateMask(ev); } } return fPassFlag ? OKFALSE : OKTRUE; } bool RetriggerCut::RetriggerIdentifier(DS::EV& ev) { std::vector::iterator retriggerFirstSearch = find (fFirstRetriggerReadout.begin(), fFirstRetriggerReadout.end(), ev.GetGTID()); std::vector::iterator retriggerLastSearch = find (fLastRetriggerReadout.begin(), fLastRetriggerReadout.end(), ev.GetGTID()); if (retriggerFirstSearch != fFirstRetriggerReadout.end()) {//If input event is first event fRetriggerCutGTID[0] = ev.GetGTID(); fPassFlag = false; fBit = firstEventBit;//Update mask for all the first event UpdateMask(ev); return false; } else if (retriggerLastSearch != fLastRetriggerReadout.end()) {//if input event is last event fRetriggerCutGTID[1] = ev.GetGTID(); bool correspond = CorrespondenceCheck(fRetriggerCutGTID, fFirstRetriggerReadout, fLastRetriggerReadout); if (!correspond) { warn << "RetriggerCut::CorrespondenceCheck: A Retrigger Cut failed Correspondence Check has been applied!" << "\n"; } fRetriggerCutGTID[0] = -1; fRetriggerCutGTID[1] = -1; return true; } else { //if first retrigger GTID is set, it means input event is a retrigger event, otherwise it's not a retrigger event. return (fRetriggerCutGTID[0] != -1); } } bool RetriggerCut::CorrespondenceCheck(int eventSet[2], std::vector& firstArray, std::vector& secondArray) { if ((eventSet[0] == -1) || (eventSet[1] == -1)) return false; std::vector::iterator firstArraySearch = find (firstArray.begin(), firstArray.end(), eventSet[0]); std::vector::iterator secondArraySearch = find (secondArray.begin(), secondArray.end(), eventSet[1]); int firstCheck = (int)(firstArray.end() - firstArraySearch);//The position of input first_event in the first event readout array int lastCheck = (int)(secondArray.end() - secondArraySearch);//The position of input last_event in the last event readout array if ((firstCheck == 0) || (lastCheck == 0)) {//event not found warn << "TPBurstCut::CorrespondenceCheck: Start of Burst or End of Burst Event not found!" << "\n"; return false; } else if (firstCheck != lastCheck) {//event position is not identical, so not correspondent. warn << "TPBurstCut::CorrespondenceCheck: Start of Burst and End of Burst Events not Correspondent!" << "\n"; return false; } else {//pass correspondence check. firstArray.erase(firstArraySearch);//For optimization, removing good pairs out of the readout secondArray.erase(secondArraySearch);//to reduce the search time for further call. return true; } } void RetriggerCut::BeginOfRun(DS::Run& run) { //Read Retrigger Cut Bits from DataCleaningBits std::string cutName = "retriggercut"; firstEventBit = DU::Utility::Get()->GetDataCleaningBits().GetBitIndex("firstevent"); retriggerBit = DU::Utility::Get()->GetDataCleaningBits().GetBitIndex("retriggercut"); bool foundDBTable = false; bool foundJSONFile = false; foundCut = false; try{ DBLinkPtr fData = DB::Get()->GetLink("LIVETIME_CUT","retriggercut"); if (fData->GetI("cutapplied") == 1) { fFirstRetriggerReadout = fData->GetIArray("firstburst"); fLastRetriggerReadout = fData->GetIArray("lastburst"); foundCut = true; } else { std::string readDetail = "RetriggerCut::BeginOfRun: No event is cut by " + cutName; detail << readDetail << endl; } foundDBTable = true; } catch(DBNotFoundError &e){ std::string readWarn = "RetriggerCut::BeginOfRun: Could not find " + cutName + " info, Looking for .json file"; warn << readWarn << endl; } if (!foundDBTable) { std::ostringstream fname; fname << "livetimecut_" << cutName << "_" << run.GetRunID() << ".json"; try { std::vector contents = DBJsonLoader::parse(fname.str()); json::Value firstEventTemp = contents[0]->GetJSON("firstburst"); json::Value lastEventTemp = contents[0]->GetJSON("lastburst"); foundJSONFile = true; if ((int)firstEventTemp.getArraySize() != 0 ) { foundCut = true; for (int j = 0; j < (int)firstEventTemp.getArraySize(); j++) { int firstgtid = firstEventTemp[j].cast(); int lastgtid = lastEventTemp[j].cast(); fFirstRetriggerReadout.push_back(firstgtid); fLastRetriggerReadout.push_back(lastgtid); } } else { std::string readJSONDetail = "RetriggerCut::BeginOfRun: No event is cut by " + cutName; detail << readJSONDetail << endl; } } catch(FileError &e){ std::string readJSONWarn = "RetriggerCut::BeginOfRun: Could not find " + cutName + ".json file"; warn << readJSONWarn << endl; } } if ((!foundDBTable) && (!foundJSONFile)) { std::string readNoInfo = "RetriggerCut::BeginOfRun: Could not find " + cutName + " info, " + cutName + " will not be applied.."; warn << readNoInfo << endl; } } } // namespace RAT