TH3F/TH2F Histogram generated legend with a white color

Hi everybody!

I’m run a code that was ok until I put more data and the legend generated was a white color, how can I fix this?

#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TROOT.h"
#include "TBranch.h"
#include "TH1.h"

void enAgX100_1()
{
gROOT->cd();
  THStack *hs =
    new THStack("energy",
                "Energies 0.02720 a 0.13509MeV with Ti 0.05mm with seed Agx100 of Theragnics; ELost/EKin;Counts");
  TH1F *h = new TH1F("h", "h", 100, 0.2, 1.8);
  if (h->GetSumw2N() == 0) h->Sumw2(kTRUE);
  const char *files[] = { "EnSAgX100_27keV.root",
                          "EnSAgX100_30keV.root",
                          "EnSAgX100_31_7keV.root", 
			  "EnSAgX100_35keV.root",
			  "EnSAgX100_35_5keV.root", 
			  "EnSAgX100_36keV.root",
			  "EnSAgX100_37keV.root",
			  "EnSAgX100_40keV.root",
			  "EnSAgX100_45keV.root",
			  "EnSAgX100_55keV.root",
			  "EnSAgX100_85keV.root",
			  "EnSAgX100_135keV.root" };
  const int n = sizeof(files) / sizeof(char*);
  int color = 0;
  for (int i = 0; i < n; i++) {
    TFile *f = TFile::Open(files[i]);
    if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
    TTree *t; f->GetObject("GmDataTTree", t);
    if (!t) { delete f; continue; } // just a precaution
    gROOT->cd();
    t->Project("h", // each file re-fills it from scratch
               "(Event_AccumulatedEnergyLost / Event_InitialKineticEnergy)");
    color += 1; // "next" color
    h->SetLineColor(color); h->SetMarkerColor(color); // do not "SetFillColor"
    h->SetTitle(files[i]);
    hs->Add((TH1F*)(h->Clone(TString::Format("h_%d", i))));
    delete f; // automatically deletes "t", too
  }
  delete h; // no longer needed
  TLegend *l = new TLegend(0.75, 0.9, 0.9, 0.25);
  for (int i = 0; i < hs->GetNhists(); i++)
    { l->AddEntry(hs->GetHists()->At(i), "", "l"); }
  hs->Draw("NOSTACK HIST");
  l->Draw();
} 

@couet maybe you can help here please?

I am not sure I see what you mean …

Check ROOT’s colour codes:
https://root.cern/doc/v610/classTColor.html#C01
when color=10 (and also 0) in your code, you get a white line (and marker) with SetLine/MarkerColor. You need to change that. Maybe make an array with the colour numbers you want and use the values from that array (eg, color=mycolor[i]).

In the histogram, the legend of 55keV isn’t a colour that appears. Could you see that? I’m not sure if is an error or not, but looks like the colour generated is white. Am I clear? If not I may send I new image with more resolution too.

Thanks

no

yes the one you posted is unreadable … see also @dastudillo comments.

I will do that and post here what I get.

Thanks

Please also consider having a look at what @dastudillo said. I think it is the solution …

Yes, @couet I’m considered the solution given by @dastudillo.

Thanks

Like I said I put here a better image.

I’m looking the solution of @dastudillo. When I get it the result I place here.

Thanks.

@couet, @dastudillo Here is my code that I did:

#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TROOT.h"
#include "TBranch.h"
#include "TH1.h"
#include "Rtypes.h"

