void problem(){ gStyle->SetOptStat(1111); gStyle->SetOptFit(1111); double ta = 32; double tb = 40; string OutFileName = "fitted_problem.root"; string InFileName = "problem.root"; cout << "input file name: " << InFileName << endl; cout << "output file name: " << OutFileName << endl; // open and create files cout << endl; cout << "######## reading files ########" << endl; TFile* InFile = new TFile(InFileName.c_str(),"OPEN"); TTree* InTree = (TTree*) InFile->Get("tree"); char muonID[40]; InTree->SetBranchAddress("muonID", &muonID); TFile* OutFile = new TFile(OutFileName.c_str(),"RECREATE"); OutFile->cd(); double chi2_ndf_init; double chi2_ndf; TTree* OutTree = new TTree("tree","a tree"); OutTree->Branch("muonID", &muonID, "muonID/C"); OutTree->Branch("chi2_ndf_init", &chi2_ndf_init, "chi2_ndf_init/D"); OutTree->Branch("chi2_ndf", &chi2_ndf, "chi2_ndf/D"); int N = 5; cout << endl; cout << "######## start looping ########" << endl; cout << " index muonID fit chi2/ndf (32,40) us" << endl; for(int i=0; iGetEntry(i); InFile->cd(); TGraph* gr_x = (TGraph*) InFile->Get(Form("gr_x_%s",muonID)); TGraph* gr = (TGraph*) gr_x->Clone(); // TimeBounds st_end = GetStartEndTimePts(gr, ta, tb); // int st = st_end.start_bin; // int end = st_end.end_bin; // vector ans = GetMinMaxMeanFromGraphRange(gr,st, end); // double min_val = ans[0]; // double max_val = ans[1]; // double xe = ans[2]; // double ampl_A = abs(max_val - min_val)/2; TF1* fit_func = new TF1(Form("fit_func_%s",muonID), "[0] + ([1]+[2]*x + [3]*x*x ) * cos([4]*x + [5]) + ([6]+[7]*x + [8]*x*x ) * cos([9]*x + [10])",ta, tb); fit_func->SetNpx(500000); fit_func->SetLineColor(2); fit_func->SetParameters(1, 2, 0.0, 0.0, 42, 0, 0, 0, 0, 39.8, 0); fit_func->SetParNames("xe","A","A1","A2","#omega_{c}","#phi_{c}","B","B1","B2","#omega_{x}","#phi_{x}"); OutFile->cd(); gr->Fit(fit_func,"REMQ"); OutFile->cd(); OutTree->Fill(); gr->Write(); delete gr; delete gr_x; //delete fit_func; } cout << endl; cout << "looping ended." << endl; cout << endl; cout << "######## finalizing ########" << endl; OutFile->cd(); OutTree->Write(); OutFile->Close(); InFile->Close(); cout << "Ciao" << endl; exit(0); }