#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; namespace RAT { DQRunProc::DQRunProc() : DataQualityProc( "dqrunproc") { } DQRunProc::~DQRunProc() { } void DQRunProc::BeginOfRun( DS::Run& run ) { DataQualityProc::BeginOfRun( run ); fDQChecks = NULL; fFirstEventTime = 0; fLastEventTime = 0; fFirstEventCount10 = 0; fLastEventCount10 = 0; fFirstEventCount50 = 0; fLastEventCount50 = 0; firstEventTimeSet = false; fFailedTrigCheckCount = 0; fNhitCount = 0; // Get criteria, save as class members and to RATDB via JSON object fDQChecks = DB::Get()->GetLink( "DQCHECKS", "neutrino" ); fMCFlagCriteria = fDQChecks->GetI( "mc_flag_criteria" ); fCriteria[ "mc_flag_criteria" ] = fMCFlagCriteria; fTrigCheckCriteria = fDQChecks->GetI( "trigger_check_criteria" ); fCriteria[ "trigger_check_criteria" ] = fTrigCheckCriteria; fTrigCheckThresh = fDQChecks->GetD( "trigger_check_thresh" ); fCriteria[ "trigger_check_thresh" ] = fTrigCheckThresh; } Processor::Result DQRunProc::DSEvent(DS::Run&, DS::Entry& ds) { for( size_t iEV = 0; iEV < ds.GetEVCount(); iEV++ ) Event(ds, ds.GetEV(iEV)); return OK; } Processor::Result DQRunProc::Event( DS::Entry&, DS::EV& ev ) { fEventCount++; // Check 2 - Check trigger mask if event has no trigger bits in criteria fail event if( (ev.GetTrigType() & fTrigCheckCriteria) == 0) { detail << "Trigger word was: " << TriggerToString(ev.GetTrigType())< "; // Set first event universal time fFirstEventTime = eventTime; detail << fFirstEventTime << " "; fLastEventTime = fFirstEventTime; // set equal at start // Set first event 10 MHz clock count fFirstEventCount10 = clockCount10; detail << fFirstEventCount10 << " "; fLastEventCount10 = fFirstEventCount10; // Set first event 50 MHz clock count fFirstEventCount50 = clockCount50; detail << fFirstEventCount50 << " "; fLastEventCount50 = fFirstEventCount50; fEventTimes.push_back( 0 ); fEventCount10s.push_back( 0 ); fEventCount50s.push_back( 0 ); firstEventTimeSet = true; } else { if( ( eventTime != 0 ) || ( clockCount10 != 0 ) || ( clockCount50 != 0 ) ) // event has some timing information // try to update last event times { if( eventTime != 0 ) // update { fEventTimes.push_back( eventTime - fLastEventTime ); fLastEventTime = eventTime; } else // don't update fLastEventTime { debug << "DQRunProc::Event: universal time returned zero, " << "fLastEventTime was not updated" << newline; fEventTimes.push_back( 0 ); } if( clockCount10 != 0 ) // update { fEventCount10s.push_back( clockCount10 - fLastEventCount10 ); fLastEventCount10 = clockCount10; } else // don't update fFirstEventCount10 { debug << "DQRunProc::Event: 10 MHz clock counts returned zero, " << "fLastEventCount10 was not updated" << newline; fEventCount10s.push_back( 0 ); } if( clockCount50 != 0 ) // update { fEventCount50s.push_back( clockCount50 - fLastEventCount50 ); fLastEventCount50 = clockCount50; } else // don't update fFirstEventCount50 { debug << "DQRunProc::Event: 50 MHz clock counts returned zero, " << "fLastEventCount50 was not updated" << newline; fEventCount50s.push_back( 0 ); } } else // event has no available timing information { warn << "DQRunProc::Event: unable to calculate a valid event time" << newline; if( ev.GetGTID() == 0 ) detail << " --> GTID = 0, expected orphan event" << newline; } } fNhitCount += ev.GetNhits(); return OK; } bool DQRunProc::CheckTrigBit( const int triggerMask, const int criteriaMask, const int bit, int &failedBits ) const { bool bitPassedCheck = true; int queryMask = 1 << bit; if( ( ( triggerMask & queryMask ) != ( criteriaMask & queryMask ) ) && ( ( criteriaMask & queryMask ) == queryMask ) ) { bitPassedCheck = false; failedBits += queryMask; } return bitPassedCheck; } void DQRunProc::EndOfRun( DS::Run& run ) { // Check 1 - run type checks unsigned long int runType = 1; //unsigned long int runType = DB::Get()->GetLink("RUN", to_string( run.GetRunID() ) )->GetI("runtype"); char MCFlag = run.GetMCFlag(); ostringstream stringRunType; stringRunType << runType; if( runType == 1 ) // As defined in RUN.ratdb { detail << "DQRunProc::EndOfRun: " << "run " << GetRunID() << " passed run type check" << newline; fCheckResult = true; } else { detail << "DQRunProc::EndOfRun: run " << GetRunID() << " failed run type check" << newline; detail << " --> 'runType' = " << hex << runType; AddMaskToRATDB( "runType", runType ); detail << " (does not equal 1 - as defined in RUN.ratdb, " <<"for neutrino data)" << newline; fCheckResult = false; } Update( "run_type", fCheckResult ); if( MCFlag == fMCFlagCriteria ) // O = Data, 1 = MC { detail << "DQRunProc::EndOfRun: " << "run " << GetRunID() << " passed MC flag check" << newline; fCheckResult = true; } else { detail << "DQRunProc::EndOfRun: " << "run " << GetRunID() << " failed MC flag check" << newline; detail << " --> 'MCFlag' does not equal 0 (data)" << newline; fCheckResult = false; } AddToRATDB( "mc_flag_value", MCFlag ); Update( "mc_flag", fCheckResult ); // Check 2 - trigger check double trigCheckResult = 100.0 * ( 1.0 - (static_cast(fFailedTrigCheckCount ) / static_cast( fEventCount ) ) ); if( trigCheckResult >= fTrigCheckThresh ) { detail << "DQRunProc::EndOfRun: " << "run " << GetRunID() << " passed trigger check" << newline; fCheckResult = true; } else { detail << "DQRunProc::EndOfRun: " << "run " << GetRunID() << " failed trigger check" << newline; detail << " --> " << trigCheckResult << "% of events " << "(less than " << fTrigCheckThresh << "% threshold) " << "had the correct active triggers" << newline; fCheckResult = false; } AddToRATDB("trigger_pass_percentage", trigCheckResult); AddToRATDB("trigger_fail_count", fFailedTrigCheckCount); Update( "trigger", fCheckResult ); // Check 3 - run time checks map runLengths; // Universal time run length runLengths["universal_time"] = static_cast( fLastEventTime - fFirstEventTime ); // 10 MHz clock run length - to seconds runLengths["count_10"] = static_cast( fLastEventCount10 - fFirstEventCount10 ) * 0.1e-6; // 50 MHz clock run length - to seconds runLengths["count_50"] = static_cast( fLastEventCount50 - fFirstEventCount50 ) * 0.02e-6; // Choose appropriate run length detail << "DQRunPRoc::EndOfRun: calculated run lengths are --> "; double runLength; bool runLengthSet = false; for( map::iterator iRunLength = runLengths.begin(); iRunLength != runLengths.end(); iRunLength++ ) { detail << iRunLength->second << ", "; AddToRATDB( iRunLength->first + "_run_length", iRunLength->second ); if( ( iRunLength->second > 0.0 ) && !runLengthSet ) { runLength = iRunLength->second; runLengthSet = true; AddToRATDB( "run_length", runLength ); // Save how the chosen run length was calculated AddToRATDB( "run_length_source", iRunLength->first ); } } detail << newline; // If run length was still not set i.e. all <= 0, use last one if( !runLengthSet ) { runLength = 0.0; AddToRATDB( "run_length", runLength ); // Save error comment AddToRATDB( "run_length_source", "ERROR: unable to calculate valid run length" ); } run.SetRunLength( runLength ); // Transient run length // Create and save event timestamp distributions fEventTimeHist = new TH1D( "eventTimeHist", "Distribution of universal time event delta-t", fEventTimes.size() + 1, 0, fEventTimes.size() ); fEventCount10Hist = new TH1D( "eventCount10Hist", "Distribution of 10 MHz clock event delta-t", fEventCount10s.size() + 1, 0, fEventCount10s.size() ); fEventCount50Hist = new TH1D( "eventCount50Hist", "Distribution of 50 MHz clock event delta-t", fEventCount50s.size() + 1, 0, fEventCount50s.size() ); for( vector::iterator iEventTime = fEventTimes.begin(); iEventTime != fEventTimes.end(); iEventTime++ ) { int bin = distance( fEventTimes.begin(), iEventTime ); if( *iEventTime <= 0.0 ) // fill value to see how negative it is fEventTimeHist->SetBinContent( bin, *iEventTime ); else // just fill one entry per bin fEventTimeHist->SetBinContent( bin, 1.0 ); } for( vector::iterator iEventCount10 = fEventCount10s.begin(); iEventCount10 != fEventCount10s.end(); iEventCount10++ ) { int bin = distance( fEventCount10s.begin(), iEventCount10 ); fEventCount10Hist->SetBinContent( bin, *iEventCount10 ); } for( vector::iterator iEventCount50 = fEventCount50s.begin(); iEventCount50 != fEventCount50s.end(); iEventCount50++ ) { int bin = distance( fEventCount50s.begin(), iEventCount50 ); fEventCount50Hist->SetBinContent( bin, *iEventCount50 ); } WriteToRoot( fEventTimeHist, "eventTimeHist" ); WriteToRoot( fEventCount10Hist, "eventCount10Hist" ); WriteToRoot( fEventCount50Hist, "eventCount50Hist" ); // clear up delete fEventTimeHist; fEventTimeHist = NULL; delete fEventCount10Hist; fEventCount10Hist = NULL; delete fEventCount50Hist; fEventCount50Hist = NULL; // Other stuff float meanNhit = static_cast( fNhitCount ) / static_cast( fEventCount ); AddToRATDB( "mean_nhit", meanNhit ); DataQualityProc::EndOfRun( run ); } }// namespace RATDB