8 #include <utl/Reader.h>
9 #include <utl/TimeStamp.h>
10 #include <utl/ErrorLogger.h>
11 #include <utl/SaveCurrentTDirectory.h>
13 #include <fwk/CentralConfig.h>
15 #include <det/Detector.h>
16 #include <det/ValidityStamp.h>
18 #include <sdet/T2LifeROOTFileManager.h>
20 #include <boost/lexical_cast.hpp>
44 const double T2LifeROOTFileManager::fgSearchMapBinning = 10000;
54 if (fBranch.GetChild(
"verbosity")) {
55 fBranch.GetChild(
"verbosity").GetData(fVerbosity);
56 cout <<
" T2LifeROOTFileManager verbosity level = " << fVerbosity << endl;
60 fBranch.GetChild(
"file").GetData(fFileName);
63 TFile*
file = TFile::Open(fFileName.c_str(),
"READ");
64 if (!file || file->IsZombie()) {
66 err <<
" T2LifeROOTFileManager input file=" << fFileName
67 <<
" is corrupt! Up times will not be available!" << endl;
72 fDataTree = (TTree*)(file->Get(
"T2Life"));
77 fAvailableComponents.insert(
"station_in_acquisition");
80 TBranch*
const startB = fDataTree->GetBranch(
"gpsStart");
81 TBranch*
const endB = fDataTree->GetBranch(
"gpsStop");
84 startB->SetAddress(&gpsStart);
85 endB->SetAddress(&gpsEnd);
87 INFO_TERSE(
"initializing T2LifeROOTFileManager index map ... ");
89 for (
unsigned int i = 0; i < (
unsigned int)startB->GetEntries(); ++i) {
94 unsigned int index_start = (
unsigned int)(gpsStart / fgSearchMapBinning);
95 unsigned int index_end = (
unsigned int)(gpsEnd / fgSearchMapBinning);
97 if (gpsStart < 756950400 || gpsEnd < 756950400) {
100 if (fVerbosity >= 3) {
102 warn <<
" removing broken T2 period: start=" << gpsStart
103 <<
" end=" << gpsEnd;
112 if (gpsStart > gpsEnd) {
114 if (fVerbosity >= 3) {
115 cout <<
" strange T2 period: start=" << gpsStart
121 vector<unsigned int> iCurr;
122 if (index_end == index_start) {
123 iCurr.push_back(index_end);
125 for (
unsigned int iIndex = min(index_start, index_end);
126 iIndex <=
max(index_start, index_end); ++iIndex) {
127 iCurr.push_back(iIndex);
131 for (
unsigned int j = 0; j < iCurr.size(); ++j) {
133 int iIndex = iCurr[j];
135 if (fIndex.find(iIndex) == fIndex.end()) {
137 fIndex[iIndex] = make_pair(i, i);
141 if (i < fIndex[iIndex].first)
142 fIndex[iIndex].first = i;
144 if (i > fIndex[iIndex].
second)
145 fIndex[iIndex].second = i;
152 if (fVerbosity > 3) {
153 cout <<
" row of T2 files with wrong gps times: " << nT2bullshit << endl;
155 iIndex != fIndex.end(); ++iIndex) {
156 cout <<
" index-maxp-entry: bin=" << iIndex->first
157 <<
" start=" << iIndex->second.first
158 <<
" end=" << iIndex->second.second << endl;
166 info <<
"using T2 ROOT file: " << fFileName;
172 T2LifeROOTFileManager::GetOkFlag(
int& returnData,
173 const std::string& componentProperty,
174 const std::string& componentName,
179 fAvailableComponents.find(componentProperty)==fAvailableComponents.end()) {
180 return VManager::eNotFound;
185 warn <<
"*****************************************************\n"
186 " property=" << componentProperty <<
" component=" << componentName
188 for (IndexMap::const_iterator imap = componentIndex.begin();
189 imap != componentIndex.end(); ++imap) {
190 warn <<
"\"" << imap->first <<
"\"=\"" << imap->second <<
"\" ";
192 warn <<
") NOT found in data source for specified time!\n"
194 "*****************************************************";
197 return VManager::eFound;
201 if (fVerbosity > 2) {
202 cout <<
"called GetOkFlag for componentName=" << componentName
203 <<
" componentProperty=" << componentProperty <<
" and index map:\n";
204 for (IndexMap::const_iterator imap = componentIndex.begin();
205 imap != componentIndex.end(); ++imap) {
206 cout <<
" index=\"" << imap->first <<
"\" -> \"" << imap->second <<
"\"\n";
211 if (componentProperty ==
"station_in_acquisition") {
213 if (componentName ==
"is_in_acquisition") {
215 int dataId = GetStationIndex(componentIndex);
220 cout <<
" no stations=" << fStationList.size() <<
" id=" << dataId << endl;
224 if( (
unsigned int)dataId < fStationList.size())
225 returnData = fStationList[dataId];
229 cout <<
" IsInDAQ=" << returnData << endl;
232 return VManager::eFound;
237 return VManager::eNotFound;
242 T2LifeROOTFileManager::ReadData()
247 INFO(
" enter ReadData ");
250 if (fDataValidity.IsValid()) {
253 INFO (
" T2 data still valid ");
258 unsigned int now = det::Detector::GetInstance().GetTime().GetGPSSecond();
261 fStationList.clear();
264 unsigned int iBin = (
unsigned int)(now / fgSearchMapBinning);
267 if (iIndex == fIndex.end()) {
269 err <<
"Index not found! now/gps=" << now <<
", iBin=" << iBin;
274 unsigned int lower = iIndex->second.first;
275 unsigned int upper = iIndex->second.second;
278 if (fVerbosity > 2) {
280 info <<
" index: now=" << now
282 <<
" lower=" << lower
283 <<
" upper=" << upper;
290 TBranch*
const startB = fDataTree->GetBranch(
"gpsStart");
291 TBranch*
const endB = fDataTree->GetBranch(
"gpsStop");
294 startB->SetAddress(&gpsStart);
295 endB->SetAddress(&gpsEnd);
299 bool foundit =
false;
300 while (!foundit && upper >= lower) {
302 index = lower + (upper - lower)/2;
304 startB->GetEntry(index);
305 endB->GetEntry(index);
307 if (now >= gpsStart && now < gpsEnd) {
322 if (fVerbosity > 3) {
323 cout <<
" search loop: nSearch=" << nSearch
324 <<
" gpsStart=" << gpsStart
325 <<
" gpsEnd=" << gpsEnd
326 <<
" foundit=" << foundit
327 <<
" index=" << index
335 if (fVerbosity > 2) {
336 cout <<
" END search loop: "
337 <<
" foundit=" << foundit
338 <<
" index=" << index
345 UInt_t time_start = 0;
346 UInt_t time_stop = 0;
347 TBits* stationBits =
new TBits();
349 fDataTree->SetBranchAddress(
"gpsStart", &time_start);
350 fDataTree->SetBranchAddress(
"gpsStop", &time_stop);
351 fDataTree->SetBranchAddress(
"stationArray", &stationBits);
353 fDataTree->GetEntry(index);
355 const unsigned int nStations = stationBits->GetNbits();
358 fStationList.resize(nStations + 1);
359 for (
unsigned int iStation = 1; iStation <= nStations; ++iStation) {
361 if (stationBits->TestBitNumber(iStation))
362 fStationList[iStation] = 1;
364 fStationList[iStation] = 0;
374 if (fVerbosity > 2) {
375 cout <<
" ReadData result is valid from start: " << fDataValidity.GetStartTime().GetGPSSecond()
376 <<
" end: " << fDataValidity.GetStopTime().GetGPSSecond()
378 if (fVerbosity > 4) {
379 cout <<
" Stations UP:\n";
381 for (
unsigned int i = 0; i<fStationList.size(); ++i) {
382 if (fStationList[i]) {
386 cout << setw(4) << i;
417 T2LifeROOTFileManager::GetStationIndex(
const IndexMap& componentIndex)
420 IndexMap::const_iterator iStationId = componentIndex.find(
"stationId");
421 if (iStationId == componentIndex.end()) {
422 INFO_TERSE(
"Called T2LifeROOTFileManager with wrong number of arguments!");
426 return boost::lexical_cast<
int>(iStationId->second);
#define INFO_TERSE(message)
Macro for logging informational messages.
Class to manage Sd life time ROOT files.
#define INFO(message)
Macro for logging informational messages.
void Init()
Initialise the registry.
std::map< unsigned int, IndexEntry >::const_iterator ConstIndexIterator
A TimeStamp holds GPS second and nanosecond for some event.
#define REGISTER_S_MANAGER(_name_, _Type_)
#define WARNING(message)
Macro for logging warning messages.
std::map< unsigned int, IndexEntry >::iterator IndexIterator
std::map< std::string, std::string > IndexMap
#define ERROR(message)
Macro for logging error messages.
Status
Specifies success or (eventually) various possible failure modes.