Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:54:52+00:00 2026-06-13T14:54:52+00:00

I am not sure how to phrase this question correctly, but this is what

  • 0

I am not sure how to phrase this question correctly, but this is what I am trying to do.

A single histogram can be plotted using cern ROOT with a following command,

(TH1F*)electron->Draw();

But I have tens of histograms named in a sequence, such as electron1, elecron2, electron3, etc, and I want to write a simple loop to plot them all. I tried using sprintf and a simple for loop, but ROOT doesn’t like it.

char name[20];
(TH1F*)electron->Draw();
for(int j=0;j<5;j++){
            sprintf(name, "%s%d","electron",j);
            (TH1F*)name->Draw("same");
 }

What am I doing wrong?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:54:53+00:00Added an answer on June 13, 2026 at 2:54 pm

    You need one additional step. As @twalberg says, you have a string, not an object pointer. For root what you can do is just change your code such that I add one additional line.

    char name[20];
    electron->Draw();
    for(int j=0;j<5;j++){
       sprintf(name, "%s%d","electron",j);
       TH1F *h = (TH1F*)gDirectory->Get(name); // THIS IS THE MISSING LINE
       if ( h ) h->Draw("same"); // make sure the Get succeeded 
     }
    

    The extra line gets the referenced object by name from the local TDirectory. The cast is necessary such that the gDirectory is cast to the right kind of object.

    When you use root interactively this happens magically behind the scenes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not sure how to phrase this question, but I am trying to
Not sure how to phrase this question, but here is the problem. I have
Not completely sure how I can phrase this question best. How can I compare
First, this question may have been asked before, but I'm not sure what phrase
I'm not quite sure how to phrase this question correctly, so I'll start with
Not sure how to phrase this question exactly, but I'll give it a shot.
I am not exactly sure how to phrase this question but the error I
I'm not even sure how I should phrase this question. I'm passing some CustomStruct
I'm not sure how to phrase this question concisely without just giving the example
I am not sure how to phrase my question properly but I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.