Home | News | Documentation | Download

Stop SetLineColor from overwriting previous colours

I have 3 TH1Fs that I plot in 3 different combinations:

h1->Draw();
h2->Draw("sames");
h2->SetLineColor(kRed);

TCanvas* c2 = new TCanvas*("c2","c2",0,0,800,800);
h1->Draw();
h3->Draw("sames");
h3->SetLineColor(kRed);

TCanvas* c3 = new TCanvas*("c3","c3",0,0,800,800);
h1->Draw();
h2->Draw("sames");
h3->Draw("sames");
h3->SetLineColor(kBlack);

I would like the colour of h3 to be kRed in c2 but kBlack in c3, but setting it to kBlack under c3 changes it to kBlack also in c2.

Please can someone tell me how to achieve this? Do I have to clone the histogram and set it manually that way?

Thanks in advance.

Search for DrawCopy on the TH1 documentation.

For some reason this seems to do the opposite of what I want it to do. When I do

h3->DrawCopy();
h3->SetLineColor(kBlack);

on c3, the histogram is turned black on c2 but remains red on c3. I assume this is because doing DrawCopy() creates a new object, which is not the same one as I access when doing h3->SetLineColor();.

Is there a way of accessing this new object or should I simply reverse the declarations of kBlack and kRed?

Thanks!

TH1F *h3_copy = h3->DrawCopy();
h3_copy->SetLineColor(kBlack);

This works when I just call h3_copy->Draw(); but when I call h3->Draw("sames"); I get a segmentation fault

PS: doing TH1F* h3_copy = h3->DrawCopy(); gives me the error: "cannot initialize a variable of type ‘TH1F *’ with an rvalue of type 'TH1 '", even though h3 is initialised as a TH1F

Can you post a running example ?

This works, but if you restore the commented line it seg faults (ROOT6):

#include "TH1.h"
#include "TCanvas.h"

void TH1FCopyTest(){

  TH1F* h1 = new TH1F("h1","h1",100,0,100);
  TH1F* h2 = new TH1F("h2","h2",100,0,200);
  TH1F* h3 = new TH1F("h3","h3",100,0,300);
  
  for(int i=0;i<100;i++){
    h1->Fill(i);
    h2->Fill(2*i);
    h3->Fill(3*i);
  }

  TCanvas* c1 = new TCanvas("c1","c1",0,0,800,800);
  h1->Draw();
  h2->Draw("same");
  h2->SetLineColor(kRed);
  
  TCanvas* c2 = new TCanvas("c2","c2",0,0,800,800);
  h1->Draw();
  h3->Draw("same");
  h3->SetLineColor(kRed);

  TCanvas* c3 = new TCanvas("c3","c3",0,0,800,800);
  TH1* h3Copy = h3->DrawCopy();
  h1->Draw();
  h2->Draw("same");
  h2->SetLineColor(kRed);
  //h3Copy->Draw("same");

}

This doesn’t seem to work either

  TCanvas* c4 = new TCanvas("c4","c4",0,0,800,800);
  h3Copy->Draw();
  h1->Draw("same");
  h2->Draw("same");
#include "TH1.h"
#include "TCanvas.h"

/// I would like the colour of h3 to be kRed in c2 but kBlack in c3

void TH1FCopyTest(){

   TH1F* h1 = new TH1F("h1","h1",6,0,6);
   TH1F* h2 = new TH1F("h2","h2",6,0,6);
   TH1F* h3 = new TH1F("h3","h3",6,0,6);

   h1->Fill(1);
   h2->Fill(3);
   h3->Fill(5);

   h1->SetLineWidth(5);
   h2->SetLineWidth(5);
   h3->SetLineWidth(5);

   TCanvas* c1 = new TCanvas("c1","c1",0,0,800,800);
   h1->Draw();
   h2->Draw("same");
   h2->SetLineColor(kRed);

   TCanvas* c2 = new TCanvas("c2","c2",0,0,800,800);
   h1->Draw();
   h3->Draw("same");
   h3->SetLineColor(kRed);

   TCanvas* c3 = new TCanvas("c3","c3",0,0,800,800);
   h1->Draw();
   TH1* h3Copy = h3->DrawCopy("same");
   h3Copy->SetLineColor(kBlack);
}
1 Like

That’s really helpful, thank you so much!

Is there an equivalent of DrawCopy for TLines?

No, you create a new line or use DrawLine.
See https://root.cern/doc/master/classTLine.html