go-hep.org/x/hep@v0.38.1/groot/riofs/gendata/gen-tformula.go (about)

     1  // Copyright ©2022 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"flag"
    11  	"log"
    12  
    13  	"go-hep.org/x/hep/groot/internal/rtests"
    14  )
    15  
    16  var (
    17  	root = flag.String("f", "test-tformula.root", "output ROOT file")
    18  )
    19  
    20  func main() {
    21  	flag.Parse()
    22  
    23  	out, err := rtests.RunCxxROOT("gentformula", []byte(script), *root)
    24  	if err != nil {
    25  		log.Fatalf("could not run ROOT macro:\noutput:\n%v\nerror: %+v", string(out), err)
    26  	}
    27  }
    28  
    29  const script = `
    30  #include "TFormula.h"
    31  #include "TF1.h"
    32  #include "TF1Convolution.h"
    33  
    34  double func2(double *x, double *par) {
    35  	return par[0] + par[1]*x[0];
    36  };
    37  
    38  void gentformula(const char* fname) {
    39  	auto f = TFile::Open(fname, "RECREATE");
    40  
    41  	auto f1 = new TF1("func1", "[0] + [1]*x", 0, 10);
    42  	f1->SetParNames("p0", "p1");
    43  	f1->SetParameters(1, 2);
    44  	f1->SetParameter(0, 10);
    45  	f1->SetParameter(1, 20);
    46  	f1->SetChisquare(0.2);
    47  	f1->SetNDF(2);
    48  	f1->SetNumberFitPoints(101);
    49  	f1->SetNormalized(true);
    50  
    51  	auto f2 = new TF1("func2", func2, 0, 10, 2);
    52  	f2->SetParNames("p0", "p1");
    53  	f2->SetParameters(1, 2);
    54  	f2->SetParameter(0, 10);
    55  	f2->SetParameter(1, 20);
    56  	f2->SetChisquare(0.2);
    57  	f2->SetNDF(2);
    58  	f2->SetNumberFitPoints(101);
    59  
    60  	auto conv = new TF1Convolution("expo", "gaus", -1, 6, true);
    61  	conv->SetRange(-1., 6.);
    62  	conv->SetNofPointsFFT(1000);
    63  	auto f3 = new TF1("func3", *conv, 0, 5, conv->GetNpar());
    64  	f3->SetParNames("p0", "p1", "p2", "p3");
    65  	f3->SetParameters(1., -0.3, 0., 1.);
    66  	f3->SetChisquare(0.2);
    67  
    68  	auto norm = new TF1NormSum(f1, f2, 10, 20); 
    69  	auto f4 = new TF1("func4", *norm, 0, 5, norm->GetNpar());
    70  	f4->SetChisquare(0.2);
    71  
    72  	f->WriteTObject(f1);
    73  	f->WriteTObject(f2);
    74  	f->WriteTObject(f3);
    75  	f->WriteTObject(f4);
    76  	f->WriteTObject(conv, "fconv");
    77  	f->WriteTObject(norm, "fnorm");
    78  
    79  	f->Write();
    80  	f->Close();
    81  
    82  	exit(0);
    83  }
    84  `