ATLAS Offline Software
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
MuonValR4::RootVisualizationService Class Reference

Implementation of the IRootVisualization service. More...

#include <RootVisualizationService.h>

Inheritance diagram for MuonValR4::RootVisualizationService:
Collaboration diagram for MuonValR4::RootVisualizationService:

Classes

struct  PlotsPerClient
 Helper struct to group all plots that are belonging to a client of the service. More...
 

Public Types

using PlotPtr_t = std::shared_ptr< detail::DrawCanvasObject >
 
using PlotVec_t = std::vector< PlotPtr_t >
 

Public Member Functions

virtual StatusCode registerClient (const ClientToken &token) override final
 
virtual std::shared_ptr< ICanvasObjectprepareCanvas (const EventContext &ctx, const ClientToken &token, const std::string &canvasName) override final
 
virtual StatusCode finalize () override final
 
virtual StatusCode initialize () override final
 

Private Types

using StorageMap_t = std::map< ClientToken, PlotsPerClient >
 

Private Member Functions

void paintObjects (const ClientToken &token, PlotVec_t &&toDraw)
 

Private Attributes

Gaudi::Property< double > m_canvasExtraScale {this, "CanvasExtraScale" , 1.5}
 Extra safety margin to zoom out from the Canvas. More...
 
Gaudi::Property< bool > m_quadCanvas {this, "QuadraticCanas", true}
 Ensure that the canvas has the same interval sizes in x & y. More...
 
Gaudi::Property< std::string > m_AtlasLabel {this, "AtlasLabel", "Internal"}
 ATLAS label (Internal / Prelimnary / Simulation) More...
 
Gaudi::Property< std::string > m_sqrtSLabel {this, "SqrtSLabel", "14"}
 Centre of mass energy label. More...
 
Gaudi::Property< std::string > m_lumiLabel {this, "LumiLabel", ""}
 Luminosity label. More...
 
Gaudi::Property< unsigned > m_canvasWidth {this, "CanvasWidth", 800}
 Width of all drawn Canvases. More...
 
Gaudi::Property< unsigned > m_canvasHeight {this, "CanvasHeight", 600}
 Height of all drawn Canvases. More...
 
Gaudi::Property< std::string > m_outDir {this, "outputDir" , "./Displays/"}
 Directory into which all plots are written to
More...
 
Gaudi::Property< std::string > m_outRootFileName {this, "outputROOTFile", "AllDisplays.root"}
 Name of the ROOT file into which the output Canvases are written (needs root to be in the list of outputFormats of the client tokens) More...
 
StorageMap_t m_storage {}
 
std::unique_ptr< TFile > m_outFile {}
 File into which all Canvases are saved if root is defined as extension. More...
 
std::mutex m_storageMutex {}
 
std::mutex m_canvasMutex {}
 

Detailed Description

Implementation of the IRootVisualization service.


Definition at line 18 of file RootVisualizationService.h.

Member Typedef Documentation

◆ PlotPtr_t

Definition at line 31 of file RootVisualizationService.h.

◆ PlotVec_t

Definition at line 32 of file RootVisualizationService.h.

◆ StorageMap_t

using MuonValR4::RootVisualizationService::StorageMap_t = std::map<ClientToken, PlotsPerClient>
private

Definition at line 69 of file RootVisualizationService.h.

Member Function Documentation

◆ finalize()

StatusCode MuonValR4::RootVisualizationService::finalize ( )
finaloverridevirtual

Definition at line 184 of file RootVisualizationService.cxx.

184  {
185  ATH_MSG_DEBUG("Finalize the visualization service. Dump all canvases that not have yet been drawn");
186  for (auto& [token, dataHolder] : m_storage) {
187  if (!dataHolder.elementsDrawn) {
188  paintObjects(token, std::move(dataHolder.toDraw));
189  }
190  dataHolder.toDraw.clear();
191  }
192  m_outFile.reset();
193  return StatusCode::SUCCESS;
194  }

◆ initialize()

