# SetTitle method not working

**URL:** <https://root-forum.cern.ch/t/settitle-method-not-working/57528>\
**Category:** Newbie\
**Created:** [December 13, 2023, 5:12pm UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528 "2023-12-13T17:12:55Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 13, 2023, 5:12pm UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/1 "2023-12-13T17:12:55Z")

</div>

So I have the following code

```auto
#include "lhcbStyle.C"

void Analysis1()
{
	lhcbStyle();

	// Opening a file using TChain 
	TChain *TreeData = new TChain("Xbc2JpsiPhip/DecayTree");
	TreeData->Add("myrootfile");

	// Defining a cut for the Xibcplus Mass to eliminate background
	TCut cut = "J_psi_1S_M > 3000 && phi_1020_M > 1010 && phi_1020_M < 1030";

	// Creating an empty histogram with 100 bins betwenn 5000 and 8000
	TH1F *h1 = new TH1F("h1","h1", 100, 4500, 8500);

	// Creating the histogram and including the cut 	
	TreeData->Project("h1", "Xi_bcplus_M", cut);
	TCanvas *c1 = new TCanvas("c1", "", 600, 400);

	// Formatting the histogram with titles
	h1->GetXaxis()->SetTitle("Mass [MeV/#it{c^{2}}]");
	h1->GetYaxis()->SetTitle("Entries");
	h1->SetLineColor(4);
	h1->SetTitle("XibcplusM");

	// Adding a legend
	TLegend* leg = new TLegend(0.75,0.75,0.9,0.9);
	leg->AddEntry(h1,"Xibcplus Mass" );

	// Create a TCanvas to draw the histogram on

	// Drawing the histogram on the canvas
	h1->Draw();
	c1->Draw();
	leg->Draw();

	delete TreeData;
	
	
}

```

And no matter how I try I am not able to set a title to my histogram. The titles for the axis are set correctly but the title for the histogram is just not working. I guess this lhcbStyle file is causing some problems. I am attaching that as well.

Also I have just switched over to c++ implementation of ROOT and does the order of commands matter like putting histogram formatting commands after or before drawing the histogram. Does that make a difference?

Thanks for the help

[lhcbStyle.C](https://root-forum.cern.ch/uploads/short-url/s4oLTqFoRNDx4GChsQKdhNKfLqJ.C) (7.4 KB)

ROOT Version: 6.3.0

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [December 14, 2023, 7:24am UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/2 "2023-12-14T07:24:42Z")

</div>

Hi,

Did you try not to set the LHCb Style?  
Please find below a shorter and more performant version of the analysis, which will work also with RNTuple, the new columnar storage layer of ROOT.

Cheers,  
D

```cpp
#include "lhcbStyle.C"

void Analysis1()
{
	lhcbStyle();

	ROOT::RDataFrame df("Xbc2JpsiPhip/DecayTree", "myrootfile.root");
	
	// Create histogram after filtering events
	auto h1 = df.Filter("J_psi_1S_M > 3000 && phi_1020_M > 1010 && phi_1020_M < 1030")
				.Histo1D("h1","h1", 100, 4500, 8500);

	// Create canvas on which the histo is drawn
	auto c1 = new TCanvas("c1", "", 600, 400);

	// Formatting the histogram with titles
	h1->SetLineColor(4);
	h1->SetTitle("XibcplusM;Mass [MeV/#it{c^{2}}];Entries");

	// Adding a legend
	auto leg = new TLegend(0.75,0.75,0.9,0.9);
	leg->AddEntry(h1,"Xibcplus Mass" );

	// Drawing the histogram on the canvas
	h1->Draw();
	leg->Draw();
}

```

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 14, 2023, 7:31am UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/3 "2023-12-14T07:31:49Z")

</div>

Hi Danilo

Thanks for the reply and the code. Yes I tried without the lhcbStyle() and it worked but since I am collaborating with LHCb researchers I wanted to format it that way.

Also I was just about the open another topic related to RDataFrame. When I write a script I have to include \<ROOT/RDataFrame.hxx\> as I saw in this topic [Using RDataFrame in C++](https://root-forum.cern.ch/t/using-rdataframe-in-c/43634) but it says ROOT/RDataFrame.hxx not found and writing the scope resolution operator ROOT::RDataFrame also does not work. Would appreciate any help with this. Thanks

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [December 14, 2023, 2:50pm UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/4 "2023-12-14T14:50:16Z")

</div>

Hi,

Could you check your ROOT build and the presence of the header on your machine? Since you are using 6.30 it has to be there if the build was configured via CMake with `-D dataframe=ON`.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 14, 2023, 3:24pm UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/5 "2023-12-14T15:24:43Z")

</div>

I guess since ROOT was released to 6.30 fully I installed it from Homebrew

---

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [December 14, 2023, 7:58pm UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/6 "2023-12-14T19:58:11Z")

</div>

Hi,

If your problem is that the histogram title is not showing at all, then it is because in `lhcbStyle.C`, the function `SetOptTitle` was called with argument 0.  
The value 0 disable printing the histogram title.  
Just simply change it to 1, or comment the line, then you’ll get the title printed on your plot.

I have a piece of code that may reproduce your situation:

```auto
#include <cstdio>
#include <TStyle.h>
#include <TH1D.h>
#include <TCanvas.h>

int main ( void )
{
	// the SetOptTitle function disable the TH1 title from printing on the canvas.
	// either remove the line or change the argument to 1 to show the title again
	gStyle -> SetOptTitle( 0 );
	TH1D* hist = new TH1D( "hist", "hist", 10, 0, 10 );
	for ( int i=0; i<10; i++ )
		hist -> Fill( 3.4 );
	TCanvas* canvas = new TCanvas( "canvas", "", 400, 300 );
	canvas -> cd( );
	hist -> Draw( "hist" );
	canvas -> SaveAs( "plot_hist.png" );
	canvas -> Close( );
	return 0;
}

```

Hope this works,  
Hoa.

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 15, 2023, 3:47am UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/7 "2023-12-15T03:47:38Z")

