Belle II Software  release-08-00-10
DQMHistAnalysisInput.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 //+
9 // File : DQMHistAnalysisInput.cc
10 // Description :
11 //-
12 
13 
14 #include <dqm/analysis/shminput/modules/DQMHistAnalysisInput.h>
15 
16 #include <TROOT.h>
17 #include <TKey.h>
18 
19 #include <ctime>
20 
21 using namespace Belle2;
22 
23 //-----------------------------------------------------------------
24 // Register the Module
25 //-----------------------------------------------------------------
26 REG_MODULE(DQMHistAnalysisInput);
27 
28 //-----------------------------------------------------------------
29 // Implementation
30 //-----------------------------------------------------------------
31 
34 {
35  //Parameter definition
36  addParam("HistMemoryPath", m_mempath, "Path to Input Hist memory", std::string(""));
37 // addParam("HistMemorySize", m_memsize, "Size of Input Hist memory", 10000000);
38  addParam("HistMemoryName", m_memname, "Name of Input Hist memory", std::string(""));
39  addParam("HistMemoryUser", m_username, "Name of Input Hist memory owner", std::string("dqmdaq"));
40  addParam("ShmId", m_shm_id, "ID of shared memory", -1);
41  addParam("SemId", m_sem_id, "ID of semaphore", -1);
42  addParam("RefreshInterval", m_interval, "Refresh interval of histograms", 10);
43  addParam("RemoveEmpty", m_remove_empty, "Remove empty histograms", false);
44  addParam("EnableRunInfo", m_enable_run_info, "Enable Run Info", false);
45  B2DEBUG(1, "DQMHistAnalysisInput: Constructor done.");
46 }
47 
48 
50 
52 {
53  if (m_memory != nullptr) delete m_memory;
54  if (m_mempath != "") { // TODO: I am not sure that this is working and what the difference to m_memname is
55  B2INFO("Open HistMemoryPath " << m_mempath);
56  m_memory = new DqmMemFile(m_mempath.c_str());
57  } else {
58  if (m_memname != "") {
59  m_shm_id = -1;
60  m_sem_id = -1;
61  auto pathname = DqmSharedMem::getTmpFileName(m_username, m_memname);
62  B2INFO("Open HistMemoryName for user " << m_username << " Name " << m_memname << " Path " << pathname);
63  if (!DqmSharedMem::getIdFromTmpFileName(pathname, m_shm_id, m_sem_id)) {
64  B2FATAL("Could not open shared memory user " << m_username << " Name " << m_memname);
65  }
66  }
67  if (m_shm_id >= 0 && m_sem_id >= 0) {
68  B2INFO("Open Shared Memory ID " << m_shm_id << " Semaphore ID " << m_sem_id);
70  } else {
71  B2FATAL("Information for shared memory is missing, either name or ID is needed!");
72  }
73  }
74  if (m_enable_run_info) {
75  m_c_info = new TCanvas("DQMInfo/c_info", "");
76  m_c_info->SetTitle("");
77  } else {
78  m_c_info = NULL;
79  }
80  m_eventMetaDataPtr.registerInDataStore();
81  B2INFO("DQMHistAnalysisInput: initialized.");
82 }
83 
84 
86 {
87  B2INFO("DQMHistAnalysisInput: beginRun called.");
88  clearHistList();
89 }
90 
92 {
94 
95  sleep(m_interval);
96  std::vector<TH1*> hs;
97  char mbstr[100];
98 
99  time_t now = time(0);
100  strftime(mbstr, sizeof(mbstr), "%c", localtime(&now));
101  B2INFO("[" << mbstr << "] before LoadMemFile");
102 
103  TMemFile* file = m_memory->LoadMemFile();
104 
105  now = time(0);
106  strftime(mbstr, sizeof(mbstr), "%c", localtime(&now));
107  B2INFO("[" << mbstr << "] after LoadMemFile");
108 
109  const TDatime& mt = file->GetModificationDate();
110  TDatime mmt(mt.Convert());
111  std::string expno("UNKNOWN"), runno("UNKNOWN"), rtype("UNKNOWN");
112 
113  file->cd();
114  TIter next(file->GetListOfKeys());
115  TKey* key = nullptr;
116 
117  now = time(0);
118  strftime(mbstr, sizeof(mbstr), "%c", localtime(&now));
119  B2INFO("[" << mbstr << "] before input loop");
120 
121  while ((key = (TKey*)next())) {
122  auto obj = key->ReadObj();
123  if (obj == nullptr) continue; // would be strange, but better check
124  if (!obj->IsA()->InheritsFrom("TH1")) continue; // other non supported (yet?)
125  TH1* h = (TH1*)obj; // we are sure its a TH1
126 
127  if (m_remove_empty && h->GetEntries() == 0) continue;
128  // Remove ":" from folder name, workaround!
129  TString a = h->GetName();
130  a.ReplaceAll(":", "");
131  h->SetName(a);
132  B2DEBUG(1, "DQMHistAnalysisInput: get histo " << a.Data());
133 
134  // the following line prevent any histogram outside a directory to be processed
135  if (StringSplit(a.Data(), '/').size() <= 1) continue;
136 
137  hs.push_back(h);
138 
139  // the following workaround need to be improved
140  if (std::string(h->GetName()) == std::string("DQMInfo/expno")) expno = h->GetTitle();
141  if (std::string(h->GetName()) == std::string("DQMInfo/runno")) runno = h->GetTitle();
142  if (std::string(h->GetName()) == std::string("DQMInfo/rtype")) rtype = h->GetTitle();
143  }
144 
145  now = time(0);
146  strftime(mbstr, sizeof(mbstr), "%c", localtime(&now));
147  B2INFO("[" << mbstr << "] after input loop");
148 
149  if (expno == std::string("UNKNOWN") || runno == std::string("UNKNOWN")) {
150  m_expno = 22;
151  m_runno = 1;
152  if (m_c_info != NULL) m_c_info->SetTitle((m_memname + ": Last Updated " + mmt.AsString()).c_str());
153  } else {
154  if (m_c_info != NULL) m_c_info->SetTitle((m_memname + ": Exp " + expno + ", Run " + runno + ", RunType " + rtype + ", Last Updated "
155  + mmt.AsString()).c_str());
156  m_expno = std::stoi(expno);
157  m_runno = std::stoi(runno);
158  }
159  B2INFO("DQMHistAnalysisInput: " << m_memname + ": Exp " + expno + ", Run " + runno + ", RunType " + rtype + ", Last Updated " +
160  mmt.AsString());
161 
162 
163  m_count++;
164  m_eventMetaDataPtr.create();
165  m_eventMetaDataPtr->setExperiment(m_expno);
166  m_eventMetaDataPtr->setRun(m_runno);
167  m_eventMetaDataPtr->setEvent(m_count);
168  m_eventMetaDataPtr->setTime(mt.Convert());
169 
170  //setExpNr(m_expno); // redundant access from MetaData
171  //setRunNr(m_runno); // redundant access from MetaData
172  // ExtractRunType();// Run Type is processed above already, just take it
173  setRunType(rtype);
174  ExtractEvent(hs);
175 
176  // this code must be run after "event processed" has been extracted
177  bool anyupdate = false; // flag if any histogram updated at all
178  for (auto& h : hs) {
179  anyupdate |= addHist("", h->GetName(), h);
180  B2DEBUG(1, "Found : " << h->GetName() << " : " << h->GetEntries());
181  }
182 
183  // if no histogram was updated, we could stop processing
184  setReturnValue(anyupdate);
185 }
186 
188 {
189  B2INFO("DQMHistAnalysisInput : endRun called");
190 
191  TIter nextckey(gROOT->GetListOfCanvases());
192  TObject* cobj = NULL;
193 
194  while ((cobj = dynamic_cast<TObject*>(nextckey()))) {
195  if (cobj->IsA()->InheritsFrom("TCanvas")) {
196  (dynamic_cast<TCanvas*>(cobj))->Clear();
197  }
198  }
199 }
200 
202 {
203  B2INFO("terminate called");
204 }
205 
std::string m_memname
The name of the memory file (HLT or ExpressReco).
bool m_remove_empty
Whether to remove empty histograms.
virtual void initialize() override
Initializer.
virtual void event() override
This method is called for each event.
virtual ~DQMHistAnalysisInputModule()
Destructor.
bool m_enable_run_info
Whether to enable the run info to be displayed.
int m_shm_id
The shmid for the shared memory.
virtual void endRun() override
This method is called if the current run ends.
virtual void terminate() override
This method is called at the end of the event processing.
std::string m_mempath
The name of the shared memory for the histograms.
virtual void beginRun() override
Called when entering a new run.
int m_interval
The size of the shared memory.
std::string m_username
The owner of the memory file (HLT or ExpressReco).
DqmMemFile * m_memory
Memory file to hold histograms.
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
The metadata for each event.
TCanvas * m_c_info
The canvas hold the basic DQM info.
int m_sem_id
The semid for the shared memory.
The base class for the histogram analysis module.
static void clearHistList(void)
Clears the list of histograms.
std::vector< std::string > StringSplit(const std::string &s, const char delim)
Helper function for string token split.
void setRunType(std::string &t)
Set the Run Type.
static bool addHist(const std::string &dirname, const std::string &histname, TH1 *h)
Add histogram.
static void initHistListBeforeEvent(void)
Reset the list of histograms.
void ExtractEvent(std::vector< TH1 * > &hs)
Extract event processed from daq histogram, called from input module.
void setReturnValue(int value)
Sets the return value for this module as integer.
Definition: Module.cc:220
REG_MODULE(arichBtest)
Register the Module.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
Abstract base class for different kinds of events.