StatusCode MuonValR4::RootVisualizationService::initialize ( )
finaloverridevirtual

Definition at line 34 of file RootVisualizationService.cxx.

34  {
35  gROOT->SetStyle("ATLAS");
36  TStyle* plotStyle = gROOT->GetStyle("ATLAS");
37  plotStyle->SetOptTitle(0);
38  plotStyle->SetHistLineWidth(1.);
39  plotStyle->SetPalette(kViridis);
40 
41  return StatusCode::SUCCESS;
42  }

◆ paintObjects()

void MuonValR4::RootVisualizationService::paintObjects ( const ClientToken &  token,
PlotVec_t &&  toDraw 
)
private

First sort the plots by the event number

The client still holds the pointer to the plot

Number the plots per event

Setup the frame

Draw the primitives

Save the single plots

Definition at line 90 of file RootVisualizationService.cxx.

91  {
92  if (toDraw.empty()){
93  return;
94  }
96  std::ranges::stable_sort(toDraw, [](const PlotPtr_t& a, const PlotPtr_t& b){
97  return a->event() < b->event();
98  });
99  std::unique_lock guard{m_canvasMutex};
100  std::unique_ptr<TCanvas> summaryCan{};
101  const std::string summaryPdfName = std::format("{:}/All{}.pdf",
102  m_outDir.value(), token.preFixName);
103  if (token.saveSummaryPlot) {
104  ATH_MSG_DEBUG("Open "<<summaryPdfName<<" to dump all canvases in a common file");
105  ensureDirectory(summaryPdfName);
106  summaryCan = std::make_unique<TCanvas>("allCan","allCan", m_canvasWidth, m_canvasHeight);
107  summaryCan->SaveAs(std::format("{:}[", summaryPdfName).c_str());
108  }
109  if (token.fileFormats.count("root") && !m_outFile) {
110  std::string outFile = std::format("{:}/{:}", m_outDir.value(),
111  m_outRootFileName.value());
113  m_outFile.reset(TFile::Open(outFile.c_str(), "RECREATE"));
114  if (!m_outFile || m_outFile->IsZombie()) {
115  THROW_EXCEPTION("Failed to create "<<outFile<<".");
116  }
117  ATH_MSG_DEBUG("Open "<<outFile<<" to save the plots in root format");
118  }
119  std::size_t currEvt{toDraw.back()->event()}, plotCount{0};
120  for (const PlotPtr_t& drawMe : toDraw) {
122  while (!drawMe.unique()) {
123  using namespace std::chrono_literals;
124  ATH_MSG_DEBUG("Wait until "<<drawMe->name()<<" is finished.");
125  std::this_thread::sleep_for(10ms);
126  }
128  if (currEvt != drawMe->event()) {
129  currEvt = drawMe->event();
130  plotCount = 0;
131  }
132  std::string plotName = std::format("{:}/{:}/{:}_{:}_{:}_{:}", m_outDir.value(),
133  token.subDirectory, token.preFixName,
134  drawMe->event(), ++plotCount,
135  removeNonAlphaNum(drawMe->name()));
136 
137  auto singleCan = std::make_unique<TCanvas>("can", "can" , m_canvasWidth, m_canvasHeight);
138  singleCan->cd();
140  using enum ICanvasObject::AxisRanges;
141  ATH_MSG_VERBOSE("Crate new canvas: "<<plotName<<" ["<<drawMe->corner(xLow)<<";"<<drawMe->corner(xHigh)
142  <<"], ["<<drawMe->corner(yLow)<<";"<<drawMe->corner(yHigh)<<"].");
143  auto frameH = std::make_unique<TH2F>("frameH",
144  std::format("frame;{:};{:};{:}", drawMe->xTitle(), drawMe->yTitle(), drawMe->zTitle()).c_str(),
145  1, drawMe->corner(xLow), drawMe->corner(xHigh),
146  1, drawMe->corner(yLow), drawMe->corner(yHigh));
147  frameH->Draw("AXIS");
148  drawMe->add(drawAtlasLabel(0.65, 0.26, m_AtlasLabel));
149  drawMe->add(drawLumiSqrtS(0.65,0.21, m_sqrtSLabel, m_lumiLabel));
151  for (auto& [primitive, opt] : drawMe->primitives()) {
152  primitive->Draw(opt.c_str());
153  }
155  if (token.saveSinglePlots){
156  for (const std::string& fileExt : token.fileFormats) {
157  if (fileExt != "root") {
158  ensureDirectory(plotName);
159  singleCan->SaveAs(std::format("{:}.{:}", plotName, fileExt).c_str());
160  }
161  }
162  }
163  if (token.fileFormats.count("root")) {
164  TDirectory* writeTo = m_outFile.get();
165  if (!token.subDirectory.empty()) {
166  writeTo = m_outFile->GetDirectory(token.subDirectory.c_str());
167  if (!writeTo) {
168  writeTo = m_outFile->mkdir(token.subDirectory.c_str());
169  }
170  }
171  writeTo->WriteObject(singleCan.get(),
172  std::format("{:}_{:}_{:}_{:}",token.preFixName,
173  drawMe->event(), plotCount,
174  removeNonAlphaNum(drawMe->name())).c_str());
175  }
176  if (summaryCan) {
177  singleCan->SaveAs(summaryPdfName.c_str());
178  }
179  }
180  if (summaryCan) {
181  summaryCan->SaveAs(std::format("{:}]", summaryPdfName).c_str());
182  }
183  }

