All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Member Functions | Protected Member Functions | Static Private Member Functions | Private Attributes | List of all members
icarus::trigger::PlotSandbox Class Reference

A helper to manage ROOT objects with consistent naming. More...

#include <PlotSandbox.h>

Classes

struct  Data_t
 The whole data in a convenient package! More...
 
struct  TFileDirectoryHelper
 Contains both a art::TFileDirectory and the TDirectory it manages. More...
 

Public Member Functions

 PlotSandbox (art::TFileDirectory parentDir, std::string name, std::string desc)
 Constructor: specifies all sandbox characteristics. More...
 
 PlotSandbox (PlotSandbox const &)=delete
 
 PlotSandbox (PlotSandbox &&from)
 
PlotSandboxoperator= (PlotSandbox const &)=delete
 
PlotSandboxoperator= (PlotSandbox &&from)=delete
 
virtual ~PlotSandbox ()=default
 Virtual destructor. Default, but C++ wants it. More...
 
bool hasName () const
 Returns whether we have a non-empty name. More...
 
std::string const & name () const
 Returns the sandbox name. More...
 
bool hasDescription () const
 Returns whether we have a non-empty description. More...
 
std::string const & description () const
 Returns the sandbox description. More...
 
virtual std::string const & Description () const
 
std::string ID () const
 Returns a string ID for this sandbox. More...
 
virtual std::string processName (std::string const &name) const
 Processes the specified string as it were a name. More...
 
virtual std::string processTitle (std::string const &title) const
 Processes the specified string as it were a description or title. More...
 
bool deleteSubSandbox (std::string const &name)
 Deletes the subbox with the specified name and its directory. More...
 
template<typename Stream >
void dump (Stream &&out, std::string indent, std::string firstIndent) const
 Dumps the hierarchy of sandboxes into the specified stream. More...
 
template<typename Stream >
void dump (Stream &&out, std::string indent="") const
 Dumps the hierarchy of sandboxes into the specified stream. More...
 
ROOT object management
bool empty () const
 Returns if the sandbox is empty (neither it nor subboxes hold objects). More...
 
template<typename Obj = TObject>
Obj const * get (std::string const &name) const
 Fetches the object with the specified name from the sandbox. More...
 
template<typename Obj = TObject>
Obj * use (std::string const &name) const
 Fetches an object with the specified name to be modified. More...
 
template<typename Obj = TObject>
Obj & demand (std::string const &name) const
 Fetches an object with the specified name to be modified. More...
 
template<typename DirObj = TDirectory>
DirObj * getDirectory () const
 Fetches the base directory of the sandbox. More...
 
template<typename DirObj = TDirectory>
DirObj * getDirectory (std::string const &path) const
 Fetches the directory with the specified name from the sandbox. More...
 
template<typename Obj , typename... Args>
Obj * make (std::string const &name, std::string const &title, Args &&...args)
 Creates a new ROOT object with the specified name and title. More...
 
Contained sandboxes
template<typename SandboxType = PlotSandbox, typename... Args>
SandboxType & addSubSandbox (std::string const &name, std::string const &desc, Args &&...args)
 Creates a new sandbox contained in this one. More...
 
std::size_t nSubSandboxes () const
 Returns the number of contained sand boxes. More...
 
PlotSandbox const * findSandbox (std::string const &name) const
 Returns the first contained sandbox with the specified name. More...
 
PlotSandboxfindSandbox (std::string const &name)
 
PlotSandbox const & demandSandbox (std::string const &name) const
 Returns the first contained sandbox with the specified name. More...
 
PlotSandboxdemandSandbox (std::string const &name)
 
decltype(auto) subSandboxes () const
 Returns an object proper to iterate through all contained sand boxes. More...
 
decltype(auto) subSandboxes ()
 

Protected Member Functions

 PlotSandbox (PlotSandbox const &parent, std::string name, std::string desc)
 Constructor: specifies all sandbox characteristics. More...
 
virtual void setParent (PlotSandbox const *parent)
 Sets the parent of this box. More...
 
std::string processPlotTitle (std::string const &title) const
 Applies title processing only at the title part of the string. More...
 
virtual std::string processedSandboxName () const
 
virtual std::string processedSandboxDesc () const
 
template<typename Stream >
void dumpContent (Stream &&out, std::string indent, std::string firstIndent) const
 Dumps the content of this box (nosubboxes) into out stream. More...
 

Static Private Member Functions

template<typename SandboxType >
static auto * findSandbox (SandboxType &sandbox, std::string const &name)
 Helper function for findSandbox() implementations. More...
 
template<typename SandboxType >
static auto & demandSandbox (SandboxType &sandbox, std::string const &name)
 Helper function for demandSandbox() implementations. More...
 

Private Attributes

struct
icarus::trigger::PlotSandbox::Data_t 
fData
 

Object creation specializations

These are implementation details.

PlotSandbox manages the creation of objects to be put into ROOT directories and acts as an interface between art::TFileDirectory and user code. In particular it takes the responsibility of constructing new objects to be stored in the directory. There are special arguments to these objects, that are the name and, to a lesser extent, a title. PlotSandbox manipulates the name of the object to make it unique, and therefore it has to have full control of everywhere a name is set. The name is crucial in ROOT because it is the key for searching the object. So it has a double role of the name of the object (property of the object itself) and the key of the container that hosts that object. Some ROOT objects can and must be constructed with a name, and usually a title too (e.g. TH1 and TTree hierarchies), and their constructor takes these as the first two arguments. Other object, though, do not follow that pattern (e.g. TGraph), and worse, these object might accept strings as first constructor arguments, assigning them a different meaning. For these objects a different initialization pattern is necessary, where the object is constructed with user-specified arguments first, and then the name (and, if supported, the title) are set.