</div>

Hi

Thank you for the code @LongHoa . I am having some problems with ROOT right now but I will test this and let you know if it works.

Also @Danilo has version 6.30 not been added to branch latest-stable? To install 6.30 from source which branch will be the best to checkout?

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 16, 2023, 3:15am UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/8 "2023-12-16T03:15:19Z")

</div>

The RDataFrame.hxx file is there in the build files but the program just fails to recognise it. Could this be a problem with how the root environment has been set up?

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 16, 2023, 3:10pm UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/9 "2023-12-16T15:10:53Z")

</div>

Alright so I spent some time debugging the cause. The file not found is not an issue here since I guess the text editor which I use “micro” will have a different place to find the include files and its not where root stores the files. (Apologies for not being specific about where the file not found errors was raised)

So when run `root file_name.c` I don’t get an errors, however the functionality which I am trying to test which is making a simple plot from my root file opened as a dataframe just gives me a blank canvas.

I installed root again built the files and check if dataframe was ON and now I am a bit confused as to where the problem lies.

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 20, 2023, 4:55pm UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/10 "2023-12-20T16:55:56Z")

</div>

Hello,

@vpadulan would it be possible for you to look into this issue? Having RDataFrame’s functionality will make my job much easier. Thanks

Cheers  
AG

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [December 22, 2023, 11:06am UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/11 "2023-12-22T11:06:06Z")

</div>

Dear @amishgog ,

I cannot really tell from the description you are giving in your posts whether there is a problem with RDataFrame, TCanvas or the ROOT installation. I see that RDataFrame was added to the conversation only later on and it was not part of the initial problem. Would you maybe have a reproducible example (or at least some snippet of code) with a clear error to show us?  
Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 22, 2023, 12:42pm UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/12 "2023-12-22T12:42:36Z")

</div>

Hello Vincenzo,

Apologies for not being more descriptive about the problem. I am attaching a simple code which opens my root file as a dataframe and makes distribution for the specified branch.

```auto
#include <ROOT/RDataFrame.hxx>

void rdataframe()
{

	ROOT::RDataFrame rdf("Xbc2JpsiPhip/DecayTree", "my_file.root");

	auto h = rdf.Histo1D("Xi_bcplus_M");
	
	h->Draw();
	
}

```

This is code should print the spectrum of Xibc plus however it just prints a blank canvas.

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [December 22, 2023, 6:06pm UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/13 "2023-12-22T18:06:18Z")

</div>

Dear @amishgog ,

Thanks for the clarification, now I see the issue. RDataFrame follows a different approach than previous ROOT libraries w.r.t. ownership of objects. In particular, there is no global management for the histogram returned by `Histo1D`. In practice, this means that the histogram doesn’t survive the end of the scope of the function as you would have expected previously. In order to see it in the canvas in the particular case you present, it is sufficient to substitute `Draw` with `DrawClone`, so the canvas will own a copy of the histogram.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 23, 2023, 2:27am UTC](https://root-forum.cern.ch/t/settitle-method-not-working/57528/14 "2023-12-23T02:27:32Z")

</div>

Hi Vincenzo

Thank you so much for your help on this, substituting `Draw` with `DrawClone` worked. So that means that when we are writing a script DrawClone is the method to use to draw a histogram and when using the ROOT prompt Draw is sufficient.

Cheers  
AG
