Is it possible to save plain text in a ROOT file?

Dear ROOTers,

Is it possible to save plain text in a ROOT file?

I have tried with a TName, but maybe it is not the best way of doing it.

Thank you for your time.

Regards,
ATD.

Try with a TString or the “title” of a TNamed.

1 Like

Another option is TObjString.

1 Like

Hello

Thank you, that was what I was looking for.

For anyone with this issue, please find attached a working example.

#include "TFile.h"
#include "TObjString.h"

#include <string>
#include <iostream>

void writeString( std::string & ostring )
{
  TFile * ofile = TFile::Open("junk.root","recreate");
  TObjString a( ostring.c_str() );
  ofile->WriteObject( &a, "myStr"  );
  ofile->Close();
  return;
}

void readString()
{
  TFile * ofile = TFile::Open("junk.root","read");
  TObjString * a;
  ofile->GetObject( "myStr" , a);
  ofile->ls();
  std::cerr << "\n\t** String: " << a->GetString().Data() << std::endl;
  ofile->Close();
  
  return;
}

int saveString( std::string dummyString = "atd" )
{
  writeString( dummyString );
  readString();
  return 0;
}

Regards,
ATD

1 Like

Another option (especially for code) is TMacro.

1 Like

Hi ATD,

nice reference for future Forum readers, thanks!
I add a last example, using directly std::string, just to complement your solution:

void writeString()
{
  TFile ofile("myfile.root","recreate");
  std::string a( "My very nice STL string" );
  ofile.WriteObject( &a, "myStr"  );
}
void readString()
{
  TFile ifile("junk.root","read");
  std::string *aptr;
  ifile.GetObject( "myStr" , aptr);
  auto &a = *aptr;
  ifile.ls();
  std::cerr << "\n\t** String: " << a << std::endl;
}

Cheers,
D

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.