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