tdaq-develop-2025-02-12
DQMHistosBase.cc
1 #include "otsdaq/RootUtilities/DQMHistosBase.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/RootUtilities/VisualDataManager.h"
4 
5 #include <TDirectory.h>
6 #include <TFile.h>
7 #include <TStyle.h>
8 
9 #include <ctime>
10 #include <iostream>
11 
12 using namespace ots;
13 
14 #undef __MF_SUBJECT__
15 #define __MF_SUBJECT__ "DQMHistos"
16 #define mfSubject_ (std::string("DQMHistos"))
17 
18 //==============================================================================
19 DQMHistosBase::DQMHistosBase(void) { gStyle->SetPalette(1); }
20 
21 //==============================================================================
22 DQMHistosBase::~DQMHistosBase(void) { closeFile(); }
23 
24 //==============================================================================
25 bool DQMHistosBase::isFileOpen(void)
26 {
27  if(theFile_ == nullptr || !theFile_->IsOpen())
28  return false;
29  return true;
30 }
31 
32 //==============================================================================
33 void DQMHistosBase::openFile(std::string fileName)
34 {
35  closeFile();
36  myDirectory_ = nullptr;
37  theFile_ = theDataManager_->openFile(fileName);
38 
39  theFile_->cd();
40  myDirectory_ = theFile_;
41 }
42 
43 //==============================================================================
44 void DQMHistosBase::save(void)
45 {
46  if(theFile_ != nullptr)
47  {
48  if(autoSave_)
49  theFile_->Write(
50  "",
51  TObject::
52  kOverwrite); // write the histogram to the file with kOverwrite update option
53  else
54  theFile_->Write(); // Lorenzo changed 2023-04-07 to kOverwrite
55  }
56 }
57 
58 //==============================================================================
60  bool
61  force) // The file will be saved if currentTime - beginTimeTime_ is >= autoSaveInterval_
62 {
63  if(!autoSave_)
64  return;
65 
66  time_t currentTime;
67  time(&currentTime);
68  if(beginTime_ == 0)
69  {
70  theFile_->Write(
71  "",
72  TObject::
73  kOverwrite); // write the histogram to the file with kOverwrite update option
74  beginTime_ = currentTime;
75  return;
76  }
77 
78  if(force || currentTime - beginTime_ >= autoSaveInterval_)
79  {
80  theFile_->Write(
81  "",
82  TObject::
83  kOverwrite); // write the histogram to the file with kOverwrite update option
84  beginTime_ = currentTime;
85  }
86 }
87 
88 //==============================================================================
89 void DQMHistosBase::closeFile(void)
90 {
91  if(theFile_ != nullptr)
92  {
93  if(theFile_->IsOpen())
94  theFile_->Close();
95  theFile_ = nullptr;
96  }
97 }
98 
99 //==============================================================================
100 TObject* DQMHistosBase::get(std::string name)
101 {
102  if(theFile_ != nullptr)
103  return theFile_->Get(name.c_str());
104  return 0;
105 }
virtual void autoSave(bool force=false)
The file will be saved if force == true or currentTime - beginTimeTime_ is >= autoSaveInterval_.