ATLAS Offline Software
Loading...
Searching...
No Matches
graph_to_function.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4// utility to take a root file full of TGraph2DErrors and create a root file with TF2s.
5
6
7#include "TFile.h"
8#include "TGraph2DErrors.h"
9#include "TF2.h"
10#include "TFitResult.h"
11
12#include <iostream>
13#include <memory>
14#include <string>
15#include <vector>
16#include <cmath>
17
18using std::make_unique;
19using std::cout;
20using std::cerr;
21using std::endl;
22using std::string;
23using std::vector;
24
25int main(int argc, char** argv) {
26 if (argc !=3) {
27 cout << "Usage: " << argv[0] << " input.root output.root" << endl;
28 return 1;
29 }
30
31 auto fin = make_unique<TFile>(argv[1], "READ");
32 auto fout = make_unique<TFile>(argv[2], "RECREATE");
33 //fin and fout are necessarily non-null pointers
34
35 auto etaLegFunc = [](double* x, double* par){
36 double tanhEtaSq = std::tanh(x[0]);
37 tanhEtaSq = tanhEtaSq*tanhEtaSq;
38 // tanhEtaSq = x[0]*x[0]/6.25; // try just eta
39 // double lp0 = 1.
40 double lp2 = 1.5*tanhEtaSq - 0.5;// 2nd order legendre poly
41 double lp4 = 0.125*(35.*tanhEtaSq*tanhEtaSq - 30.*tanhEtaSq + 3.);
42 double lp6 = 0.0625*(231*tanhEtaSq*tanhEtaSq*tanhEtaSq - 315*tanhEtaSq*tanhEtaSq + 105*tanhEtaSq - 5);
43 return par[0]*(1. + par[2]*(lp2-1) + par[4]*(lp4-1) + par[6]*(lp6-1)) +
44 par[1]*(1. + par[3]*(lp2-1) + par[5]*(lp4-1) + par[7]*(lp6-1))/x[1];
45 };
46
47 // loop through histograms in infile
48 TIter next(fin->GetListOfKeys());
49 while (TObject* obj = next()) {
50 string objName = obj->GetName();
51 cout << "Fitting " << objName << endl;
52 TGraph2DErrors* gr = nullptr;
53 fin->GetObject(obj->GetName(), gr);
54 if (gr == nullptr) {
55 cerr << "Could not retrieve " << obj->GetName()
56 << " as a TGraph2DErrors." << endl;
57 continue;
58 }
59 TF2* func = new TF2(objName.data(), etaLegFunc, -2.5, 2.5, 0.1, 100.0, 8);
60 // func->SetNpx(200);
61 func->SetNpy(200);
62 func->SetParameters(0.02, 0.1, 0.1, 0.1, 0., 0., 0., 0.);
63 func->SetParLimits(0, 0., 5.);
64 func->SetParLimits(1, 0., 5.);
65 double ext = 0.7;
66 func->SetParLimits(2, -ext, ext);
67 func->SetParLimits(3, -ext, ext);
68 func->SetParLimits(4, -ext, ext);
69 func->SetParLimits(5, -ext, ext);
70 func->SetParLimits(6, -ext, ext);
71 func->SetParLimits(7, -ext, ext);
72
73 // func->FixParameter(4, 0.);
74 // func->FixParameter(5, 0.);
75 func->FixParameter(6, 0.);
76 func->FixParameter(7, 0.);
77 auto result = gr->Fit(func, "S"); // "R" to use function range
78 cout << "ndf, g.N = " << result->Ndf() << ", " << gr->GetN() << endl;
79 func->Write();
80 }
81
82
83 return 0;
84}
#define gr
#define x
int main()
Definition hello.cxx:18
static TFile * fout
Definition listroot.cxx:40