PlotSandbox interface attempts to save the user from this hassle, but because specific actions are needed for specific objects, it needs to know these needs case by case. This implementation tries to give the necessary flexibility to easily implement special cases.

A few basic tools are provided, which support specific types of objects:

  • makeWithNameTitle() supports the objects which can be constructed with a name and a title (e.g. TH1);
  • makeAndSetNameTitle(), support objects which are known not to use name and title as first constructor arguments, but do provide SetName() (safe bet, coming from TObject) and SetTitle().

Building on that, different overloads of doConstruct() use one or another of these utilities, or none, to create the correct type of object. The overload is entrusted to C++ via the pointer type (which C++ matches with a pointer to a base class).

Note that in this strategy it is not possible to use a constructor without name and title for an object that also supports the construction with name and title: as long as PlotSandbox recognizes an object as falling in that category, it will always construct it by calling a constructor with name and title as first arguments.

Also note that no name decoration happens at this level: all object names and titles are expected to be final (processing happens e.g. in make()).

template<typename Obj , typename... Args>
Obj * makeWithNameTitle (art::TFileDirectory &destDir, std::string const &name, std::string const &title, Args &&...args) const
 Creates, stores and returns a new object. More...
 
template<typename Obj , typename... Args>
Obj * makeAndSetNameTitle (art::TFileDirectory &destDir, std::string const &name, std::string const &title, Args &&...args) const
 Creates, stores and returns a new object. More...
 
template<typename Obj , typename... Args>
Obj * doConstruct (art::TFileDirectory &destDir, TObject *, std::string const &name, std::string const &title, Args &&...args) const
 General implementation: creates and registers an Obj (name and title used in Obj constructor). More...
 
template<typename GraphObj , typename... Args>
GraphObj * doConstruct (art::TFileDirectory &destDir, TGraph *, std::string const &name, std::string const &title, Args &&...args) const
 Implementation for TGraph-derived objects. More...
 
static std::pair< std::string,
std::string > 
splitPath (std::string const &path, char sep= '/')
 
static std::string joinPath (std::initializer_list< std::string > pathElements, char sep= '/')
 

Detailed Description

A helper to manage ROOT objects with consistent naming.

A sandbox includes a ROOT directory where all the objects are written. It also provides a name pattern to modify a generic object name in one specific to this sandbox, e.g. from "HEnergy" to "HMuonNeutrinoEnergy". Object descriptions (usually titles) may also be processed in the same way.

In addition, the sandbox can point to a parent sandbox, in which case the names and descriptions are first processed by this sandbox and then passed to that parent sandbox for further processing.

The sandbox has two characterizing strings:

Note
By convention the subdirectory names are not processed.

Definition at line 95 of file PlotSandbox.h.

Constructor & Destructor Documentation

icarus::trigger::PlotSandbox::PlotSandbox ( art::TFileDirectory  parentDir,
std::string  name,
std::string  desc 
)

Constructor: specifies all sandbox characteristics.

Parameters
parentDirROOT directory under which the sandbox is created
namethe name of the sandbox
descdescription of the sandbox

If the name is empty, as a special case, the sandbox is unnamed and it is created directly in parentDir. In that case, the title of the output directory (parentDir) is also not changed. If name or desc are empty, the pertaining processing is not performed: if name is empty, ROOT object names will be unchanged, and if desc is empty their descriptions/titles will be.

To create a sandbox with a parent, call addSubSandbox() of that parent.

Definition at line 80 of file PlotSandbox.cxx.

84  : fData(std::move(name), std::move(desc),
85  name.empty()
86  ? TFileDirectoryHelper::create(parentDir)
87  : TFileDirectoryHelper::create(parentDir, name, desc)
88  )
89 {}
static TFileDirectoryHelper create(art::TFileDirectory parentDir, std::string const &subdir, std::string const &dirTitle="")
Creates a helper managing a subdirectory of parentDir.
struct icarus::trigger::PlotSandbox::Data_t fData
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
icarus::trigger::PlotSandbox::PlotSandbox ( PlotSandbox const &  )
delete
icarus::trigger::PlotSandbox::PlotSandbox ( PlotSandbox &&  from)

Definition at line 93 of file PlotSandbox.cxx.

94  : fData(std::move(from.fData))
95 {
96  fData.resetSubboxParents(this); // adjust the parent pointers of the subboxes
97 } // PlotSandbox::PlotSandbox(PlotSandbox&&)
struct icarus::trigger::PlotSandbox::Data_t fData
void resetSubboxParents(PlotSandbox const *newParent)
Definition: PlotSandbox.cxx:73
virtual icarus::trigger::PlotSandbox::~PlotSandbox ( )
virtualdefault

Virtual destructor. Default, but C++ wants it.

icarus::trigger::PlotSandbox::PlotSandbox ( PlotSandbox const &  parent,
std::string  name,
std::string  desc 
)
protected

Constructor: specifies all sandbox characteristics.

Parameters
parentthe sandbox used as parent
namethe name of the sandbox
descdescription of the sandbox

Compared to the public constructor, this one picks the parent directory to be the output directory of the parent sandbox, and it registers that parent box as parent. Note that this constructor does not update the list of subboxes of the parent.

Definition at line 192 of file PlotSandbox.cxx.