◆ prepareCanvas()

std::shared_ptr< ICanvasObject > MuonValR4::RootVisualizationService::prepareCanvas ( const EventContext &  ctx,
const ClientToken &  token,
const std::string &  canvasName 
)
finaloverridevirtual

Definition at line 63 of file RootVisualizationService.cxx.

64  {
65  if (canvasName.empty()) {
66  THROW_EXCEPTION("The canvas name must not be empty");
67  }
68  std::unique_lock lock_guard{m_storageMutex};
69  StorageMap_t::iterator store_itr = m_storage.find(token);
70  if (store_itr == m_storage.end()) {
71  THROW_EXCEPTION("The token "<<token.preFixName<<" is unknown.");
72  }
73  PlotsPerClient& dataHolder = store_itr->second;
74  if (!dataHolder.elementsDrawn &&
75  cleanTrashed(dataHolder.toDraw) < store_itr->first.canvasLimit){
76  const std::size_t evt = ctx.eventID().event_number();
77  ATH_MSG_VERBOSE("Provide new canvas "<<canvasName<<" for stream "<<token.preFixName
78  <<" in "<<ctx.eventID());
79  auto newCanvas = dataHolder.toDraw.emplace_back(std::make_shared<detail::DrawCanvasObject>(canvasName, evt));
80  newCanvas->setRangeScale(m_canvasExtraScale, m_quadCanvas);
81  return newCanvas;
82  } else if (!dataHolder.elementsDrawn) {
83  paintObjects(store_itr->first, std::move(dataHolder.toDraw));
84  dataHolder.elementsDrawn = true;
85  }
86  ATH_MSG_VERBOSE("Maximum elements for "<<token.preFixName
87  <<" reached. Don't provide any new canvas");
88  return nullptr;
89  }

◆ registerClient()

StatusCode MuonValR4::RootVisualizationService::registerClient ( const ClientToken &  token)
finaloverridevirtual

Definition at line 43 of file RootVisualizationService.cxx.

43  {
44  if (token.preFixName.empty()){
45  ATH_MSG_FATAL("Prefix name must not be empty");
46  return StatusCode::FAILURE;
47  }
48  std::unique_lock guard{m_storageMutex};
49  auto insert_itr = m_storage.insert(std::make_pair(token, PlotsPerClient{}));
50  if (!insert_itr.second) {
51  ATH_MSG_FATAL("The token "<<token.preFixName<<" is already registered");
52  return StatusCode::FAILURE;
53  }
54  ATH_MSG_INFO("Registered new client "<<token.preFixName
55  <<", maximum number of plots "<<token.canvasLimit
56  <<", formats: "<<token.fileFormats<<", save single: "
57  <<(token.saveSinglePlots ? "yay" : "nay")
58  <<", save summary: "<<(token.saveSummaryPlot ? "yay" : "nay"));
59  return StatusCode::SUCCESS;
60  }

