Plotting multiple TF1 on the same graph

Hi everyone,
I have a problem that seems very trivial but is proving difficult.
Basically, I have multiple root files and with a TF1 stored and I want to retrieve the TF1, scale it by another TF1 and plot all the scaled functions on a single plot.
To scale the functions, i have created and defined a function called Old_NewFracFunc to be used in my constructor which divides fNew by fOld. I did this because simply defining the scaled function using something like “f1/fOld” didn’t work.

But in my script whenever I reassign fNew, this seems to erase the memory of the previously defined fNew.
And as a result, I can’t plot all the functions on the same plot.

I’ve attached a copy of my simplified script. (I have two functions to scale here whereas I actually have about 20!)

Please help and I thank you in advance.

Have a nice day!
Ally
MyScript.C (2.5 KB)
outputFile0.root (3.37 KB)
outputFile1.root (3.37 KB)
outputFile2.root (3.36 KB)

note you have a mistake in your macro… a lonely / at line 40 …
I am looking at your question

Line:

TCanvas* c1 = new TCanvas(“c1”,"",900,900);

seems useless… you never use c1.

Your macro gives the attached plot. What is wrong ?


Hi,

Thank you for the quick response.
I’m sorry for the careless “”. c1 is there for another purpose in my original macro which i forgot to take out.
The problem is I can’t seem to plot the red line (which should be above the black one) as well as the black and blue on the same plot. If I comment out the rest of this script from 98 onwards, I get the red line above the black one as I wanted but once I redefine fNew in line 98, I can’t eem to put both the cyan and red line on the same plot using Draw(“SAME”). The red line seems to have changed so that it is identical to the cyan one and hence cannot be seen.

I apologize again I wasn’t being clear. Hope you can help!
Thanks!

Yes Red line and Cyan line are identical

The following version of your script shows the red line:

// Declare function used in function implementation:

TF1 * fOld  = NULL;
TF1 * fNew1 = NULL;
TF1 * fNew2 = NULL;

//To find the fraction of new/old with the modified correction values.

Double_t Old_NewFracFunc1(Double_t *x, Double_t *par)
{
   const Double_t xx =x[0];
   return fNew1->Eval(xx)/fOld->Eval(xx);
}

Double_t Old_NewFracFunc2(Double_t *x, Double_t *par)
{
   const Double_t xx =x[0];
   return fNew2->Eval(xx)/fOld->Eval(xx);
}

//Function to divide original JPSi function by itself to get a straight line for comparison
//Can't just use Old_NewFracFunc and reassign fNew, this changes all the other plots too??

Double_t Old_OldFracFunc(Double_t *x, Double_t *par)
{
   const Double_t xx =x[0];
   return fOld->Eval(xx)/fOld->Eval(xx);
}

void MyScript() {

   TCanvas* c0 = new TCanvas("c0","",900,900);

   //open file and get function
   TFile* file = TFile::Open("outputFile0.root");
   fOld        = (TF1*)file->Get("func");


   //Get the fraction of old/old (should be horizontal line)
   TF1* Old_OldFrac = new TF1("Old_OldFrac",Old_OldFracFunc,10,30,40); //This is the old/old function, should give a straight line
   Old_OldFrac->SetLineColor(kBlack);
   Old_OldFrac->GetYaxis()->SetRangeUser(0.9000,1.1);
   Old_OldFrac->Draw();

   //In my original script, I have 17 elements in this vector:
   vector <string> vec_RootFileNames;
   vec_RootFileNames.push_back("outputFile1.root");
   vec_RootFileNames.push_back("outputFile2.root");

   TFile* file1 = TFile::Open(vec_RootFileNames[0].c_str()); //open root file

   fNew1 = (TF1*)file1->Get("func"); //get function and assign it to fNew

   TF1 *f1Frac = new TF1("f1Frac",Old_NewFracFunc1,10,30,4); //construct new ratio function

   //plot
   f1Frac->SetLineColor(kRed);
   f1Frac->Draw("SAME");

   file1 = TFile::Open(vec_RootFileNames[1].c_str()); //open root file

   //Assign fNew again
   fNew2 = (TF1*)file1->Get("func");

   TF1*f2Frac = new TF1("f2Frac",Old_NewFracFunc2,10,30,4); //construct new ratio function

   //plot
   f2Frac->SetLineColor(kCyan);
   f2Frac->Draw("SAME");
}

Hello,
Thank you for your reply. I realised this but as I mentioned in my original question, I have about 20 plots in total that I need to scale. This would mean I need to create 20 functions each with a different “fNew” in order for this to work. Is there an easier way to get around this?
Thanks!

Here is an other which might be better for your case.

// Declare function used in function implementation:

TF1 * fOld  = NULL;
TF1 * fNew[2] = { NULL, NULL};

//To find the fraction of new/old with the modified correction values.

Double_t Old_NewFracFunc(Double_t *x, Double_t *par)
{
   const Double_t xx =x[0];
   Int_t i = (Int_t)par[0];
   return fNew[i]->Eval(xx)/fOld->Eval(xx);
}

//Function to divide original JPSi function by itself to get a straight line for comparison
//Can't just use Old_NewFracFunc and reassign fNew, this changes all the other plots too??

Double_t Old_OldFracFunc(Double_t *x, Double_t *par)
{
   const Double_t xx =x[0];
   return fOld->Eval(xx)/fOld->Eval(xx);
}

void MyScript() {

   TCanvas* c0 = new TCanvas("c0","",900,900);

   //open file and get function
   TFile* file = TFile::Open("outputFile0.root");
   fOld        = (TF1*)file->Get("func");

   //Get the fraction of old/old (should be horizontal line)
   TF1* Old_OldFrac = new TF1("Old_OldFrac",Old_OldFracFunc,10,30,40); //This is the old/old function, should give a straight line
   Old_OldFrac->SetLineColor(kBlack);
   Old_OldFrac->GetYaxis()->SetRangeUser(0.9000,1.1);
   Old_OldFrac->Draw();

   //In my original script, I have 17 elements in this vector:
   vector <string> vec_RootFileNames;
   vec_RootFileNames.push_back("outputFile1.root");
   vec_RootFileNames.push_back("outputFile2.root");

   TFile* file1 = TFile::Open(vec_RootFileNames[0].c_str()); //open root file
   Int_t i=1;
   fNew[i] = (TF1*)file1->Get("func"); //get function and assign it to fNew

   TF1 *f1Frac = new TF1("f1Frac",Old_NewFracFunc,10,30,4); //construct new ratio function
   f1Frac->SetParameter(0,i);

   //plot
   f1Frac->SetLineColor(kRed);
   f1Frac->Draw("SAME");

   file1 = TFile::Open(vec_RootFileNames[1].c_str()); //open root file

   //Assign fNew again
    i = 2;
   fNew[i] = (TF1*)file1->Get("func");

   TF1*f2Frac = new TF1("f2Frac",Old_NewFracFunc,10,30,4); //construct new ratio function
   f2Frac->SetParameter(0,i);

   //plot
   f2Frac->SetLineColor(kCyan);
   f2Frac->Draw("SAME");
}