How to draw an object with special characters in its name?

In general it is not a good idea to have object’s name containing special character like “”, “/”, “#” etc … or even have object name starting with a number because they might be not accessible from the ROOT command line.

For instance “/” is the separator for directory level in a file therefore an object having a “/” in its name cannot be access from the command line.

Nevertheless it may happen that the some object can be named that way and stored in a ROOT file. The following macro show how to access such object in a file.

#include "Riostream.h"
#include "TFile.h"
#include "TList.h"
#include "TKey.h"

void draw_object(const char *file_name = "myfile.root",
                 const char *obj_name = "name")
   // first open the file
   TFile *file = TFile::Open(file_name);
   if (!file || file->IsZombie()) {
      std::cout << "Cannot open " << file_name << "! Aborting..." << std::endl;
   // get the list of keys
   TList *list = (TList *)file->GetListOfKeys();
   if (!list) {
      std::cout << "Cannot get the list of TKeys! Aborting..." << std::endl;
   // try to find the proper key by its object name
   TKey *key = (TKey *)list->FindObject(obj_name);
   if (!key) {
      std::cout << "Cannot find a TKey named" << obj_name << "! Aborting..." << std::endl;
   // finally read the object itself
   TObject *obj = ((TKey *)key)->ReadObj();
   if (!obj) {
      std::cout << "Cannot read the object named " << obj_name << "! Aborting..." << std::endl;
   // and draw it
1 Like