193  : PlotSandbox(parent.fData.outputDir.fDir, std::move(name), std::move(desc))
194 {
195  setParent(&parent);
196 }
virtual void setParent(PlotSandbox const *parent)
Sets the parent of this box.
Definition: PlotSandbox.h:410
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
PlotSandbox(art::TFileDirectory parentDir, std::string name, std::string desc)
Constructor: specifies all sandbox characteristics.
Definition: PlotSandbox.cxx:80

Member Function Documentation

template<typename SandboxType , typename... Args>
SandboxType & icarus::trigger::PlotSandbox::addSubSandbox ( std::string const &  name,
std::string const &  desc,
Args &&...  args 
)

Creates a new sandbox contained in this one.

Template Parameters
SandboxType(default: PlotSandbox) type of sandbox to be created
Argstypes of the additional arguments for the sandbox constructor
Parameters
namename of the new contained sand box
descdescription of the new contained sand box
argsadditional arguments for the sandbox constructor
Returns
a reference to the created sandbox
Exceptions
cet::exception(category: "PlotSandbox") if a sandbox with this name already exists.

The arguments of this method are equivalent to the ones of the constructor.

The new sand box parent is set to point to this sand box.

Definition at line 723 of file PlotSandbox.h.

724 {
725  // we can't use make_unique() because the constructor it needs is protected:
726  auto [ it, bInserted ] = fData.subBoxes.try_emplace
727  (name, new SandboxType(*this, name, desc, std::forward<Args>(args)...));
728  if (!bInserted) {
729  throw cet::exception("PlotSandbox")
730  << "PlotSandbox::addSubSandbox(): a subbox with name '" << name
731  << "' already exists in box '" << ID() << "'.\n";
732  }
733  return *(it->second); // it iterator to the inserted element
734 } // icarus::trigger::PlotSandbox::addSubSandbox()
std::string ID() const
Returns a string ID for this sandbox.
struct icarus::trigger::PlotSandbox::Data_t fData
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
std::map< std::string, std::unique_ptr< PlotSandbox > > subBoxes
Contained sand boxes.
Definition: PlotSandbox.h:131
bool icarus::trigger::PlotSandbox::deleteSubSandbox ( std::string const &  name)

Deletes the subbox with the specified name and its directory.

Returns
whether there was a subbox with that name

Definition at line 174 of file PlotSandbox.cxx.

174  {
175 
176  auto const it = fData.subBoxes.find(name);
177  if (it == fData.subBoxes.end()) return false;
178 
179  if (it->second) {
180  auto&& subbox = std::move(it->second); // will get destroyed at end of scope
181  if (subbox->getDirectory()) delete subbox->getDirectory();
182  if (getDirectory()) getDirectory()->Delete((name + ";*").c_str());
183  }
184 
185  fData.subBoxes.erase(it);
186  return true;
187 } // icarus::trigger::PlotSandbox::deleteSubSandbox()
struct icarus::trigger::PlotSandbox::Data_t fData
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
DirObj * getDirectory() const
Fetches the base directory of the sandbox.
Definition: PlotSandbox.h:683
std::map< std::string, std::unique_ptr< PlotSandbox > > subBoxes
Contained sand boxes.
Definition: PlotSandbox.h:131
template<typename Obj >
Obj & icarus::trigger::PlotSandbox::demand ( std::string const &  name) const

Fetches an object with the specified name to be modified.

Template Parameters
Obj(default: TObject) type of the object to fetch
Parameters
nameunprocessed name and path of the object to fetch
Returns
the requested object
Exceptions
cet::exception(category: "PlotSandbox") if no object with name exists in the box
See Also
get(), use()

This method is equivalent to use(), with the difference that the returned object must exist.

Definition at line 667 of file PlotSandbox.h.

667  {
668 
669  auto* obj = use<Obj>(name);
670  if (obj) return *obj;
671  cet::exception e { "PlotSandbox" };
672  e << "PlotSandbox::demand(): object '" << name
673  << "' not available in the sandbox '" << ID() << "'"
674  << "\nBox content: ";
675  dumpContent(e, "", ""); // no indent
676 
677  throw e << "\n";
678 } // icarus::trigger::PlotSandbox::demand()
std::string ID() const
Returns a string ID for this sandbox.
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
do i e
void dumpContent(Stream &&out, std::string indent, std::string firstIndent) const
Dumps the content of this box (nosubboxes) into out stream.
Definition: PlotSandbox.h:844
template<typename SandboxType >
auto & icarus::trigger::PlotSandbox::demandSandbox ( SandboxType &  sandbox,
std::string const &  name 
)
staticprivate

Helper function for demandSandbox() implementations.

Definition at line 750 of file PlotSandbox.h.

751 {
752  auto* box = findSandbox(sandbox, name);
753  if (box) return *box;
754 
755  cet::exception e { "PlotSandbox" };
756  e << "PlotSandbox::demandSandbox(): box '" << name
757  << "' not available in the sandbox '" << sandbox.ID() << "'";
758  if (sandbox.nSubSandboxes()) {
759  e << "\n" << "Available nested boxes (" << sandbox.nSubSandboxes() << "):";
760  for (auto const& subbox: sandbox.subSandboxes())
761  e << "\n * '" << subbox.ID() << "'";
762  } // if has subboxes
763  else {
764  e << " (no contained box!)";
765  }
766  throw e << "\n";
767 
768 } // icarus::trigger::PlotSandbox::demandSandbox(SandboxType)
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
static auto * findSandbox(SandboxType &sandbox, std::string const &name)
Helper function for findSandbox() implementations.
Definition: PlotSandbox.h:740
do i e
auto icarus::trigger::PlotSandbox::demandSandbox ( std::string const &  name) const

Returns the first contained sandbox with the specified name.

