Belle II Software  release-08-01-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  clearCanvases();
90 }
91 
93 {
95 
96  sleep(m_interval);
97  std::vector<TH1*> hs;
98  char mbstr[100];
99 
100  time_t now = time(0);
101  strftime(mbstr, sizeof(mbstr), "%c", localtime(&now));
102  B2INFO("[" << mbstr << "] before LoadMemFile");
103 
104  TMemFile* file = m_memory->LoadMemFile();
105 
106  now = time(0);
107  strftime(mbstr, sizeof(mbstr), "%c", localtime(&now));
108  B2INFO("[" << mbstr << "] after LoadMemFile");
109 
110  const TDatime& mt = file->GetModificationDate();
111  TDatime mmt(mt.Convert());
112  std::string expno("UNKNOWN"), runno("UNKNOWN"), rtype("UNKNOWN");
113 
114  file->cd();
115  TIter next(file->GetListOfKeys());
116  TKey* key = nullptr;
117 
118  now = time(0);
119  strftime(mbstr, sizeof(mbstr), "%c", localtime(&now));
120  B2INFO("[" << mbstr << "] before input loop");
121 
122  while ((key = (TKey*)next())) {
123  auto obj = key->ReadObj();
124  if (obj == nullptr) continue; // would be strange, but better check
125  if (!obj->IsA()->InheritsFrom("TH1")) continue; // other non supported (yet?)
126  TH1* h = (TH1*)obj; // we are sure its a TH1
127 
128  if (m_remove_empty && h->GetEntries() == 0) continue;
129  // Remove ":" from folder name, workaround!
130  TString a = h->GetName();
131  a.ReplaceAll(":", "");
132  h->SetName(a);
133  B2DEBUG(1, "DQMHistAnalysisInput: get histo " << a.Data());
134 
135  // the following line prevent any histogram outside a directory to be processed
136  if (StringSplit(a.Data(), '/').size() <= 1) continue;
137 
138  hs.push_back(h);
139 
140  // the following workaround need to be improved
141  if (std::string(h->GetName()) == std::string("DQMInfo/expno")) expno = h->GetTitle();
142  if (std::string(h->GetName()) == std::string("DQMInfo/runno")) runno = h->GetTitle();
143  if (std::string(h->GetName()) == std::string("DQMInfo/rtype")) rtype = h->GetTitle();
144  }
145 
146  now = time(0);
147  strftime(mbstr, sizeof(mbstr), "%c", localtime(&now));
148  B2INFO("[" << mbstr << "] after input loop");
149 
150  if (expno == std::string("UNKNOWN") || runno == std::string("UNKNOWN")) {
151  m_expno = 22;
152  m_runno = 1;
153  if (m_c_info != NULL) m_c_info->SetTitle((m_memname + ": Last Updated " + mmt.AsString()).c_str());
154  } else {
155  if (m_c_info != NULL) m_c_info->SetTitle((m_memname + ": Exp " + expno + ", Run " + runno + ", RunType " + rtype + ", Last Updated "
156  + mmt.AsString()).c_str());
157  m_expno = std::stoi(expno);
158  m_runno = std::stoi(runno);
159  }
160  B2INFO("DQMHistAnalysisInput: " << m_memname + ": Exp " + expno + ", Run " + runno + ", RunType " + rtype + ", Last Updated " +
161  mmt.AsString());
162 
163 
164  m_count++;
165  m_eventMetaDataPtr.create();
166  m_eventMetaDataPtr->setExperiment(m_expno);
167  m_eventMetaDataPtr->setRun(m_runno);
168  m_eventMetaDataPtr->setEvent(m_count);
169  m_eventMetaDataPtr->setTime(mt.Convert());
170 
171  //setExpNr(m_expno); // redundant access from MetaData
172  //setRunNr(m_runno); // redundant access from MetaData
173  // ExtractRunType();// Run Type is processed above already, just take it
174  setRunType(rtype);
175  ExtractEvent(hs);
176 
177  // this code must be run after "event processed" has been extracted
178  bool anyupdate = false; // flag if any histogram updated at all
179  for (auto& h : hs) {
180  anyupdate |= addHist("", h->GetName(), h);
181  B2DEBUG(1, "Found : " << h->GetName() << " : " << h->GetEntries());
182  }
183 
184  // if no histogram was updated, we could stop processing
185  setReturnValue(anyupdate);
186 }
187 
189 {
190  B2INFO("DQMHistAnalysisInput : endRun called");
191 }
192 
194 {
195  B2INFO("terminate called");
196 }
197 
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.
void clearCanvases(void)
Clear content of all Canvases.
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.