Pass pointer to TInterpreter

Hi! How can i pass an pointer to the Execute method of TInterpreter?

What i do :
TInterpreter* myint = TInterpreter::Instance();
TString arg ("->Print("")");
… a loop over all browsables that are TH1
TH1* histo = dynamic_cast<TH1*>(obj);
TString arg1 = “histo” + arg ;
myint->ProcessLine(arg.Data());

but histo identifier is not recognized

What am i trying to do :
Apply a method with argument(s) over all created histograms

Goal is being able to do :
ForEachH (“GetXaxis()->SetTitle(“my_new_title”)”);

Any idea on this?
Thank you!!
Adrian

Hi Adrian,

here the trick is to pass the pointer as a string and cast it to the right type to then call methods on it. See for example how we do this in the TDataFrame: https://root.cern/doc/v610/TDFInterface_8hxx_source.html#l00287

Cheers,
D

Hi! Thanks! well, i don’t know if i got it right but i succeeded like this :

static void ForEachH (TString arg = "") {
  if ( arg.IsNull() ) {return;}
  arg = "->" + arg + ";" ;
//  cout << arg.Data() << endl;
  
  if ( gROOT->GetListOfCanvases()->GetEntries() == 0 ) {return;}

  TSeqCollection* list_canvases = gROOT->GetListOfCanvases();

  TInterpreter* myint = TInterpreter::Instance();

  for (int i=0; i<list_canvases->GetEntries(); ++i) {
    TPad* canvas = dynamic_cast<TPad*> (list_canvases->At(i));
    TList* glist = canvas->GetListOfPrimitives();

    for (int i=0; i<glist->GetEntries(); ++i) {
      TObject *obj = glist->At(i);

      if ( obj->InheritsFrom("TH1") ) {
        TH1* histo = dynamic_cast<TH1*>(obj);
        std::stringstream h_str ("");
        h_str << histo;
        TString histo_ptr_str = h_str.str();
        TString arg1 = "(reinterpret_cast<TH1*>(" + histo_ptr_str + "))" + arg ;
        myint->ProcessLine(arg1.Data());
        canvas->Modified();
        }

      if ( obj->InheritsFrom("TPad") ) {
        TPad* pad = dynamic_cast<TPad*>(obj);
        TList* padlist = pad->GetListOfPrimitives();
        for (int i=0; i<padlist->GetEntries(); ++i) {
          TObject *obj = padlist->At(i);
          if ( obj->InheritsFrom("TH1") ) {
            TH1* histo = dynamic_cast<TH1*>(obj);
            std::stringstream h_str ("");
            h_str << histo;
            TString histo_ptr_str = h_str.str();
            TString arg2 = "(reinterpret_cast<TH1*>(" + histo_ptr_str + "))" + arg ;
            myint->ProcessLine(arg2.Data());
            pad->Modified();
            }
          }
        }
    }
  }
}

and now i can do something like this :

ForEachH("GetXaxis()->SetTitle(\"test_{exemplu}^{test}\")")

while it works (which is good enough for me) i am still wondering if this is what you meant and if the code is ok (hope that i did not miss some obvious flaws)

Thank you!
Adrian

Hi Adrian,

glad to hear that works for you.

Cheers,
D

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