Parameters
nameunprocessed name of the box to be retrieved
Returns
the requested contained sandbox
Exceptions
cet::exception(category: "PlotSandbox") if no a sandbox with this name exists
See Also
findSandbox()

Definition at line 168 of file PlotSandbox.cxx.

170  { return demandSandbox(*this, name); }
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
static auto & demandSandbox(SandboxType &sandbox, std::string const &name)
Helper function for demandSandbox() implementations.
Definition: PlotSandbox.h:750
auto icarus::trigger::PlotSandbox::demandSandbox ( std::string const &  name)

Definition at line 164 of file PlotSandbox.cxx.

166  { return demandSandbox(*this, name); }
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
static auto & demandSandbox(SandboxType &sandbox, std::string const &name)
Helper function for demandSandbox() implementations.
Definition: PlotSandbox.h:750
std::string const& icarus::trigger::PlotSandbox::description ( ) const
inline

Returns the sandbox description.

Definition at line 202 of file PlotSandbox.h.

202 { return fData.desc; }
struct icarus::trigger::PlotSandbox::Data_t fData
std::string desc
The description characterizing the sandbox.
Definition: PlotSandbox.h:126
virtual std::string const& icarus::trigger::PlotSandbox::Description ( ) const
inlinevirtual

Returns the sandbox description for use at the beginning of a sentence.

Todo:
Not implemented yet.

Definition at line 206 of file PlotSandbox.h.

206 { return description(); }
std::string const & description() const
Returns the sandbox description.
Definition: PlotSandbox.h:202
template<typename Obj , typename... Args>
Obj * icarus::trigger::PlotSandbox::doConstruct ( art::TFileDirectory &  destDir,
TObject *  ,
std::string const &  name,
std::string const &  title,
Args &&...  args 
) const
protected

General implementation: creates and registers an Obj (name and title used in Obj constructor).

Template Parameters
Objtype of the object to construct
Argstype of additional arguments to Obj constructor, if any
Parameters
destDirart::TFileDirectory where to store the object
objpointer to Obj; unused except to direct C++ overloading
namefinal name of the object
titlefinal title of the object
argsadditional arguments to Obj constructor

This is the most fundamental method constructing an object, which is used as fallback for all TObject-derived objects if nothing more appropriate is available. It is also the best option in general, when Obj supports it. It requires Obj to have a constructor whose first two arguments are name and title of the object.

Definition at line 795 of file PlotSandbox.h.

800  {
801  return
802  makeWithNameTitle<Obj>(destDir, name, title, std::forward<Args>(args)...);
803 } // icarus::trigger::PlotSandbox::doConstruct()
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
template<typename GraphObj , typename... Args>
GraphObj * icarus::trigger::PlotSandbox::doConstruct ( art::TFileDirectory &  destDir,
TGraph *  ,
std::string const &  name,
std::string const &  title,
Args &&...  args 
) const
protected

Implementation for TGraph-derived objects.

Template Parameters
GraphObjtype of TGraph-derived object to construct
Argstype of arguments to GraphObj constructor, if any
Parameters
destDirart::TFileDirectory where to store the object
graphpointer to GraphObj; unused except to direct C++ overloading
namefinal name of the graph object
titlefinal title of the graph object
argsarguments to GraphObj constructor

This method manages the creation of objects derived from TGraph. Note that TGraph itself does feature a constructor with two strings as first arguments, but that is not the standard name/title constructor (in fact, there is no such standard constructor for TGraph, and that matching one takes a text file path for input and an option string...).

The name and title of the graph are still set, via SetName() and SetTitle() (this action is delegated to TFileDirectory::makeAndRegister()).

Definition at line 808 of file PlotSandbox.h.

813  {
814  return makeAndSetNameTitle<GraphObj>
815  (destDir, name, title, std::forward<Args>(args)...);
816 } // icarus::trigger::PlotSandbox::doConstruct(TGraph)
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
template<typename Stream >
void icarus::trigger::PlotSandbox::dump ( Stream &&  out,
std::string  indent,
std::string  firstIndent 
) const

Dumps the hierarchy of sandboxes into the specified stream.

Definition at line 822 of file PlotSandbox.h.

823 {
824  out << firstIndent;
825  if (hasName()) out << "Box '" << name() << "'";
826  else out << "Unnamed box";
827  if (hasDescription()) out << " (\"" << description() << "\")";
828  out << " [ID=" << ID() << "] with ";
829  dumpContent(std::forward<Stream>(out), indent, "");
830 
831  if (nSubSandboxes()) {
832  out << "\n" << indent << "Nested boxes (" << nSubSandboxes() << "):";
833  for (auto const& subbox: subSandboxes()) {
834  out << "\n";
835  subbox.dump(std::forward<Stream>(out), indent + " ");
836  }
837  } // if has subboxes
838 } // icarus::trigger::PlotSandbox::dump()
std::string ID() const
Returns a string ID for this sandbox.
decltype(auto) subSandboxes() const
Returns an object proper to iterate through all contained sand boxes.
Definition: PlotSandbox.h:369
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
std::size_t nSubSandboxes() const
Returns the number of contained sand boxes.
Definition: PlotSandbox.h:341
std::string const & description() const
Returns the sandbox description.
Definition: PlotSandbox.h:202
bool hasDescription() const
Returns whether we have a non-empty description.
Definition: PlotSandbox.h:199
void dumpContent(Stream &&out, std::string indent, std::string firstIndent) const
Dumps the content of this box (nosubboxes) into out stream.
Definition: PlotSandbox.h:844
bool hasName() const
Returns whether we have a non-empty name.
Definition: PlotSandbox.h:193
template<typename Stream >
void icarus::trigger::PlotSandbox::dump ( Stream &&  out,
std::string  indent = "" 
) const
inline