void enAgX100_1()
{
gROOT->cd();
  THStack *hs =
    new THStack("energy",
                "Energies 0.02720 a 0.13509MeV with Ti 0.05mm with seed Agx100 of Theragnics; ELost/EKin;Counts");
  TH1F *h = new TH1F("h", "h", 100, 0.2, 1.8);
  if (h->GetSumw2N() == 0) h->Sumw2(kTRUE);
  const char *files[] = { "EnSAgX100_27keV.root",
                          "EnSAgX100_30keV.root",
                          "EnSAgX100_31_7keV.root", 
			  "EnSAgX100_35keV.root",
			  "EnSAgX100_35_5keV.root", 
			  "EnSAgX100_36keV.root",
			  "EnSAgX100_37keV.root",
			  "EnSAgX100_40keV.root",
			  "EnSAgX100_45keV.root",
			  "EnSAgX100_55keV.root",
			  "EnSAgX100_85keV.root",
			  "EnSAgX100_135keV.root" };
  const int n = sizeof(files) / sizeof(char*);
  int color = 0;
  int mycolor[i] = 0;
  for (int i = 0; i < n; i++) {
    TFile *f = TFile::Open(files[i]);
    if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
    TTree *t; f->GetObject("GmDataTTree", t);
    if (!t) { delete f; continue; } // just a precaution
    gROOT->cd();
    t->Project("h", // each file re-fills it from scratch
               "(Event_AccumulatedEnergyLost / Event_InitialKineticEnergy)");
    /*color += 1; // "next" color color = mycolor[j];
    h->SetLineColor(color); h->SetMarkerColor(color); // do not "SetFillColor"*/
      for (int i = 0; i < n; i++) {
         color = mycolor[i];
         h->SetLineColor(color); h->SetMarkerColor(color);
      }
    h->SetTitle(files[i]);
    hs->Add((TH1F*)(h->Clone(TString::Format("h_%d", i))));
    delete f; // automatically deletes "t", too
  }
  delete h; // no longer needed
  TLegend *l = new TLegend(0.75, 0.9, 0.9, 0.25);
  for (int i = 0; i < hs->GetNhists(); i++)
    { l->AddEntry(hs->GetHists()->At(i), "", "l"); }
  hs->Draw("NOSTACK HIST");
  l->Draw();
}

What am I do wrong? In attaching the image of the error message.

Thanks!
error|690x347

The color in the legend is taken from the histogram color. To define the color of the histograms you are doing:

h->SetLineColor(color); 
h->SetMarkerColor(color);