Member Data Documentation

◆ m_AtlasLabel

Gaudi::Property<std::string> MuonValR4::RootVisualizationService::m_AtlasLabel {this, "AtlasLabel", "Internal"}
private

ATLAS label (Internal / Prelimnary / Simulation)

Definition at line 44 of file RootVisualizationService.h.

◆ m_canvasExtraScale

Gaudi::Property<double> MuonValR4::RootVisualizationService::m_canvasExtraScale {this, "CanvasExtraScale" , 1.5}
private

Extra safety margin to zoom out from the Canvas.

Definition at line 40 of file RootVisualizationService.h.

◆ m_canvasHeight

Gaudi::Property<unsigned> MuonValR4::RootVisualizationService::m_canvasHeight {this, "CanvasHeight", 600}
private

Height of all drawn Canvases.

Definition at line 52 of file RootVisualizationService.h.

◆ m_canvasMutex

std::mutex MuonValR4::RootVisualizationService::m_canvasMutex {}
private

Definition at line 75 of file RootVisualizationService.h.

◆ m_canvasWidth

Gaudi::Property<unsigned> MuonValR4::RootVisualizationService::m_canvasWidth {this, "CanvasWidth", 800}
private

Width of all drawn Canvases.

Definition at line 50 of file RootVisualizationService.h.

◆ m_lumiLabel

Gaudi::Property<std::string> MuonValR4::RootVisualizationService::m_lumiLabel {this, "LumiLabel", ""}
private

Luminosity label.

Definition at line 48 of file RootVisualizationService.h.

◆ m_outDir

Gaudi::Property<std::string> MuonValR4::RootVisualizationService::m_outDir {this, "outputDir" , "./Displays/"}
private

Directory into which all plots are written to

Definition at line 54 of file RootVisualizationService.h.

◆ m_outFile

std::unique_ptr<TFile> MuonValR4::RootVisualizationService::m_outFile {}
private

File into which all Canvases are saved if root is defined as extension.

Definition at line 72 of file RootVisualizationService.h.

◆ m_outRootFileName

Gaudi::Property<std::string> MuonValR4::RootVisualizationService::m_outRootFileName {this, "outputROOTFile", "AllDisplays.root"}
private

Name of the ROOT file into which the output Canvases are written (needs root to be in the list of outputFormats of the client tokens)

Definition at line 57 of file RootVisualizationService.h.

◆ m_quadCanvas

Gaudi::Property<bool> MuonValR4::RootVisualizationService::m_quadCanvas {this, "QuadraticCanas", true}
private

Ensure that the canvas has the same interval sizes in x & y.

Definition at line 42 of file RootVisualizationService.h.

◆ m_sqrtSLabel

Gaudi::Property<std::string> MuonValR4::RootVisualizationService::m_sqrtSLabel {this, "SqrtSLabel", "14"}
private

Centre of mass energy label.

Definition at line 46 of file RootVisualizationService.h.

◆ m_storage

StorageMap_t MuonValR4::RootVisualizationService::m_storage {}
private

Definition at line 70 of file RootVisualizationService.h.

◆ m_storageMutex

std::mutex MuonValR4::RootVisualizationService::m_storageMutex {}
private

Definition at line 74 of file RootVisualizationService.h.