Dumps the hierarchy of sandboxes into the specified stream.

Definition at line 389 of file PlotSandbox.h.

390  { dump(std::forward<Stream>(out), indent, indent); }
void dump(Stream &&out, std::string indent, std::string firstIndent) const
Dumps the hierarchy of sandboxes into the specified stream.
Definition: PlotSandbox.h:822
template<typename Stream >
void icarus::trigger::PlotSandbox::dumpContent ( Stream &&  out,
std::string  indent,
std::string  firstIndent 
) const
protected

Dumps the content of this box (nosubboxes) into out stream.

Definition at line 844 of file PlotSandbox.h.

845 {
846  out << firstIndent;
847 
848  TDirectory const* pDir = fData.outputDir.fROOTdir;
849  if (!pDir) {
850  out << "no content available";
851  return;
852  }
853 
854  TList const* objects = pDir->GetList();
855  TList const* keys = pDir->GetListOfKeys();
856  if (objects && !objects->IsEmpty()) {
857  out << objects->GetSize() << " direct entries:";
858  for (TObject const* obj: *objects) {
859  out << "\n" << indent << " '" << obj->GetName() << "' ["
860  << obj->IsA()->GetName() << "]";
861  } // for objects
862  }
863  else out << "no direct entries;";
864  if (keys) {
865  for (TObject const* keyObj: *keys) {
866  auto key = dynamic_cast<TKey const*>(keyObj);
867  if (!key) continue;
868  if (objects->Contains(key->GetName())) continue; // already in objects
869  out << "\n" << indent
870  << "[KEY] '" << key->GetName() << "' ["
871  << key->GetClassName() << "]"
872  ;
873  } // for
874  } // if has keys
875 
876 } // icarus::trigger::PlotSandbox::dumpContent()
TFileDirectoryHelper outputDir
Output ROOT directory of the sandbox.
Definition: PlotSandbox.h:133
struct icarus::trigger::PlotSandbox::Data_t fData
bool icarus::trigger::PlotSandbox::empty ( ) const

Returns if the sandbox is empty (neither it nor subboxes hold objects).

Definition at line 124 of file PlotSandbox.cxx.

124  {
125 
126  std::vector<TDirectory const*> subDirectories; // directories of subboxes
127 
128  // if any of the subboxes is not empty, then this one is not either
129  for (auto& subbox: subSandboxes()) {
130  if (!subbox.empty()) return false;
131  subDirectories.push_back(subbox.getDirectory());
132  } // for
133 
134  auto const isSubDirectory = [b=subDirectories.begin(),e=subDirectories.end()]
135  (TObject const* obj)
136  {
137  auto dir = dynamic_cast<TDirectory const*>(obj);
138  return dir && std::find(b, e, dir) != e;
139  };
140 
141  // if there is any object in memory associated to the directory,
142  // that is not empty (directories from subboxes are exempted)
143  for (TObject const* obj: *(getDirectory()->GetList()))
144  if (!isSubDirectory(obj)) return false;
145 
146  // if there is any key associated to the directory, that is not empty
147  if (getDirectory()->GetListOfKeys()->GetSize()) return false;
148 
149  return true;
150 } // icarus::trigger::PlotSandbox::empty()
decltype(auto) subSandboxes() const
Returns an object proper to iterate through all contained sand boxes.
Definition: PlotSandbox.h:369
DirObj * getDirectory() const
Fetches the base directory of the sandbox.
Definition: PlotSandbox.h:683
tuple dir
Definition: dropbox.py:28
do i e
template<typename SandboxType >
auto * icarus::trigger::PlotSandbox::findSandbox ( SandboxType &  sandbox,
std::string const &  name 
)
staticprivate

Helper function for findSandbox() implementations.

Definition at line 740 of file PlotSandbox.h.

741 {
742  auto const it = sandbox.fData.subBoxes.find(name);
743  return (it == sandbox.fData.subBoxes.end())? nullptr: it->second.get();
744 }
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
auto icarus::trigger::PlotSandbox::findSandbox ( std::string const &  name) const

Returns the first contained sandbox with the specified name.

Parameters
nameunprocessed name of the box to be retrieved
Returns
the requested contained sandbox, or nullptr if not found
See Also
demandSandbox()

Definition at line 158 of file PlotSandbox.cxx.

160  { return findSandbox(*this, name); }
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
static auto * findSandbox(SandboxType &sandbox, std::string const &name)
Helper function for findSandbox() implementations.
Definition: PlotSandbox.h:740
auto icarus::trigger::PlotSandbox::findSandbox ( std::string const &  name)

Definition at line 154 of file PlotSandbox.cxx.

156  { return findSandbox(*this, name); }
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
static auto * findSandbox(SandboxType &sandbox, std::string const &name)
Helper function for findSandbox() implementations.
Definition: PlotSandbox.h:740
template<typename Obj >
Obj const * icarus::trigger::PlotSandbox::get ( std::string const &  name) const

Fetches the object with the specified name from the sandbox.

Template Parameters
Obj(default: TObject) type of the object to fetch
Parameters
nameunprocessed name and path of the object to fetch
Returns
a constant pointer to the requested object , or nullptr if not available or wrong type
See Also
use()

The name specification may contain a ROOT path. The directory component of the path, defined by everything preceding a / character, is not processed.

The fetched object is converted to the desired type via dynamic_cast. If conversion fails, a null pointer is returned.

