#ifndef __JSUPPORT__JSUPPORTTOOLKIT__ #define __JSUPPORT__JSUPPORTTOOLKIT__ #include #include #include "TDirectory.h" #include "km3net-dataformat/online/JDAQClock.hh" #include "JDAQ/JDAQEvaluator.hh" #include "JDAQ/JDAQSummarysliceIO.hh" #include "JDAQ/JDAQTimesliceIO.hh" #include "JDAQ/JDAQUTCExtendedIO.hh" #include "JDAQ/JSupport.hh" #include "JTrigger/JTriggerParameters.hh" #include "JTools/JRange.hh" #include "JSupport/JFilenameSupportkit.hh" #include "JSupport/JFileScanner.hh" #include "JSupport/JTreeScanner.hh" #include "JSupport/JMultipleFileScanner.hh" #include "JSupport/JTriggerParametersSupportkit.hh" /** * \file * Support methods. * \author mdejong */ namespace JSUPPORT {} namespace JPP { using namespace JSUPPORT; } namespace JSUPPORT { using KM3NETDAQ::JDAQUTCExtended; /** * Type definition for frame index range. */ typedef JTOOLS::JRange JFrameIndexRange; /** * Type definition for DAQ UTC time range. */ typedef JTOOLS::JRange JDAQUTCTimeRange; /** * Get time duration of given UTC time range. * * \param utc UTC time range * \return time duration [s] */ inline double getTimeDuration(const JDAQUTCTimeRange& utc) { if (utc.is_valid()) return (utc.getUpperLimit().getTimeNanoSecond() - utc.getLowerLimit().getTimeNanoSecond()) * 1.0e-9; else return 0.0; } /** * Get run number for given file name of data taking run. * * \param file_name file name * \return run number */ template inline int getRunNumber(const std::string& file_name) { JFileScanner in(file_name.c_str()); if (in.hasNext()) return in.next()->getRunNumber(); else return -1; } /** * Get range of frame indices. * * \param in TTree scanner * \return frame index range */ template inline JFrameIndexRange getFrameIndexRange(JTreeScannerInterface& in) { if (!in.empty()) return JFrameIndexRange(in.begin()->getFrameIndex(), in.rbegin()->getFrameIndex()); else return JFrameIndexRange(0,0); } /** * Get range of frame indices for given file name of data taking run. * * \param file_name file name * \return frame index range */ template inline JFrameIndexRange getFrameIndexRange(const std::string& file_name) { JFrameIndexRange result(JFrameIndexRange::DEFAULT_RANGE()); TDirectory* dir = TDirectory::CurrentDirectory(); if (isROOTFile(file_name.c_str())) { JTreeScanner in(file_name); return getFrameIndexRange(in); } else { JFileScanner in(file_name.c_str()); while (in.hasNext()) { result.include(in.next()->getFrameIndex()); } } if (dir != NULL) { dir->cd(); // restore current working directory } return result; } /** * Get UTC time range. * * \return UTC time range */ inline JDAQUTCTimeRange getUTCTimeRange() { return JDAQUTCTimeRange(JDAQUTCExtended(), JDAQUTCExtended()); } /** * Get UTC time range. * * \param in TTree scanner * \return UTC time range */ template inline JDAQUTCTimeRange getUTCTimeRange(JTreeScannerInterface& in) { if (!in.empty()) return JDAQUTCTimeRange(in.begin()->getTimesliceStart(), in.rbegin()->getTimesliceStart()); else return getUTCTimeRange(); } /** * Get UTC time range for given file name of data taking run. * * \param file_name file name * \return UTC time range */ template inline JDAQUTCTimeRange getUTCTimeRange(const std::string& file_name) { JDAQUTCTimeRange result(JDAQUTCTimeRange::DEFAULT_RANGE()); TDirectory* dir = TDirectory::CurrentDirectory(); if (isROOTFile(file_name.c_str())) { JTreeScanner in(file_name); result = getUTCTimeRange(in); } else { for (JFileScanner in(file_name.c_str()); in.hasNext(); ) { result.include(in.next()->getTimesliceStart()); } } if (dir != NULL) { dir->cd(); // restore current working directory } return result; } /** * Get UTC time range for given file name of data taking run. * Get data taking live time. * * \param __begin begin of file list * \param __end end of file list * \return UTC time range */ template inline JDAQUTCTimeRange getUTCTimeRange(T __begin, T __end) { JDAQUTCTimeRange result(JDAQUTCTimeRange::DEFAULT_RANGE()); for (T i = __begin; i != __end; ++i) { JDAQUTCTimeRange buffer = getUTCTimeRange(*i); if (buffer == getUTCTimeRange()) { buffer = getUTCTimeRange(*i); } result.combine(buffer); } return result; } /** * Get data taking live time. * * \param file_name file name * \return live time [s] */ inline double getLivetime(const std::string& file_name) { using namespace KM3NETDAQ; using namespace JTRIGGER; TDirectory* dir = TDirectory::CurrentDirectory(); double livetime = 0.0; try { const JTriggerParameters parameters = getTriggerParameters(file_name); if (parameters.writeSummary.prescale != 0) { JTreeScanner in(file_name); livetime = in.getEntries() * getFrameTime() * 1.0e-9 / parameters.writeSummary.prescale; } } catch(const std::exception& error) {} if (dir != NULL) { dir->cd(); // restore current working directory } return livetime; } /** * Get data taking live time. * * \param __begin begin of file list * \param __end end of file list * \return live time [s] */ template inline double getLivetime(T __begin, T __end) { double livetime = 0.0; for (T i = __begin; i != __end; ++i) { livetime += getLivetime(*i); } return livetime; } /** * Get data taking live time. * * \param file_list file list * \return live time [s] */ inline double getLivetime(const JMultipleFileScanner_t& file_list) { return getLivetime(file_list.begin(), file_list.end()); } } #endif