#include #include #include #include #include #include #include #include using std::vector; namespace RAT { void PedCut::BeginOfRun(DS::Run&) { fLClean = DB::Get()->GetLink("DATACLEANING",fName); fTimeWindow = static_cast(fLClean->GetD("time_window")); fCounter = 0; fInWindow = false; // Initialize various DB/Table/Column names fDBName = "PedCut.ratdb"; fDBTableName = "PEDCUT"; fDBInitialTimeColumnName = "initial_pedestal_times"; fDBFinalTimeColumnName = "final_pedestal_times"; if(fProcessPass == 2) { bool foundDBFile = false; try { DBLinkPtr PedCutDB = DB::Get()->GetLink(fDBTableName); fInitialPedestalTimes = PedCutDB->GetDArray(fDBInitialTimeColumnName); fFinalPedestalTimes = PedCutDB->GetDArray(fDBFinalTimeColumnName); foundDBFile = true; } catch (const DBNotFoundError &e) { warn << "PedCut::BeginOfRun: Couldn't access DB." "Attempting to use local file.\n"; foundDBFile = false; } if(!foundDBFile) { try { std::vector contents = DBJsonLoader::parse(fDBName); fInitialPedestalTimes = contents[0]->GetDArray(fDBInitialTimeColumnName); fFinalPedestalTimes = contents[0]->GetDArray(fDBFinalTimeColumnName); } catch (const FileError &e) { warn << "PedCut::BeginOfRun: " "Couldn't find file of for two pass ped cut" "Results of this cut won't be useful\n"; } catch (const DBWrongTypeError &e) { // Will occur if there were no pedstals in run. // Nothing to do. } } for(size_t i=0; i < fFinalPedestalTimes.size(); i++) { fFinalPedestalTimes[i] += fTimeWindow; } for(size_t i=0; i < fInitialPedestalTimes.size(); i++) { fInitialPedestalTimes[i] -= fTimeWindow; } } } Processor::Result PedCut::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 PedCut::Event(DS::Entry&, DS::EV& ev) { int ped_mask = 1< fTimeWindow) { fFinalPedestalTimes.push_back(fLastPedestalTime); fInWindow = false; } } else { // First make sure you haven't already processed all the ped windows const size_t initial_size = fInitialPedestalTimes.size(); const size_t final_size = fFinalPedestalTimes.size(); if (initial_size < fCounter || final_size < fCounter || initial_size == 0 || final_size == 0 ) { UpdateMask(ev); return fPassFlag ? OKFALSE : OKTRUE; } // Check if you've finished the most recent ped window if (time > fFinalPedestalTimes[fCounter] && fCounter+1 < final_size) { fCounter++; } // Then check if you're in a pedestal window if(time >= fInitialPedestalTimes[fCounter] && time <= fFinalPedestalTimes[fCounter]) { fPassFlag = false; } } // now update the mask UpdateMask(ev); return fPassFlag ? OKFALSE : OKTRUE; } void PedCut::EndOfRun(DS::Run& run) { if (fProcessPass == 1) { if(fInWindow) { // This should happen if a pedestal event ocurred within the // last second of a run fFinalPedestalTimes.push_back(fLastPedestalTime); fInWindow = false; } // Now store the results in a ratdb file DBTable table(fDBTableName); table.SetI("version", 1); table.SetPassNumber(-1); table.SetRunRange(run.GetRunID(), run.GetRunID()); table.SetDArray("initial_pedestal_times", fInitialPedestalTimes); table.SetDArray("final_pedestal_times", fFinalPedestalTimes); table.SaveAs(fDBName); } } } // namespace RAT