Definition at line 646 of file PlotSandbox.h.

647  { return use<std::add_const_t<Obj>>(name); }
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
template<typename DirObj >
DirObj * icarus::trigger::PlotSandbox::getDirectory ( ) const

Fetches the base directory of the sandbox.

Returns
a pointer to the requested directory, or nullptr if wrong type

The directory is converted to the desired type via dynamic_cast. If conversion fails, a null pointer is returned.

Definition at line 683 of file PlotSandbox.h.

684  { return dynamic_cast<DirObj*>(fData.outputDir.fROOTdir); }
TFileDirectoryHelper outputDir
Output ROOT directory of the sandbox.
Definition: PlotSandbox.h:133
struct icarus::trigger::PlotSandbox::Data_t fData
template<typename DirObj >
DirObj * icarus::trigger::PlotSandbox::getDirectory ( std::string const &  path) const

Fetches the directory with the specified name from the sandbox.

Template Parameters
DirObj(default: TDirectory) type of ROOT directory object to get
Parameters
pathpath of the directory to fetch within this sandbox
Returns
a constant pointer to the requested directory, or nullptr if not available or wrong type

The fetched object is converted to the desired type via dynamic_cast. If conversion fails, a null pointer is returned.

Definition at line 690 of file PlotSandbox.h.

691 {
692  TDirectory* pBaseDir = fData.outputDir.fROOTdir;
693  return dynamic_cast<DirObj*>
694  (path.empty()? pBaseDir: pBaseDir->GetDirectory(path.c_str()));
695 } // icarus::trigger::PlotSandbox::getDirectory()
TFileDirectoryHelper outputDir
Output ROOT directory of the sandbox.
Definition: PlotSandbox.h:133
struct icarus::trigger::PlotSandbox::Data_t fData
BEGIN_PROLOG triggeremu_data_config_icarus settings PMTADCthresholds sequence::icarus_stage0_multiTPC_TPC physics sequence::icarus_stage0_EastHits_TPC physics sequence::icarus_stage0_WestHits_TPC physics producers purityana0 caloskimCalorimetryCryoE physics caloskimCalorimetryCryoW physics path
bool icarus::trigger::PlotSandbox::hasDescription ( ) const
inline

Returns whether we have a non-empty description.

Definition at line 199 of file PlotSandbox.h.

199 { return !fData.desc.empty(); }
struct icarus::trigger::PlotSandbox::Data_t fData
std::string desc
The description characterizing the sandbox.
Definition: PlotSandbox.h:126
bool icarus::trigger::PlotSandbox::hasName ( ) const
inline

Returns whether we have a non-empty name.

Definition at line 193 of file PlotSandbox.h.

193 { return !fData.name.empty(); }
struct icarus::trigger::PlotSandbox::Data_t fData
std::string name
The name/key representing this sandbox.
Definition: PlotSandbox.h:125
std::string icarus::trigger::PlotSandbox::ID ( ) const

Returns a string ID for this sandbox.

Definition at line 101 of file PlotSandbox.cxx.

102  { return fData.parent? (fData.parent->ID() + '/' + name()): name(); }
std::string ID() const
Returns a string ID for this sandbox.
PlotSandbox const * parent
Optional parent sandbox.
Definition: PlotSandbox.h:128
struct icarus::trigger::PlotSandbox::Data_t fData
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
std::string icarus::trigger::PlotSandbox::joinPath ( std::initializer_list< std::string >  pathElements,
char  sep = '/' 
)
staticprotected

Merges the pieces of path that are not empty into a path. One separator at the end of each piece is ignored.

Definition at line 269 of file PlotSandbox.cxx.