The documentation for this class was generated from the following files:
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
MuonValR4::RootVisualizationService::m_outFile
std::unique_ptr< TFile > m_outFile
File into which all Canvases are saved if root is defined as extension.
Definition: RootVisualizationService.h:72
MuonValR4::RootVisualizationService::m_storage
StorageMap_t m_storage
Definition: RootVisualizationService.h:70
MuonValR4::RootVisualizationService::m_canvasHeight
Gaudi::Property< unsigned > m_canvasHeight
Height of all drawn Canvases.
Definition: RootVisualizationService.h:52
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
vtune_athena.format
format
Definition: vtune_athena.py:14
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
make_coralServer_rep.opt
opt
Definition: make_coralServer_rep.py:19
MuonValR4::RootVisualizationService::m_canvasWidth
Gaudi::Property< unsigned > m_canvasWidth
Width of all drawn Canvases.
Definition: RootVisualizationService.h:50
MuonValR4::RootVisualizationService::m_quadCanvas
Gaudi::Property< bool > m_quadCanvas
Ensure that the canvas has the same interval sizes in x & y.
Definition: RootVisualizationService.h:42
MuonValR4::RootVisualizationService::paintObjects
void paintObjects(const ClientToken &token, PlotVec_t &&toDraw)
Definition: RootVisualizationService.cxx:90
MuonValR4::RootVisualizationService::m_storageMutex
std::mutex m_storageMutex
Definition: RootVisualizationService.h:74
MuonValR4::removeNonAlphaNum
std::string removeNonAlphaNum(std::string str)
Removes all non-alpha numerical characters from a string.
Definition: FileHelpers.cxx:10
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
DeMoUpdate.newCanvas
bool newCanvas
Definition: DeMoUpdate.py:1115
MuonValR4::RootVisualizationService::m_outRootFileName
Gaudi::Property< std::string > m_outRootFileName
Name of the ROOT file into which the output Canvases are written (needs root to be in the list of out...
Definition: RootVisualizationService.h:57
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
MuonValR4::RootVisualizationService::m_canvasExtraScale
Gaudi::Property< double > m_canvasExtraScale
Extra safety margin to zoom out from the Canvas.
Definition: RootVisualizationService.h:40
MuonValR4::RootVisualizationService::m_sqrtSLabel
Gaudi::Property< std::string > m_sqrtSLabel
Centre of mass energy label.
Definition: RootVisualizationService.h:46
MuonValR4::RootVisualizationService::m_canvasMutex
std::mutex m_canvasMutex
Definition: RootVisualizationService.h:75
DQPostProcessTest.outFile
outFile
Comment Out Those You do not wish to run.
Definition: DQPostProcessTest.py:36
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:76
THROW_EXCEPTION
#define THROW_EXCEPTION(MESSAGE)
Definition: throwExcept.h:10
MuonValR4::RootVisualizationService::PlotPtr_t
std::shared_ptr< detail::DrawCanvasObject > PlotPtr_t
Definition: RootVisualizationService.h:31
a
TList * a
Definition: liststreamerinfos.cxx:10
MuonValR4::RootVisualizationService::m_outDir
Gaudi::Property< std::string > m_outDir
Directory into which all plots are written to
Definition: RootVisualizationService.h:54
MuonValR4::RootVisualizationService::m_lumiLabel
Gaudi::Property< std::string > m_lumiLabel
Luminosity label.
Definition: RootVisualizationService.h:48
MuonValR4::drawAtlasLabel
std::unique_ptr< TLatex > drawAtlasLabel(const double xPos, const double yPos, const std::string &status="Internal")
Create a ATLAS label.
Definition: VisualizationHelpers.cxx:80
MuonValR4::RootVisualizationService::m_AtlasLabel
Gaudi::Property< std::string > m_AtlasLabel
ATLAS label (Internal / Prelimnary / Simulation)
Definition: RootVisualizationService.h:44
MuonValR4::ensureDirectory
void ensureDirectory(const std::string &path)
Ensures that the subdirectory in the path is created.
Definition: FileHelpers.cxx:17
MuonValR4::drawLumiSqrtS
std::unique_ptr< TLatex > drawLumiSqrtS(const double xPos, const double yPos, const std::string_view sqrtS="14", const std::string_view lumi="")
Create a luminosity sqrtS label.
Definition: VisualizationHelpers.cxx:84
python.SystemOfUnits.ms
float ms
Definition: SystemOfUnits.py:148