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.
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:
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).
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();
}