Check the value of color (via a printf or a cout for instance very likely you will find that there is a white value somewhere.

Seems to me you should revise the way you set the variablecolor … the array mycolor is very strangely initialized. As a first approximation instead of:

color = mycolor[i];

try:

color = 20 + i;

I did what you told me to do, but the same error continues:
@couet
That’s my change in the code:

int color = 0;
 for (int i = 0; i < n; i++) {
         color = 20 + i;
	 cout << "color is: " << color << endl;
         h->SetLineColor(color); h->SetMarkerColor(color);
      }

Error message:

root [1] .x enAgX100_1.cpp
Warning in TROOT::Append: Replacing existing TH1: h (Potential memory leak).
Error: Non-static-const variable in array dimension enAgX100_1.cpp:31:
_ (cint allows this only in interactive command and special form macro which_
_ is special extension. It is not allowed in source code. Please ignore_
_ subsequent errors.)_
*** Interpreter error recovered ***

I was replying to your question about the white legend. You did not mention any error message before. It is difficult to tell what is wrong as we cannot run your macro (we do not have the data files).

If its necessary I can attached the files here. So you can see.

Thanks

It would be could to have some thing we can run reproducing the problem …

Here I put again the code and the files to reproduce the error. Without this correction of white legend was running but generated a white legend and when I’m doing the code that metioned above (the loop for to color) this error appears.

code

#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TROOT.h"
#include "TBranch.h"
#include "TH1.h"
#include "Rtypes.h"

void enAgX100_1()
{
gROOT->cd();
  THStack *hs =
    new THStack("energy",
                "Energies 0.02720 a 0.13509MeV with Ti 0.05mm with seed Agx100 of Theragnics; ELost/EKin;Counts");
  TH1F *h = new TH1F("h", "h", 100, 0.2, 1.8);
  if (h->GetSumw2N() == 0) h->Sumw2(kTRUE);
  const char *files[] = { "EnSAgX100_27keV.root",
                          "EnSAgX100_30keV.root",
                          "EnSAgX100_31_7keV.root", 
			  "EnSAgX100_35keV.root",
			  "EnSAgX100_35_5keV.root", 
			  "EnSAgX100_36keV.root",
			  "EnSAgX100_37keV.root",
			  "EnSAgX100_40keV.root",
			  "EnSAgX100_45keV.root",
			  "EnSAgX100_55keV.root",
			  "EnSAgX100_85keV.root",
			  "EnSAgX100_135keV.root" };
  const int n = sizeof(files) / sizeof(char*);
  int color = 0;
  //int mycolor[i] = 0;
  for (int i = 0; i < n; i++) {
    TFile *f = TFile::Open(files[i]);
    if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
    TTree *t; f->GetObject("GmDataTTree", t);
    if (!t) { delete f; continue; } // just a precaution
    gROOT->cd();
    t->Project("h", // each file re-fills it from scratch
               "(Event_AccumulatedEnergyLost / Event_InitialKineticEnergy)");
    /*color += 1; // "next" color color = mycolor[j];
    h->SetLineColor(color); h->SetMarkerColor(color); // do not "SetFillColor"*/
      for (int i = 0; i < n; i++) {
         color = 20 + i;
	 cout << "color is: " << color << endl;
         h->SetLineColor(color); h->SetMarkerColor(color);
      }
    h->SetTitle(files[i]);
    hs->Add((TH1F*)(h->Clone(TString::Format("h_%d", i))));
    delete f; // automatically deletes "t", too
  }
  delete h; // no longer needed
  TLegend *l = new TLegend(0.75, 0.9, 0.9, 0.25);
  for (int i = 0; i < hs->GetNhists(); i++)
    { l->AddEntry(hs->GetHists()->At(i), "", "l"); }
  hs->Draw("NOSTACK HIST");
  l->Draw();
}

files

EnSAgX100_27keV.root (836.1 KB)
EnSAgX100_30keV.root (836.4 KB)
EnSAgX100_35_5keV.root (845.9 KB)
EnSAgX100_31_7keV.root (842.5 KB)
EnSAgX100_36keV.root (846.0 KB)
EnSAgX100_35keV.root (839.2 KB)
EnSAgX100_40keV.root (845.8 KB)
EnSAgX100_37keV.root (839.4 KB)
EnSAgX100_45keV.root (839.9 KB)
EnSAgX100_55keV.root (857.0 KB)
EnSAgX100_85keV.root (900.3 KB)
EnSAgX100_135keV.root (1.1 MB)

Thanks!

#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TROOT.h"
#include "TBranch.h"
#include "TH1.h"
#include "Rtypes.h"

void enAgX100_1()
{
   gROOT->cd();
   THStack *hs =
   new THStack("energy",
                "Energies 0.02720 a 0.13509MeV with Ti 0.05mm with seed Agx100 of Theragnics; ELost/EKin;Counts");
   TH1F *h = new TH1F("h", "h", 100, 0.2, 1.8);
   if (h->GetSumw2N() == 0) h->Sumw2(kTRUE);
   const char *files[] = { "EnSAgX100_27keV.root",
                           "EnSAgX100_30keV.root",
                           "EnSAgX100_31_7keV.root",
                           "EnSAgX100_35keV.root",
                           "EnSAgX100_35_5keV.root",
                           "EnSAgX100_36keV.root",
                           "EnSAgX100_37keV.root",
                           "EnSAgX100_40keV.root",
                           "EnSAgX100_45keV.root",
                           "EnSAgX100_55keV.root",
                           "EnSAgX100_85keV.root",
                           "EnSAgX100_135keV.root" };
   const int n = sizeof(files) / sizeof(char*);
   int color = 0;
   for (int i = 0; i < n; i++) {
      TFile *f = TFile::Open(files[i]);
      if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
      TTree *t; f->GetObject("GmDataTTree", t);
      if (!t) { delete f; continue; } // just a precaution
      gROOT->cd();
      t->Project("h", // each file re-fills it from scratch
               "(Event_AccumulatedEnergyLost / Event_InitialKineticEnergy)");
      color = 20 + i;
      h->SetLineColor(color);
      h->SetLineWidth(2);
      h->SetTitle(files[i]);
      hs->Add((TH1F*)(h->Clone(TString::Format("h_%d", i))));
      delete f; // automatically deletes "t", too
   }
   delete h; // no longer needed
   TLegend *l = new TLegend(0.75, 0.9, 0.9, 0.25);
   for (int i = 0; i < hs->GetNhists(); i++) l->AddEntry(hs->GetHists()->At(i), "", "l");
   hs->Draw("nostack hist");
   l->Draw();
}

For now its perfect!

Thanks!

Best regards!

God bless all!

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