270 {
271  if (std::empty(pathElements)) return {};
272 
273  auto stripSep = [sep](std::string const& s) -> std::string_view {
274  return {
275  s.data(),
276  ((s.length() > 1) && (s.back() == sep))? s.length() - 1: s.length()
277  };
278  };
279 
280  auto iElem = pathElements.begin();
281  auto const eend = pathElements.end();
282  auto const& first = stripSep(*iElem);
283  std::string s { first.begin(), first.end() };
284  while (++iElem != eend) {
285  auto const& elem = stripSep(*iElem);
286  if (elem.empty()) continue;
287  if (elem.front() != sep) s.push_back(sep);
288  s += elem;
289  } // while
290  return s;
291 } // icarus::trigger::PlotSandbox::joinPath()
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
bool empty(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:555
template<typename Obj , typename... Args>
Obj * icarus::trigger::PlotSandbox::make ( std::string const &  name,
std::string const &  title,
Args &&...  args 
)

Creates a new ROOT object with the specified name and title.

Template Parameters
Objtype of ROOT object to be created
Argstypes of the argumenst to be forwarded to the constructor
Parameters
nameunprocessed name of the new object
titleunprocessed title of the new object
argsadditional arguments forwarded to the constructor
Returns
a pointer to the newly created object

The name and title are processed with processName() and processTitle() method respectively, before the object is created.

Definition at line 701 of file PlotSandbox.h.

702 {
703  auto [ objDir, objName ] = splitPath(name);
704 
705  std::string const processedName = processName(objName);
706  std::string const processedTitle = processPlotTitle(title);
707 
708  art::TFileDirectory destDir
709  = objDir.empty()? fData.outputDir.fDir: fData.outputDir.fDir.mkdir(objDir);
710 
711  using ObjPtr_t = Obj*;
712  return doConstruct<Obj>(
713  destDir, ObjPtr_t{},
714  processedName, processedTitle, std::forward<Args>(args)...
715  );
716 } // icarus::trigger::PlotSandbox::make()
TFileDirectoryHelper outputDir
Output ROOT directory of the sandbox.
Definition: PlotSandbox.h:133
struct icarus::trigger::PlotSandbox::Data_t fData
static std::pair< std::string, std::string > splitPath(std::string const &path, char sep= '/')
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
virtual std::string processName(std::string const &name) const
Processes the specified string as it were a name.
std::string processPlotTitle(std::string const &title) const
Applies title processing only at the title part of the string.
template<typename Obj , typename... Args>
Obj * icarus::trigger::PlotSandbox::makeAndSetNameTitle ( art::TFileDirectory &  destDir,
std::string const &  name,
std::string const &  title,
Args &&...  args 
) const
protected

Creates, stores and returns a new object.

Template Parameters
Objtype of the new object to create
Argstype of the additional arguments to Obj constructor
Parameters
destDirthe art::TFileDirectory where the object is stored
namefinal name of the object to be created
titlefinal title of the object to be created
argsadditional arguments to Obj constructor, if any
Returns
a pointer to the registered object
See Also
makeAndRegister()

This method registers a new object into destDir via art::TFileDirectory::makeAndRegister(), using only the constructor arguments specified in args. The name and title are set afterwards via SetName() and SetTitle().

Definition at line 785 of file PlotSandbox.h.

788  {
789  return destDir.makeAndRegister<Obj>(name, title, std::forward<Args>(args)...);
790 } // icarus::trigger::PlotSandbox::justMake()
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
template<typename Obj , typename... Args>
Obj * icarus::trigger::PlotSandbox::makeWithNameTitle ( art::TFileDirectory &  destDir,
std::string const &  name,
std::string const &  title,
Args &&...  args 
) const
protected

Creates, stores and returns a new object.

Template Parameters
Objtype of the new object to create
Argstype of the additional arguments to Obj constructor
Parameters
destDirthe art::TFileDirectory where the object is stored
namefinal name of the object to be created
titlefinal title of the object to be created
argsadditional arguments to Obj constructor, if any
Returns
a pointer to the registered object
See Also
makeAndSetNameTitle()

This method registers a new object into destDir via art::TFileDirectory::makeAndRegister(), using the name and title as the first arguments of the constructor.

Definition at line 774 of file PlotSandbox.h.

777  {
778  return destDir.makeAndRegister<Obj>
779  (name, title, name.c_str(), title.c_str(), std::forward<Args>(args)...);
780 } // icarus::trigger::PlotSandbox::makeWithNameTitle()
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
std::string const& icarus::trigger::PlotSandbox::name ( ) const
inline

Returns the sandbox name.

Definition at line 196 of file PlotSandbox.h.

196 { return fData.name; }
struct icarus::trigger::PlotSandbox::Data_t fData
std::string name
The name/key representing this sandbox.
Definition: PlotSandbox.h:125
std::size_t icarus::trigger::PlotSandbox::nSubSandboxes ( ) const
inline

Returns the number of contained sand boxes.

Definition at line 341 of file PlotSandbox.h.

341 { return fData.subBoxes.size(); }
struct icarus::trigger::PlotSandbox::Data_t fData
std::map< std::string, std::unique_ptr< PlotSandbox > > subBoxes
Contained sand boxes.
Definition: PlotSandbox.h:131
PlotSandbox& icarus::trigger::PlotSandbox::operator= ( PlotSandbox const &  )
delete
PlotSandbox& icarus::trigger::PlotSandbox::operator= ( PlotSandbox &&  from)
delete
std::string icarus::trigger::PlotSandbox::processedSandboxDesc ( ) const
protectedvirtual

Returns a processed version of the description of this sandbox.

This may be used when integrating the sandbox description into the processed object descriptions.

Definition at line 242 of file PlotSandbox.cxx.

243 {
244  std::string processed;
245 
246  // if there is no name, the processed name is empty (we don't recurse parents)
247  if (hasDescription()) processed += description();
248 
249  if (fData.parent) processed += ' ' + fData.parent->processedSandboxDesc();
250 
251  return processed;
252 } // icarus::trigger::PlotSandbox::processedSandboxDesc()
PlotSandbox const * parent
Optional parent sandbox.
Definition: PlotSandbox.h:128
struct icarus::trigger::PlotSandbox::Data_t fData
virtual std::string processedSandboxDesc() const
std::string const & description() const
Returns the sandbox description.
Definition: PlotSandbox.h:202
bool hasDescription() const
Returns whether we have a non-empty description.
Definition: PlotSandbox.h:199
std::string icarus::trigger::PlotSandbox::processedSandboxName ( ) const
protectedvirtual

Returns a processed version of the name of this sandbox.

This may be used when integrating the sandbox name into the processed object names.

Definition at line 227 of file PlotSandbox.cxx.

228 {
229  std::string processed;
230 
231  // if there is no name, the processed name is empty (we don't recurse parents)
232  if (!hasName()) return processed;
233 
234  processed = name();
235  if (fData.parent) processed += '_' + fData.parent->processedSandboxName();
236 
237  return processed;
238 } // icarus::trigger::PlotSandbox::processedSandboxName()
PlotSandbox const * parent
Optional parent sandbox.
Definition: PlotSandbox.h:128
struct icarus::trigger::PlotSandbox::Data_t fData
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
virtual std::string processedSandboxName() const
bool hasName() const
Returns whether we have a non-empty name.
Definition: PlotSandbox.h:193
std::string icarus::trigger::PlotSandbox::processName ( std::string const &  name) const
virtual

Processes the specified string as it were a name.

Definition at line 107 of file PlotSandbox.cxx.

108 {
109  std::string const sandboxName = processedSandboxName();
110  return sandboxName.empty()? name: name + '_' + sandboxName;
111 } // icarus::trigger::PlotSandbox::processName()
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
virtual std::string processedSandboxName() const
std::string icarus::trigger::PlotSandbox::processPlotTitle ( std::string const &  title) const
protected

Applies title processing only at the title part of the string.

Definition at line 200 of file PlotSandbox.cxx.

201 {
202  /*
203  * We want to process the title of the plot, but this "title" string might
204  * contain also axis labels (see e.g. `TH1::SetTitle()`).
205  *
206  * We need to first identify the actual title portion of the "title",
207  * then process it alone and merge it back (one Python line...).
208  *
209  */
210 
211  // eat title until an unescaped ';' is found
212  auto const tbegin = title.begin();
213  auto const tend = title.end();
214  auto atend = tbegin; // actual title end
215  while (atend != tend) {
216  if ((*atend == ';') && ((atend == tbegin) || (*std::prev(atend) != '\\')))
217  break;
218  ++atend;
219  } // while
220 
221  return processTitle({ tbegin, atend }).append(atend, tend);
222 
223 } // icarus::trigger::PlotSandbox::processedPlotTitle()
virtual std::string processTitle(std::string const &title) const
Processes the specified string as it were a description or title.
std::string icarus::trigger::PlotSandbox::processTitle ( std::string const &  title) const
virtual

Processes the specified string as it were a description or title.

Definition at line 116 of file PlotSandbox.cxx.

117 {
118  std::string const sandboxDesc = processedSandboxDesc();
119  return sandboxDesc.empty()? title: title + ' ' + sandboxDesc;
120 } // icarus::trigger::PlotSandbox::processTitle()
virtual std::string processedSandboxDesc() const
virtual void icarus::trigger::PlotSandbox::setParent ( PlotSandbox const *  parent)
inlineprotectedvirtual

Sets the parent of this box.

Definition at line 410 of file PlotSandbox.h.

410 { fData.parent = parent; }
PlotSandbox const * parent
Optional parent sandbox.
Definition: PlotSandbox.h:128
struct icarus::trigger::PlotSandbox::Data_t fData
std::pair< std::string, std::string > icarus::trigger::PlotSandbox::splitPath ( std::string const &  path,
char  sep = '/' 
)
staticprotected

Retrieves or, if not present, creates a ROOT subdirectory in the sandbox. Returns a pair with the directory and the name part of path. The directory part may be empty.

Definition at line 257 of file PlotSandbox.cxx.

258 {
259 
260  auto const iSep = path.rfind(sep);
261  if (iSep == std::string::npos) return { {}, path };
262  else return { path.substr(0U, iSep), path.substr(iSep + 1) };
263 
264 } // icarus::trigger::PlotSandbox::splitPath()
BEGIN_PROLOG triggeremu_data_config_icarus settings PMTADCthresholds sequence::icarus_stage0_multiTPC_TPC physics sequence::icarus_stage0_EastHits_TPC physics sequence::icarus_stage0_WestHits_TPC physics producers purityana0 caloskimCalorimetryCryoE physics caloskimCalorimetryCryoW physics path
decltype(auto) icarus::trigger::PlotSandbox::subSandboxes ( ) const
inline

Returns an object proper to iterate through all contained sand boxes.

Definition at line 369 of file PlotSandbox.h.

struct icarus::trigger::PlotSandbox::Data_t fData
decltype(auto) map_dereferenced_values(Map &&map)
Definition: PlotSandbox.h:63
std::map< std::string, std::unique_ptr< PlotSandbox > > subBoxes
Contained sand boxes.
Definition: PlotSandbox.h:131
decltype(auto) icarus::trigger::PlotSandbox::subSandboxes ( )
inline

Definition at line 371 of file PlotSandbox.h.

struct icarus::trigger::PlotSandbox::Data_t fData
decltype(auto) map_dereferenced_values(Map &&map)
Definition: PlotSandbox.h:63
std::map< std::string, std::unique_ptr< PlotSandbox > > subBoxes
Contained sand boxes.
Definition: PlotSandbox.h:131
template<typename Obj >
Obj * icarus::trigger::PlotSandbox::use ( std::string const &  name) const

Fetches an object with the specified name to be modified.

Template Parameters
Obj(default: TObject) type of the object to fetch
Parameters
nameunprocessed name and path of the object to fetch
Returns
a pointer to the requested object , or nullptr if not available or wrong type
See Also
get()

This method is fully equivalent to get(), with the difference that the returned object may be modified.

Definition at line 652 of file PlotSandbox.h.

652  {
653 
654  auto [ objDir, objName ] = splitPath(name);
655 
656  TDirectory* dir = getDirectory(objDir);
657  if (!dir) return nullptr;
658 
659  std::string const processedName = processName(objName);
660  return dir->Get<Obj>(processedName.c_str());
661 
662 } // icarus::trigger::PlotSandbox::use()
static std::pair< std::string, std::string > splitPath(std::string const &path, char sep= '/')
std::string const & name() const
Returns the sandbox name.
Definition: PlotSandbox.h:196
virtual std::string processName(std::string const &name) const
Processes the specified string as it were a name.
DirObj * getDirectory() const
Fetches the base directory of the sandbox.
Definition: PlotSandbox.h:683
tuple dir
Definition: dropbox.py:28

Member Data Documentation

struct icarus::trigger::PlotSandbox::Data_t icarus::trigger::PlotSandbox::fData
private

The documentation for this class was generated from the following files: