Belle II Software  release-08-01-10
DQMHistAnalysisOutputImages.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 : DQMHistAnalysisOutputImages.cc
10 // Description : DQM Output, send Canvases to jsroot server.
11 //-
12 
13 
14 #include <dqm/analysis/modules/DQMHistAnalysisOutputImages.h>
15 #include <TROOT.h>
16 #include <TClass.h>
17 #include <TObject.h>
18 #include <TCanvas.h>
19 #include <ctime>
20 #include <filesystem>
21 
22 using namespace std;
23 using namespace Belle2;
24 
25 //-----------------------------------------------------------------
26 // Register the Module
27 //-----------------------------------------------------------------
28 REG_MODULE(DQMHistAnalysisOutputImages);
29 
30 //-----------------------------------------------------------------
31 // Implementation
32 //-----------------------------------------------------------------
33 
34 DQMHistAnalysisOutputImagesModule::DQMHistAnalysisOutputImagesModule()
36 {
37  setDescription("Module to produce output images during 'events' for dqm steering scripts.");
38  //Parameter definition
39  addParam("CanvasSaveDefault", m_canvasSaveDefault, "Save untagged canvases", false);
40  addParam("OutputPath", m_outputPath, "Set output path", std::string(""));
41  addParam("asPNG", m_asPNG, "Save as PNG files", true);
42  addParam("asJPEG", m_asJPEG, "Save as JPEG files", false);
43  addParam("asPDF", m_asPDF, "Save as PDF files", false);
44  addParam("asJSON", m_asJSON, "Save as JSON files", false);
45  addParam("asROOT", m_asROOT, "Save as ROOT files", false);
46  addParam("useExpRun", m_useExpRun, "crate exp/run subdirs", false);
47  B2DEBUG(20, "DQMHistAnalysisOutputImages: Constructor done.");
48 }
49 
51 {
52  m_evtMetaDataPtr.isRequired();
53 }
54 
56 {
57  B2DEBUG(20, "DQMHistAnalysisOutputImages: event called.");
58  if (!m_evtMetaDataPtr.isValid()) {
59  B2ERROR("No valid EventMetaData.");
60  return;
61  }
62 
63  TSeqCollection* seq = gROOT->GetListOfCanvases();
64  TIter nextkey(seq);
65  TObject* obj = 0;
66 
67  std::filesystem::path outpath = m_outputPath;
68  if (m_useExpRun) outpath = outpath / std::to_string(m_evtMetaDataPtr->getExperiment()) / std::to_string(
69  m_evtMetaDataPtr->getRun()); // add exp/run
70 
71  auto& clist = getCanvasUpdatedList();
72  int saved_canvases = 0;
73 
74  while ((obj = (TObject*)nextkey())) {
75  if (obj->IsA()->InheritsFrom("TCanvas")) {
76  TCanvas* c = (TCanvas*) obj;
77  auto process_canvas = m_canvasSaveDefault;
78 
79  auto it = clist.find(c->GetName());
80  if (it != clist.end()) {
81  process_canvas = it->second;
82  }
83  if (!process_canvas) continue;
84  saved_canvases++;
85 
86  std::string cname = c->GetName();
87  create_directories((outpath / cname).parent_path()); // create recursive path, even if it exist
88  if (m_asPNG) c->Print((outpath / (cname + ".png")).c_str());
89  if (m_asJPEG) c->Print((outpath / (cname + ".jpg")).c_str());
90  if (m_asPDF) c->Print((outpath / (cname + ".pdf")).c_str());
91  if (m_asROOT) c->Print((outpath / (cname + ".root")).c_str());
92  if (m_asJSON) c->Print((outpath / (cname + ".json")).c_str());
93  }
94  }
95  B2INFO("Saved " << saved_canvases << " of " << seq->GetEntries() << " objects.");
96 }
97 
The base class for the histogram analysis module.
static const CanvasUpdatedList & getCanvasUpdatedList()
Get the list of the canvas update status.
void initialize(void) override final
Initializer.
StoreObjPtr< EventMetaData > m_evtMetaDataPtr
The metadata for each event.
bool m_canvasSaveDefault
Save untagged canvase by default.
void event() override final
This method is called for each event.
std::string m_outputPath
Output path for saving images in sub-folders.
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
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
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.