// ProduceRunTableProc.cc // Contact person: Javier Caravaca // See ProduceRunTableProc.hh for more details. //------------------------------------------------------------// #include #include #include #include #include #include using namespace RAT; #include #include #include #include ProduceRunTableProc::ProduceRunTableProc() : Processor("ProduceRunTableProc") { fRunNumber = -1; fRunTypeWord = -1; fFirstValidGTID = -1; fRunTableName = ""; setInvalidTimes(); } void ProduceRunTableProc::SetS( const std::string& param, const std::string& value ) { /* Store processed zdab file names in a vector */ if( param == "zdabs" ){ //Breakdown string in chunks std::istringstream iss(value); copy(std::istream_iterator(iss), std::istream_iterator(), std::back_inserter(fZdabFilenames)); } else throw ParamUnknown( param ); } void ProduceRunTableProc::BeginOfRun( DS::Run& run ) { /* Collect run information */ fRunNumber = run.GetRunID(); fRunTypeWord = run.GetRunType(); getRunTypeAndLengthFromDetectorDB(); if(fRunTypeWord != fRunTypeWordDB){ warn << " Run type words are different!: DS="<(ds.GetEV(0).GetUniversalTime().GetDays()); fStart_times[1] = static_cast(ds.GetEV(0).GetUniversalTime().GetSeconds()); fStart_times[2] = static_cast(ds.GetEV(0).GetUniversalTime().GetNanoSeconds()); } // Store time of the last non-orphan event // Assumes that the events are sorted by timing! if (ds.GetEV(0).GetTrigType() != 0) { // it's NOT an orphan fStop_times[0] = static_cast(ds.GetEV(0).GetUniversalTime().GetDays()); fStop_times[1] = static_cast(ds.GetEV(0).GetUniversalTime().GetSeconds()); fStop_times[2] = static_cast(ds.GetEV(0).GetUniversalTime().GetNanoSeconds()); } return Processor::OK; } void ProduceRunTableProc::EndOfRun( DS::Run& run ) { /* Compute run duration */ fRunLength = (fStop_times[0] - fStart_times[0])*60*60*24 + // days (fStop_times[1] - fStart_times[1]) + // secs (fStop_times[2] - fStart_times[2])*1e-9; // nsecs printSummary(); /* Check that the event time are OK and the * run duration makes sense */ checkTimes(); /* Create and upload RUN table */ RAT::DBTable *RUN_table = new DBTable("RUN"); RUN_table->SetRunRange(fRunNumber,fRunNumber); RUN_table->SetPassNumber(-1); RUN_table->SetS("index",""); RUN_table->SetI("version",3); RUN_table->SetI("runtype",fRunTypeWord); RUN_table->SetI("start_day",fStart_times[0]); RUN_table->SetI("start_sec",fStart_times[1]); RUN_table->SetD("start_nsc",fStart_times[2]); RUN_table->SetI("stop_day",fStop_times[0]); RUN_table->SetI("stop_sec",fStop_times[1]); RUN_table->SetD("stop_nsc",fStop_times[2]); RUN_table->SetSArray("subfile_list",fZdabFilenames); // Save locally just in case RUN_table->SaveAs(fRunTableName); info<<" ProduceRunTableProc: RUN_table saved as: "< 0){ try{ DB::Get()->SetServer("postgres://ratdbwriter@pgsql.snopl.us:5400/ratdb"); DB::Get()->UploadTableOnline(*RUN_table); } catch(...){ warn << "ProduceRunTableProc::EndOfRun: RATDB upload failed. " << newline; } } else{ warn << "ProduceRunTableProc::EndOfRun: Invalid RunType: " <0){ fRunTypeWordDB = atoi( PQgetvalue(qResult, 0, 0) ); } // Get run type sprintf(query, "SELECT timestamp,end_timestamp FROM run_state WHERE run = %d", fRunNumber); qResult = PQexec(detectorDB, query); if (PQresultStatus(qResult) != PGRES_TUPLES_OK){ warn<< " Query to DetectorDB failed. " << newline; } if(PQntuples(qResult)>0){ char *start_ts = PQgetvalue(qResult, 0, 0); char *end_ts = PQgetvalue(qResult, 0, 1); std::tm tm_start = {}, tm_end = {}; strptime(start_ts, "%Y-%m-%d %H:%M:%S", &tm_start); strptime(end_ts, "%Y-%m-%d %H:%M:%S", &tm_end); time_t start_time = mktime(&tm_start); time_t end_time = mktime(&tm_end); fRunLengthDB = end_time - start_time; if(fRunLengthDB < 0 || fRunLengthDB > 1e6) fRunLengthDB = -1.; //run can't be that long nor negative. This is a DB problem. } //Close connection PQclear(qResult); PQfinish(detectorDB); } void ProduceRunTableProc::printSummary(){ info<<" ProduceRunTableProc: Run Type = "<