9 #include <utl/Reader.h>
10 #include <utl/TimeStamp.h>
11 #include <utl/ErrorLogger.h>
12 #include <utl/SaveCurrentTDirectory.h>
14 #include <fwk/CentralConfig.h>
16 #include <det/Detector.h>
17 #include <det/ValidityStamp.h>
19 #include <fdet/FDetector.h>
20 #include <fdet/FdUpTimeFileManager.h>
42 const double FdUpTimeFileManager::fgSearchMapBinning = 10000;
49 fDataTree = (TTree*)(fRootFilePtr->Get(
"AugerUpTime"));
51 TBranch*
const startB = fDataTree->GetBranch(
"uptime_gpsStart");
52 startB->SetAddress(&fGpsStart);
54 TBranch*
const stopB = fDataTree->GetBranch(
"uptime_gpsStop");
55 stopB->SetAddress(&fGpsStop);
56 stopB->GetEntry(stopB->GetEntries() - 1);
58 fHeaderTree = (TTree*)(fRootFilePtr->Get(
"Header"));
60 TBranch*
const UpTimeVerB = fHeaderTree->GetBranch(
"SoftwareVersion");
61 UpTimeVerB->SetAddress(&fFileVersion);
62 UpTimeVerB->GetEntry(0);
67 err <<
" FdUpTimeFileManager input file=" << file->GetName()
68 <<
" is corrupt! Up times will not be available!";
70 fRootFilePtr =
nullptr;
90 fBranch.GetChild(
"file").GetData(fFileList);
93 for (fRootFileIterator = fFileList.begin(); fRootFileIterator != fFileList.end(); ++fRootFileIterator) {
95 TFile*
const rootFile = TFile::Open(fRootFileIterator->c_str(),
"READ");
96 if (!rootFile || rootFile->IsZombie()) {
98 err << fRootFileIterator->c_str()
99 <<
" is an UpTime file corrupted or inexistent!";
105 fUpTimeVector.push_back(rootUpTime);
109 if (fUpTimeVector.size() != fFileList.size()) {
111 err <<
"UpTime file(s) unavailable!";
115 if (fFileList.empty())
122 FdUpTimeFileManager::Initialize()
125 msg <<
"Initializing FdUpTimeFileManager index map ...";
126 auto verbosityB = fBranch.GetChild(
"verbosity");
128 verbosityB.GetData(fVerbosity);
129 msg <<
"\n\tverbosity level: " << fVerbosity;
134 fAvailableComponents.insert(
"has_fduptime");
135 fAvailableComponents.insert(
"uptime_gpsStart");
136 fAvailableComponents.insert(
"uptime_gpsStop");
137 fAvailableComponents.insert(
"uptime_fraction");
138 fAvailableComponents.insert(
"status");
139 fAvailableComponents.insert(
"veto_fraction");
140 fAvailableComponents.insert(
"ADCVariance");
141 fAvailableComponents.insert(
"Threshold");
142 fAvailableComponents.insert(
"Baseline");
146 for (fUpTimeVectorIterator = fUpTimeVector.begin(); fUpTimeVectorIterator != fUpTimeVector.end(); ++fUpTimeVectorIterator) {
149 TBranch*
const startB = fUpTimeVectorIterator->fDataTree->GetBranch(
"uptime_gpsStart");
151 startB->SetAddress(&gpsStart);
152 TBranch*
const stopB = fUpTimeVectorIterator->fDataTree->GetBranch(
"uptime_gpsStop");
154 stopB->SetAddress(&gpsStop);
156 for (
unsigned int i = 0; i < (
unsigned int)startB->GetEntries(); ++i) {
161 unsigned int bin1 = (
unsigned int)(gpsStart / fgSearchMapBinning);
162 unsigned int bin2 = (
unsigned int)(gpsStop / fgSearchMapBinning);
164 vector<unsigned int> iCurr;
165 iCurr.push_back(bin1);
167 iCurr.push_back(bin2);
169 Index& refIndex = fUpTimeVectorIterator->fFileVersion ? fIndexHeadered : fIndexUnheadered;
171 for (
unsigned int j = 0; j < iCurr.size(); ++j) {
172 if (refIndex.find(iCurr[j]) == refIndex.end() || refIndex[iCurr[j]].back().second.fRootFilePtr != fUpTimeVectorIterator->fRootFilePtr)
173 refIndex[iCurr[j]].push_back(make_pair(make_pair(i, i), *fUpTimeVectorIterator));
175 if (i < refIndex[iCurr[j]].back().first.first)
176 refIndex[iCurr[j]].back().first.first = i;
177 if (i > refIndex[iCurr[j]].back().first.second)
178 refIndex[iCurr[j]].back().first.second = i;
184 if (fVerbosity > 3) {
185 for (
int j = 0; j < 2; ++j) {
186 Index& verbRefIndex = !j ? fIndexHeadered : fIndexUnheadered;
187 cerr <<
"\t == " << (!j ?
"Headered" :
"Unheadered") <<
" files ==\n";
188 for (
auto iIndex = verbRefIndex.begin(); iIndex != verbRefIndex.end(); ++iIndex) {
189 cerr <<
" => index-maxp-entry: bin=" << iIndex->first <<
":\n";
190 for (
auto iVector = iIndex->second.begin(); iVector != iIndex->second.end(); ++iVector)
191 cerr <<
"\tstart=" << iVector->first.first
192 <<
" stop=" << iVector->first.second
193 <<
" TFile=" << iVector->second.fRootFilePtr->GetName() <<
'\n';
201 FdUpTimeFileManager::GetOkFlag(
int& returnData,
202 const std::string& componentProperty,
203 const std::string& componentName,
207 if (fAvailableComponents.find(componentProperty) == fAvailableComponents.end())
208 return VManager::eNotFound;
210 if (!fUpTimeVector.size())
211 return VManager::eNotFound;
214 return VManager::eNotFound;
216 if (componentProperty ==
"has_fduptime") {
218 return VManager::eFound;
221 if (componentProperty ==
"uptime_gpsStart") {
222 returnData = fDataValidity.GetStartTime().GetGPSSecond();
223 return VManager::eFound;
224 }
else if (componentProperty ==
"uptime_gpsStop") {
225 returnData = fDataValidity.GetStopTime().GetGPSSecond();
226 return VManager::eFound;
229 if (fVerbosity > 2) {
231 info <<
"called GetOkFlag for componentName=" << componentName
232 <<
" componentProperty=" << componentProperty <<
" and index map:\n";
233 for (
auto imap = componentIndex.begin(); imap != componentIndex.end(); ++imap)
234 info <<
" index=\"" << imap->first <<
"\" -> \"" << imap->second <<
"\"\n";
238 if (componentProperty ==
"status") {
240 if (componentName ==
"telescope") {
242 const int dataId = GetTelIndex(componentIndex);
243 const auto iData = fTelescopeStatus.find(dataId);
244 if (iData == fTelescopeStatus.end()) {
245 INFO_TERSE(
"Requested property of FdUpTimeFileManager not available!");
246 return VManager::eNotFound;
249 returnData = iData->second;
250 return VManager::eFound;
252 }
else if (componentName ==
"eye") {
254 const int eyeId = GetEyeIndex(componentIndex);
256 if (fVerbosity > 2) {
258 info <<
" eyeIndex=" << eyeId;
259 for (
auto iI = fEyeStatus.begin(); iI != fEyeStatus.end(); ++iI) {
260 info <<
"\n eyeStatus[" << iI->first <<
"] = "
266 const auto iData = fEyeStatus.find(eyeId);
267 if (iData == fEyeStatus.end()) {
268 INFO_TERSE(
"Requested property of FdUpTimeFileManager not available!");
269 return VManager::eNotFound;
272 returnData = iData->second;
273 return VManager::eFound;
275 }
else if (componentName ==
"CDAS") {
277 returnData = fCDASStatus;
278 return VManager::eFound;
283 return VManager::eNotFound;
288 FdUpTimeFileManager::GetData(
double& returnData,
289 const std::string& componentProperty,
290 const std::string& componentName,
295 if (fAvailableComponents.find(componentProperty) == fAvailableComponents.end())
296 return VManager::eNotFound;
299 if (!fUpTimeVector.size())
300 return VManager::eNotFound;
303 return VManager::eNotFound;
305 if (fVerbosity > 2) {
307 info <<
"called GetData(double) for componentName=" << componentName
308 <<
" componentProperty=" << componentProperty <<
" and index map:";
309 for (
auto imap = componentIndex.begin(); imap != componentIndex.end(); ++imap) {
310 info <<
"\n index=\"" << imap->first <<
"\" -> \"" << imap->second <<
'"';
315 if (componentProperty ==
"has_fduptime") {
317 return VManager::eFound;
319 if (componentName ==
"channel") {
321 return GetChannelData(returnData, componentProperty, componentName, componentIndex);
322 }
else if (componentProperty ==
"veto_fraction") {
323 if (componentName ==
"eye" || componentName ==
"CDAS")
324 return GetVetoFraction(returnData, componentProperty, componentName, componentIndex);
325 }
else if (componentName ==
"telescope" || componentName ==
"eye" || componentName ==
"CDAS") {
327 return GetUpTimeFraction(returnData, componentProperty, componentName, componentIndex);
330 return VManager::eNotFound;
335 FdUpTimeFileManager::GetUpTimeFraction(
double& returnData,
336 const std::string& componentProperty,
337 const std::string& componentName,
342 if (!fUpTimeVector.size())
343 return VManager::eNotFound;
346 return VManager::eNotFound;
348 if (fVerbosity > 2) {
350 info <<
"called GetUpTimeFraction for componentName=" << componentName
351 <<
" componentProperty=" << componentProperty <<
" and index map:";
352 for (
auto imap = componentIndex.begin(); imap != componentIndex.end(); ++imap) {
353 info <<
"\n index=\"" << imap->first <<
"\" -> \"" << imap->second <<
'"';
358 if (componentProperty ==
"uptime_fraction") {
360 if (componentName ==
"telescope") {
362 int dataId = GetTelIndex(componentIndex);
363 auto iData = fTelescopeUpTimeFraction.find(dataId);
364 if (iData == fTelescopeUpTimeFraction.end()) {
365 INFO_TERSE(
"Requested property of FdUpTimeFileManager not available!");
366 return VManager::eNotFound;
369 returnData = iData->second;
370 return VManager::eFound;
372 }
else if (componentName ==
"eye") {
374 int eyeId = GetEyeIndex(componentIndex);
375 auto iData = fEyeUpTimeFraction.find(eyeId);
376 if (iData == fEyeUpTimeFraction.end()) {
377 INFO_TERSE(
"Requested property of FdUpTimeFileManager not available!");
378 return VManager::eNotFound;
381 returnData = iData->second;
382 return VManager::eFound;
384 }
else if (componentName ==
"CDAS") {
386 returnData = fCDASUpTimeFraction;
387 return VManager::eFound;
393 return VManager::eNotFound;
398 FdUpTimeFileManager::GetVetoFraction(
double& returnData,
399 const std::string& componentProperty,
400 const std::string& componentName,
405 if (!fUpTimeVector.size())
406 return VManager::eNotFound;
409 return VManager::eNotFound;
411 if (fVerbosity > 2) {
413 info <<
"called GetVetoFraction for componentName=" << componentName
414 <<
" componentProperty=" << componentProperty <<
" and index map:";
415 for (
auto imap = componentIndex.begin(); imap != componentIndex.end(); ++imap) {
416 info <<
"\n index=\"" << imap->first <<
"\" -> \"" << imap->second <<
'"';
421 if (componentProperty ==
"veto_fraction") {
423 if (componentName ==
"eye") {
425 int eyeId = GetEyeIndex(componentIndex);
426 const auto iData = fFDASVetoFraction.find(eyeId);
427 if (iData == fFDASVetoFraction.end()) {
428 INFO_TERSE(
"Requested property of FdUpTimeFileManager not available!");
429 return VManager::eNotFound;
432 returnData = iData->second;
433 return VManager::eFound;
435 }
else if (componentName ==
"CDAS") {
437 int eyeId = GetEyeIndex(componentIndex);
438 const auto iDataSD = fCDASVetoFraction.find(eyeId);
439 if (iDataSD == fCDASVetoFraction.end()) {
440 INFO_TERSE(
"Requested property of FdUpTimeFileManager not available!");
441 return VManager::eNotFound;
444 returnData = iDataSD->second;
445 return VManager::eFound;
451 return VManager::eNotFound;
456 FdUpTimeFileManager::GetEyeIndex(
const IndexMap& componentIndex)
459 const auto iEyeId = componentIndex.find(
"eyeId");
460 if (iEyeId == componentIndex.end()) {
461 cerr <<
"WRONG iEyeId = " << iEyeId->second <<
'\n';
462 INFO_TERSE(
"Called FdUpTimeFileManager with wrong number of arguments!");
466 return boost::lexical_cast<
int>(iEyeId->second);
471 FdUpTimeFileManager::GetTelIndex(
const IndexMap& componentIndex)
474 const auto iEyeId = componentIndex.find(
"eyeId");
475 const auto iTelId = componentIndex.find(
"telescopeId");
476 if (iEyeId == componentIndex.end() || iTelId == componentIndex.end()) {
477 INFO_TERSE(
"Called FdUpTimeFileManager with wrong number of arguments!");
481 const auto eyeId = boost::lexical_cast<
int>(iEyeId->second);
482 const auto telId = boost::lexical_cast<
int>(iTelId->second);
483 const int dataId = 6 * (eyeId - 1) + telId;
489 FdUpTimeFileManager::GetChannelIndex(
const IndexMap& componentIndex)
492 const auto iChannelId = componentIndex.find(
"channelId");
493 if (iChannelId == componentIndex.end()) {
494 INFO_TERSE(
"Called FdUpTimeFileManager with wrong number of arguments!");
498 return boost::lexical_cast<
int>(iChannelId->second);
503 FdUpTimeFileManager::GetChannelData(
double& returnData,
504 const std::string& componentProperty,
512 if (fVerbosity > 2) {
514 info <<
"called GetChannelData with componentProperty=" << componentProperty <<
" and index map:";
515 for (
auto imap = componentIndex.begin(); imap != componentIndex.end(); ++imap) {
516 info <<
"\n index=\"" << imap->first <<
"\" -> \"" << imap->second <<
'"';
521 const int dataId = GetTelIndex(componentIndex);
522 int channelId = GetChannelIndex(componentIndex);
524 if (fVerbosity > 2) {
526 info <<
" (eye/tel) dataId=" << dataId <<
" channelId=" << channelId
527 <<
" fChannelInfo.size()=" << fChannelInfo.size() <<
" # tels";
531 const auto iData = fChannelInfo.find(dataId);
532 if (iData == fChannelInfo.end()) {
533 INFO_TERSE(
"Requested property of FdUpTimeFileManager not available!");
534 return VManager::eNotFound;
537 const auto& info = iData->second;
540 cerr <<
" channel vector size=" << info.size() <<
'\n';
544 if ((
unsigned int)(channelId) < info.size()) {
545 if (componentProperty ==
"ADCVariance")
546 returnData = info[channelId].fVariance;
547 else if (componentProperty ==
"Threshold")
548 returnData = info[channelId].fThreshold;
549 else if (componentProperty ==
"Baseline")
550 returnData = info[channelId].fBaseline;
552 return VManager::eNotFound;
556 cerr <<
" VAR=" << returnData <<
'\n';
558 return VManager::eFound;
563 FdUpTimeFileManager::ReadData()
567 INFO(
"enter ReadData");
569 if (fDataValidity.IsValid()) {
571 INFO(
"Data is still valid");
575 unsigned int now = det::Detector::GetInstance().GetTime().GetGPSSecond();
578 fEyeUpTimeFraction.clear();
579 fFDASVetoFraction.clear();
580 fTelescopeUpTimeFraction.clear();
582 fTelescopeStatus.clear();
584 fCDASUpTimeFraction = 0.;
585 fCDASVetoFraction.clear();
586 fChannelInfo.clear();
589 unsigned int iBin = (
unsigned int)(now / fgSearchMapBinning);
590 const ConstIndexIterator iIndex[2] = { fIndexHeadered.find(iBin), fIndexUnheadered.find(iBin) };
592 if (iIndex[0] == fIndexHeadered.end() && iIndex[1] == fIndexUnheadered.end())
595 unsigned int lower = 0;
596 unsigned int upper = 0;
598 bool foundLimits =
false;
599 TTree* actualDataTree =
nullptr;
600 double upTimeVer = 0;
602 for (
int j = 0; j < 2; ++j) {
604 if (iIndex[j] == (!j ? fIndexHeadered.end() : fIndexUnheadered.end()))
607 for (
auto iVector = iIndex[j]->
second.begin(); iVector != iIndex[j]->second.end(); ++iVector) {
608 if (iVector->second.fGpsStart <= now && now < iVector->
second.fGpsStop) {
609 lower = iVector->first.first;
610 upper = iVector->first.second;
611 fileName = iVector->second.fRootFilePtr->GetName();
612 actualDataTree = iVector->second.fDataTree;
613 upTimeVer = iVector->second.fFileVersion;
624 if (fVerbosity > 2) {
626 info <<
" index: now=" << now
628 <<
" lower=" << lower
629 <<
" upper=" << upper
630 <<
" TFile=" << fileName;
635 TBranch*
const startB = actualDataTree->GetBranch(
"uptime_gpsStart");
637 startB->SetAddress(&gpsStart);
638 TBranch*
const stopB = actualDataTree->GetBranch(
"uptime_gpsStop");
640 stopB->SetAddress(&gpsStop);
644 bool foundit =
false;
645 while (!foundit && upper >= lower) {
647 index = lower + (upper - lower)/2;
648 startB->GetEntry(index);
649 stopB->GetEntry(index);
651 if (gpsStart <= now && now < gpsStop) {
661 if (fVerbosity > 2) {
662 cerr <<
" search loop: nSearch=" << nSearch
663 <<
" gpsStart=" << gpsStart
664 <<
" gpsStop=" << gpsStop
665 <<
" foundit=" << foundit
666 <<
" index=" << index <<
'\n';
671 if (fVerbosity > 2) {
672 cerr <<
" END search loop: "
673 <<
" index=" << index <<
'\n';
677 Double_t tel_up_time_fraction[27] = { 0 };
678 UInt_t tel_status[27] = { 0 };
679 Double_t eye_up_time_fraction[5] = { 0 };
680 UInt_t eye_status[5] = { 0 };
681 Double_t cDAS_up_time_fraction = 0;
682 Double_t fDAS_veto_fraction[5] = { 0 };
683 Double_t cDAS_conn_fraction[5] = { 0 };
684 Double_t cDAS_veto_fraction[5] = { 0 };
685 UInt_t cDAS_status = 0;
686 UInt_t time_start = 0;
687 UInt_t time_stop = 0;
688 UShort_t channel_var[27][kNPixelPerTel] = { { 0 } };
689 UShort_t channel_threshold[27][kNPixelPerTel] = { { 0 } };
690 UShort_t channel_baseline[27][kNPixelPerTel] = { { 0 } };
692 actualDataTree->SetBranchAddress(
"uptime_gpsStart", &time_start);
693 actualDataTree->SetBranchAddress(
"uptime_gpsStop", &time_stop);
694 actualDataTree->SetBranchAddress(
"TelescopeUpFraction", &tel_up_time_fraction);
695 actualDataTree->SetBranchAddress(
"EyeUpFraction", &eye_up_time_fraction);
697 cerr <<
"UpTimeFile Version is " << upTimeVer <<
'\n';
699 actualDataTree->SetBranchAddress(
"TelescopeStatus", &tel_status);
700 actualDataTree->SetBranchAddress(
"EyeStatus",eye_status);
701 actualDataTree->SetBranchAddress(
"CDASUpFraction", &cDAS_up_time_fraction);
703 actualDataTree->SetBranchAddress(
"FDASVetoFraction", &fDAS_veto_fraction);
704 actualDataTree->SetBranchAddress(
"CDASConnFraction", &cDAS_conn_fraction);
705 actualDataTree->SetBranchAddress(
"CDASVetoFraction", &cDAS_veto_fraction);
707 actualDataTree->SetBranchAddress(
"CDASStatus", &cDAS_status);
708 actualDataTree->SetBranchAddress(
"ADCVariance", &channel_var);
709 actualDataTree->SetBranchAddress(
"ADCThreshold", &channel_threshold);
710 actualDataTree->SetBranchAddress(
"ADCBaseline", &channel_baseline);
712 actualDataTree->GetEntry(index);
714 for (
int iEye = 1; iEye <= 5; ++iEye) {
720 for (
int iTel = 1; iTel <= nTel; ++iTel) {
722 const int telIndex = 6*(iEye - 1) + iTel;
723 const int telIndex1 = telIndex - 1;
725 fTelescopeUpTimeFraction[telIndex] = tel_up_time_fraction[telIndex1];
727 fTelescopeStatus[telIndex] = tel_status[telIndex1];
728 else if (fTelescopeUpTimeFraction[telIndex] > 0)
729 fTelescopeStatus[telIndex] = 1;
731 fTelescopeStatus[telIndex] = 0;
732 fChannelInfo[telIndex].clear();
733 for (
unsigned int channel = 0; channel < kNPixelPerTel; ++channel) {
734 fChannelInfo[telIndex].push_back(
736 channel_var[telIndex1][channel],
737 channel_threshold[telIndex1][channel],
738 channel_baseline[telIndex1][channel]
744 const int iEye1 = iEye - 1;
746 fEyeStatus[iEye] = eye_status[iEye1];
747 fEyeUpTimeFraction[iEye] = eye_up_time_fraction[iEye1];
748 fFDASVetoFraction[iEye] = 1;
749 fCDASVetoFraction[iEye] = 1;
751 fFDASVetoFraction[iEye] = fDAS_veto_fraction[iEye1];
752 fEyeUpTimeFraction[iEye] = eye_up_time_fraction[iEye1];
753 if (fEyeUpTimeFraction[iEye] > 0)
754 fEyeStatus[iEye] = 1;
755 fCDASVetoFraction[iEye] = cDAS_veto_fraction[iEye1];
760 fCDASStatus = cDAS_status;
763 fCDASUpTimeFraction = cDAS_up_time_fraction;
765 fCDASUpTimeFraction = 1;
771 if (fVerbosity > 2) {
772 cerr <<
" ReadData result is valid from start: "
773 << fDataValidity.GetStartTime().GetGPSSecond()
774 <<
" end: " << fDataValidity.GetStopTime().GetGPSSecond() <<
'\n';
#define INFO_TERSE(message)
Macro for logging informational messages.
bool UpTimePeriodSortFunction(const UpTimePeriod &first, const UpTimePeriod &second)
#define INFO(message)
Macro for logging informational messages.
void Init()
Initialise the registry.
std::map< unsigned int, IndexVector > Index
A TimeStamp holds GPS second and nanosecond for some event.
Base class to report exceptions in IO.
#define WARNING(message)
Macro for logging warning messages.
Class to manage Fd UpTime ROOT files.
Index::const_iterator ConstIndexIterator
#define REGISTER_F_MANAGER(_name_, _Type_)
std::map< std::string, std::string > IndexMap
#define ERROR(message)
Macro for logging error messages.
Status
Specifies success or (eventually) various possible failure modes.