Unable to fit a custom function

Dear ROOT experts,

I’m trying to fit a custom function:

f(x) = c0 + c2.x + c1.exp(c3.x)

from a binned histogram with weights (errors).

I’ve tried using TGraphError, but the fit fails.
(As a new user, I’m unable to post pictures or files, so I copied the code below test1.c and test2.c).

Then I tried to do the fit with the functions that I had been successfully using to perform weighted affine fitting, but here again I fail to find what I have to modify. My initial set of parameters is quite close to data.
Using ROOT::Math::MinimizerOptions::SetStrategy(0), the fit result says it has converged, however the result doesn’t seem fine to me.

Would someone please point out what I’m missing ?

Thanks for your help.


_ROOT Version: 6.26.10
_Platform: win64
_Compiler: MSVC 19.32.31332.0


  • test1.c:
#include "TH1.h"
#include "TF1.h"
#include "Fit/BinData.h"
#include "Fit/Fitter.h"
#include "Math/WrappedMultiTF1.h"
#include "TGraph.h"


double X[329] = {
  -9.1100, -9.0900, -9.0700, -9.0500, -9.0300, -9.0100, -8.9900
, -8.9700, -8.9500, -8.9300, -8.9100, -8.8900, -8.8700, -8.8500
, -8.8300, -8.8100, -8.7900, -8.7700, -8.7500, -8.7300, -8.7100
, -8.6900, -8.6700, -8.6500, -8.6300, -8.6100, -8.5900, -8.5700
, -8.5500, -8.5300, -8.5100, -8.4900, -8.4700, -8.4500, -8.4300
, -8.4100, -8.3900, -8.3700, -8.3500, -8.3300, -8.3100, -8.2900
, -8.2700, -8.2500, -8.2300, -8.2100, -8.1900, -8.1700, -8.1500
, -8.1300, -8.1100, -8.0900, -8.0700, -8.0500, -8.0300, -8.0100
, -7.9900, -7.9700, -7.9500, -7.9300, -7.9100, -7.8900, -7.8700
, -7.8500, -7.8300, -7.8100, -7.7900, -7.7700, -7.7500, -7.7300
, -7.7100, -7.6900, -7.6700, -7.6500, -7.6300, -7.6100, -7.5900
, -7.5700, -7.5500, -7.5300, -7.5100, -7.4900, -7.4700, -7.4500
, -7.4300, -7.4100, -7.3900, -7.3700, -7.3500, -7.3300, -7.3100
, -7.2900, -7.2700, -7.2500, -7.2300, -7.2100, -7.1900, -7.1700
, -7.1500, -7.1300, -7.1100, -7.0900, -7.0700, -7.0500, -7.0300
, -7.0100, -6.9900, -6.9700, -6.9500, -6.9300, -6.9100, -6.8900
, -6.8700, -6.8500, -6.8300, -6.8100, -6.7900, -6.7700, -6.7500
, -6.7300, -6.7100, -6.6900, -6.6700, -6.6500, -6.6300, -6.6100
, -6.5900, -6.5700, -6.5500, -6.5300, -6.5100, -6.4900, -6.4700
, -6.4500, -6.4300, -6.4100, -6.3900, -6.3700, -6.3500, -6.3300
, -6.3100, -6.2900, -6.2700, -6.2500, -6.2300, -6.2100, -6.1900
, -6.1700, -6.1500, -6.1300, -6.1100, -6.0900, -6.0700, -6.0500
, -6.0300, -6.0100, -5.9900, -5.9700, -5.9500, -5.9300, -5.9100
, -5.8900, -5.8700, -5.8500, -5.8300, -5.8100, -5.7900, -5.7700
, -5.7500, -5.7300, -5.7100, -5.6900, -5.6700, -5.6500, -5.6300
, -5.6100, -5.5900, -5.5700, -5.5500, -5.5300, -5.5100, -5.4900
, -5.4700, -5.4500, -5.4300, -5.4100, -5.3900, -5.3700, -5.3500
, -5.3300, -5.3100, -5.2900, -5.2700, -5.2500, -5.2300, -5.2100
, -5.1900, -5.1700, -5.1500, -5.1300, -5.1100, -5.0900, -5.0700
, -5.0500, -5.0300, -5.0100, -4.9900, -4.9700, -4.9500, -4.9300
, -4.9100, -4.8900, -4.8700, -4.8500, -4.8300, -4.8100, -4.7900
, -4.7700, -4.7500, -4.7300, -4.7100, -4.6900, -4.6700, -4.6500
, -4.6300, -4.6100, -4.5900, -4.5700, -4.5500, -4.5300, -4.5100
, -4.4900, -4.4700, -4.4500, -4.4300, -4.4100, -4.3900, -4.3700
, -4.3500, -4.3300, -4.3100, -4.2900, -4.2700, -4.2500, -4.2300
, -4.2100, -4.1900, -4.1700, -4.1500, -4.1300, -4.1100, -4.0900
, -4.0700, -4.0500, -4.0300, -4.0100, -3.9900, -3.9700, -3.9500
, -3.9300, -3.9100, -3.8900, -3.8700, -3.8500, -3.8300, -3.8100
, -3.7900, -3.7700, -3.7500, -3.7300, -3.7100, -3.6900, -3.6700
, -3.6500, -3.6300, -3.6100, -3.5900, -3.5700, -3.5500, -3.5300
, -3.5100, -3.4900, -3.4700, -3.4500, -3.4300, -3.4100, -3.3900
, -3.3700, -3.3500, -3.3300, -3.3100, -3.2900, -3.2700, -3.2500
, -3.2300, -3.2100, -3.1900, -3.1700, -3.1500, -3.1300, -3.1100
, -3.0900, -3.0700, -3.0500, -3.0300, -3.0100, -2.9900, -2.9700
, -2.9500, -2.9300, -2.9100, -2.8900, -2.8700, -2.8500, -2.8300
, -2.8100, -2.7900, -2.7700, -2.7500, -2.7300, -2.7100, -2.6900
, -2.6700, -2.6500, -2.6300, -2.6100, -2.5900, -2.5700, -2.5500
};

double Y[329] = {
 -10.313923144440531,  -9.861938020697474,  -9.551783092393634,  -9.620775963880586,  -9.369461535599680, 
 -9.774926643707845,  -8.892537463509370,  -9.264101019941855,  -9.369461535599680,  -9.124339077566695, 
 -9.369461535599680,  -9.369461535599680,  -9.369461535599680,  -9.001736755474363,  -9.215310855772422,  
 -8.927628783320641,  -8.892537463509370,  -9.264101019941855,  -9.315394314329405,  -8.794097390696118,  
 -8.825846089010700,  -8.763325732029365,  -8.892537463509370,  -9.426619949439630,  -8.825846089010700,  
 -8.622247133769459,  -8.570953839381909,  -8.596271647366200,  -8.704485232006432,  -8.388632282587954,  
 -8.676314355039736,  -8.409685691785786,  -8.388632282587954,  -8.388632282587954,  -8.546261226791538,  
 -8.199390282949425,  -8.216782025661296,  -8.085446023600207,  -8.199390282949425,  -8.270849246931570,  
 -7.997153416454529,  -8.040325588319739,  -8.085446023600207,  -7.853114046231592,  -8.216782025661296,  
 -7.969373852347454,  -8.070178551469420,  -7.929099953209514,  -7.782496479017639,  -7.587004459033939,  
 -7.840992685699248,  -7.903124466806253,  -7.793925174841261,  -7.771196923763705,  -7.674865814825274,  
 -7.455812248762589,  -7.439551727890808,  -7.369484165274091,  -7.400020889134173,  -7.384635970294694,  
 -7.296988663235938,  -7.262621019731730,  -7.384635970294694,  -7.283099551075271,  -7.283099551075271,  
 -7.255886987550386,  -7.159966865671647,  -7.269400706717109,  -6.946627314454058,  -7.129934578572771,  
 -6.917499041531034,  -6.936823314357437,  -6.903247018823833,  -6.966528468771353,  -6.986833734932098,  
 -6.706873708574228,  -6.796001488470235,  -6.839298294223560,  -6.610859055334640,  -6.657714472616144,  
 -6.610859055334640,  -6.691488789734748,  -6.589952370515325,  -6.513630102955692,  -6.646705187107775,  
 -6.542805592089623,  -6.365430459230994,  -6.324939097876258,  -6.338254873852030,  -6.327588106047834,  
 -6.226747071697043,  -6.280947528594723,  -6.196048084808237,  -6.115647659370199,  -6.137340483981459,  
 -6.025422567777474,  -5.986956286949678,  -5.983189804154200,  -5.983189804154200,  -5.912444083839835,  
 -5.874638894498120,  -5.874638894498120,  -5.851304502436347,  -5.779788731569192,  -5.697389199802125,  
 -5.721693469728176,  -5.654535372547643,  -5.659962794283008,  -5.688950331156261,  -5.581491178841513,  
 -5.483326301123662,  -5.513421164922471,  -5.473116342890764,  -5.438725821239380,  -5.363138961873133,  
 -5.323490557661795,  -5.343118273142236,  -5.283485223048096,  -5.269576793001964,  -5.176445445870550,  
 -5.173931829130234,  -5.159806126866585,  -5.085874973739052,  -5.066899072280045,  -5.088176470727331,  
 -4.985353734721974,  -4.929428081651443,  -4.913178130319307,  -4.907392193252262,  -4.838057499851198,  
 -4.809753723688346,  -4.790179120563717,  -4.742856068891524,  -4.696113625081479,  -4.672016073502419,  
 -4.677604099219384,  -4.588472370305201,  -4.575049349973060,  -4.569547272819078,  -4.538484680877896,  
 -4.467484369382808,  -4.409340112624704,  -4.393877879562344,  -4.406228480398009,  -4.326394905868356,  
 -4.274008627076928,  -4.290129710217190,  -4.240549500579052,  -4.178976873854859,  -4.164234592117656,  
 -4.171578566373414,  -4.103323067415879,  -4.053249679175093,  -3.988029262341137,  -3.986241495296214,  
 -3.938411706597967,  -3.925601863760986,  -3.893462991814191,  -3.841797673612826,  -3.842460437361819,  
 -3.805366931987913,  -3.768778759689302,  -3.729921329671833,  -3.676852361541605,  -3.665308759143284,  
 -3.600793377851371,  -3.574079460535690,  -3.561819671411920,  -3.520136755652821,  -3.484592834757517,  
 -3.452810337570368,  -3.420701540949736,  -3.397491329979498,  -3.382451338841079,  -3.343461378614151,  
 -3.305030540644876,  -3.286608630400755,  -3.246968726085294,  -3.216610644571344,  -3.154853437177489,  
 -3.144793101373987,  -3.102577292612118,  -3.074093038387895,  -3.057121948684153,  -3.007295872819874,  
 -2.991884608899349,  -2.952729253087354,  -2.925683635793376,  -2.910078865736984,  -2.873440977739303,  
 -2.843210727280864,  -2.805214297675494,  -2.771785430215086,  -2.742890964215371,  -2.704344149419553,  
 -2.681868146394678,  -2.665115265741983,  -2.612077052179671,  -2.602566023603904,  -2.563246916728062,  
 -2.536369938704988,  -2.500447084933974,  -2.465546956003481,  -2.447693780419022,  -2.428755156477858,  
 -2.380177474213063,  -2.351159214272417,  -2.341210853375447,  -2.299587407141108,  -2.272464256979015,  
 -2.262036061488975,  -2.218760078007154,  -2.182770656614001,  -2.148965784824985,  -2.130007298748988,  
 -2.095636691759366,  -2.074367403924615,  -2.042959372719680,  -2.015419153989124,  -1.998881108906736,  
 -1.969961520664345,  -1.944666646385085,  -1.918897586998502,  -1.878559971271927,  -1.843791611920057,
 -1.829932241852074,  -1.803898442502907,  -1.777487706801899,  -1.748402480914340,  -1.713308344885222,  
 -1.702381274275035,  -1.670172824137108,  -1.645702512783021,  -1.618344704766146,  -1.591389657653339,  
 -1.569913156343789,  -1.541799846969211,  -1.518968354728540,  -1.491011518086415,  -1.475205616368585,  
 -1.444223685957782,  -1.421409914216723,  -1.399220214647226,  -1.373705628544028,  -1.340263974232605,  
 -1.319803910492577,  -1.294795164432434,  -1.274659221714550,  -1.249054556432170,  -1.230491247147874,  
 -1.205837006503105,  -1.181513426380198,  -1.156998618623875,  -1.132893891096297,  -1.126412834964249,  
 -1.094538084962830,  -1.068160472950146,  -1.049838911740137,  -1.036257958078095,  -1.007441769058385,  
 -0.992118068189074,  -0.967803907299347,  -0.951941360667509,  -0.930854705437012,  -0.916202603479534,  
 -0.897149491856160,  -0.882956672988893,  -0.856532980386833,  -0.847126033002528,  -0.836329380381941,  
 -0.808676854923248,  -0.796413248429456,  -0.785378423088207,  -0.776068909868604,  -0.758251930226274,  
 -0.741273729908524,  -0.735424020584331,  -0.725802129028279,  -0.712332503885926,  -0.703120634557674,  
 -0.691766447529414,  -0.689412804678507,  -0.678044298736275,  -0.674010589012952,  -0.672069408927882,  
 -0.667514562462868,  -0.667228263703542,  -0.656464015756072,  -0.655249764129009,  -0.659180874289131,  
 -0.655003441856125,  -0.656427475397242,  -0.659886451585986,  -0.661087994474738,  -0.671067785843910,  
 -0.668956537108436,  -0.678539151807693,  -0.684195974729453,  -0.696129166538607,  -0.702756980790329,  
 -0.720840551463452,  -0.726869934789109,  -0.738701591978517,  -0.749501188378216,  -0.761311058836361,  
 -0.774782749291670,  -0.798832901390536,  -0.814020326166952,  -0.833588351162675,  -0.847900120999691,  
 -0.865387356676596,  -0.886273349486128,  -0.904041285747054,  -0.928129989876270,  -0.951732696180833,  
 -0.974661992397510,  -1.002161433758060,  -1.026053109955217,  -1.060413819920218,  -1.072859599821508,  
 -1.112075879869244,  -1.136135480544120,  -1.161802253238448,  -1.193069938965885,  -1.223026114237874,  
 -1.264472777279751,  -1.289086529949630,  -1.323143464178061,  -1.349318910824085 
};

double W[329] = {
   0.000033168061621,   0.000052121239689,   0.000071074417758,   0.000066336123241,   0.000085289301310,  
   0.000056859534207,   0.000137410541000,   0.000094765890344,   0.000085289301310,   0.000108980773896,   
   0.000085289301310,   0.000085289301310,   0.000085289301310,   0.000123195657448,   0.000099504184862,   
   0.000132672246482,   0.000137410541000,   0.000094765890344,   0.000090027595827,   0.000151625424551,   
   0.000146887130034,   0.000156363719068,   0.000137410541000,   0.000080551006793,   0.000146887130034,   
   0.000180055191655,   0.000189531780689,   0.000184793486172,   0.000165840308103,   0.000227438136827,   
   0.000170578602620,   0.000222699842310,   0.000227438136827,   0.000227438136827,   0.000194270075206,   
   0.000274821081999,   0.000270082787482,   0.000307989143620,   0.000274821081999,   0.000255867903930,   
   0.000336418910723,   0.000322204027171,   0.000307989143620,   0.000388540150412,   0.000270082787482,   
   0.000345895499757,   0.000312727438137,   0.000360110383309,   0.000416969917516,   0.000506997513343,   
   0.000393278444930,   0.000369586972344,   0.000412231622999,   0.000421708212033,   0.000464352862688,   
   0.000578071931101,   0.000587548520136,   0.000630193170791,   0.000611239992722,   0.000620716581756,   
   0.000677576115963,   0.000701267588549,   0.000620716581756,   0.000687052704998,   0.000687052704998,   
   0.000706005883066,   0.000777080300825,   0.000696529294032,   0.000961873786997,   0.000800771773411,   
   0.000990303554100,   0.000971350376031,   0.001004518437652,   0.000942920608928,   0.000923967430859,   
   0.001222479985444,   0.001118237506065,   0.001070854560893,   0.001345675642892,   0.001284077814168,   
   0.001345675642892,   0.001241433163513,   0.001374105409995,   0.001483086183891,   0.001298292697720,   
   0.001440441533236,   0.001720000909753,   0.001791075327511,   0.001767383854925,   0.001786337032994,   
   0.001975868813683,   0.001871626334304,   0.002037466642407,   0.002208045245027,   0.002160662299854,   
   0.002416530203785,   0.002511296094129,   0.002520772683164,   0.002520772683164,   0.002705566169335,   
   0.002809808648714,   0.002809808648714,   0.002876144771955,   0.003089368025231,   0.003354712518195,   
   0.003274161511402,   0.003501599648229,   0.003482646470160,   0.003383142285298,   0.003766944141194,   
   0.004155484291606,   0.004032288634158,   0.004198128942261,   0.004345016072295,   0.004686173277535,   
   0.004875705058224,   0.004780939167880,   0.005074713427948,   0.005145787845706,   0.005648047064532,   
   0.005662261948084,   0.005742812954876,   0.006183474344978,   0.006301931707909,   0.006169259461427,   
   0.006837358988355,   0.007230637433285,   0.007349094796216,   0.007391739446871,   0.007922428432800,   
   0.008149866569627,   0.008310968583212,   0.008713723617176,   0.009130693534692,   0.009353393377002,   
   0.009301272137312,   0.010168380033964,   0.010305790574964,   0.010362650109171,   0.010689592430859,   
   0.011476149320718,   0.012163202025716,   0.012352733806405,   0.012201108381854,   0.013215103408540,   
   0.013925847586124,   0.013703147743814,   0.014399677037846,   0.015314167879670,   0.015541606016497,   
   0.015427886948084,   0.016517694687045,   0.017365849405629,   0.018536208151383,   0.018569376213004,   
   0.019479128760311,   0.019730258369724,   0.020374666424066,   0.021454997573994,   0.021440782690442,   
   0.022251031052887,   0.023080232593402,   0.023994723435226,   0.025302492721980,   0.025596266982048,   
   0.027302053008249,   0.028041226952936,   0.028387122452693,   0.029595387554586,   0.030666242115479,   
   0.031656545669579,   0.032689493874334,   0.033457097586124,   0.033964095099467,   0.035314509036876,   
   0.036698091035906,   0.037380405446386,   0.038891921397381,   0.040090709910239,   0.042644650655023,   
   0.043075835456090,   0.044933246906842,   0.046231539604562,   0.047022834788938,   0.049425150109171,   
   0.050192753820962,   0.052197052401748,   0.053628017345950,   0.054471433770016,   0.056504162117905,   
   0.058238377911209,   0.060493806101408,   0.062550225921884,   0.064383945900050,   0.066914195172248,   
   0.068435187712277,   0.069591331574480,   0.073381967188260,   0.074083234776809,   0.077054145439109,   
   0.079153209910239,   0.082048307860264,   0.084962358988357,   0.086492828117421,   0.088146492903932,   
   0.092534153626882,   0.095258672974286,   0.096211070172249,   0.100300218340613,   0.103057905749638,   
   0.104138236899566,   0.108743859170308,   0.112728764859294,   0.116604689774384,   0.118836426491997,   
   0.122991910783603,   0.125635879124214,   0.129644476285786,   0.133264533296946,   0.135486793425524,   
   0.139462222525476,   0.143034896591464,   0.146768672671037,   0.152809998180498,   0.158216392224652,   
   0.160424437469678,   0.164655734473560,   0.169062348374579,   0.174051772501217,   0.180268414907816,   
   0.182249022016016,   0.188214534813201,   0.192877016618151,   0.198226551128097,   0.203642421761285,   
   0.208063250545856,   0.213995595281421,   0.218937636462887,   0.225144802280451,   0.228731691229990,   
   0.235929160601655,   0.241373461001946,   0.246789331635134,   0.253167076055318,   0.261776557193116,   
   0.267187689531786,   0.273953974102383,   0.279526208454640,   0.286775799065993,   0.292149025048526,   
   0.299441260310535,   0.306814046579337,   0.314428485868517,   0.322099784691904,   0.324194110868517,   
   0.334694171518687,   0.343640071567208,   0.349994124514806,   0.354779801977203,   0.365151928675408,   
   0.370790499150906,   0.379916454391080,   0.385990947962163,   0.394216627244065,   0.400035252911217,   
   0.407730243207190,   0.413558345463376,   0.424631739750130,   0.428645075206220,   0.433298080422134,   
   0.445447067564299,   0.450943489204279,   0.455947128214469,   0.460211593279971,   0.468484655507045,   
   0.476506588124707,   0.479302181889870,   0.483936233927715,   0.490498771834072,   0.495038057981573,   
   0.500690843340622,   0.501870678675411,   0.507608753335770,   0.509660434861729,   0.510650738415828,   
   0.512981979318303,   0.513128866448337,   0.518682147622524,   0.519312340793315,   0.517274874150909,   
   0.519440274745280,   0.518701100800593,   0.516910025473082,   0.516289308891326,   0.511162474223689,   
   0.512242805373616,   0.507357623726357,   0.504495693837954,   0.498511227862699,   0.495218113173227,   
   0.486343287542466,   0.483419759825338,   0.477733806404668,   0.472602233442514,   0.467053690562844,   
   0.460803880094624,   0.449853681465318,   0.443073182011169,   0.434487392345958,   0.428313394590014,   
   0.420888487081523,   0.412188978347898,   0.404929911147510,   0.395292220099475,   0.386071498968955,   
   0.377319868995641,   0.367085152838436,   0.358418812166432,   0.346312469674922,   0.342029051431351,   
   0.328875545851535,   0.321057359898115,   0.312921708212040,   0.303288755458522,   0.294338117115484,   
   0.282388138343043,   0.275522349587585,   0.266296890162548,   0.259416886523538
};

Double_t myfunc(Double_t *x, Double_t* params) {
    Double_t arg = params[0] + params[2]*(*x) - params[1]*exp(params[3]*(*x));
    return TMath::Exp(arg);
}


void test1() {
    double yErr[329];
    for (unsigned int i = 0; i < 329; ++i) {
        yErr[i] = 0.01 / sqrt(W[i]);
    }

    TF1 *f1 = new TF1("f1", myfunc, X[0], X[328], 4);
    double initParams[4] = { 6.781854336702532, 187.5966283934926, 1.846531711713894 , 1.495547402571554 };
    f1->SetParameters(initParams);

    TCanvas *c1 = new TCanvas();
    TGraphErrors *grr = new TGraphErrors(329, X, Y, 0, yErr);
    grr->Fit(f1, "WQRN"); // "initial pre-fit"
    grr->Fit(f1, "RM+rob"); 
    grr->Draw("ALP");
}
  • test2.c
#include "TH1.h"
#include "TF1.h"
#include "Fit/BinData.h"
#include "Fit/Fitter.h"
#include "Math/WrappedMultiTF1.h"
#include "TGraph.h"

[Same data as in test1.c, removed because of post size limitation]

Double_t myfunc(Double_t *x, Double_t* params) {
    Double_t arg = params[0] + params[2]*(*x) - params[1]*exp(params[3]*(*x));
    return TMath::Exp(arg);
}


void test2() {
    double yErr[329];
    for (unsigned int i = 0; i < 329; ++i) {
        yErr[i] = 0.01 / sqrt(W[i]);
    }

    TF1 *f1 = new TF1("f1", myfunc, X[0], X[328], 4);
    double initParams[4] = { 6.781854336702532, 187.5966283934926, 1.846531711713894 , 1.495547402571554 };
    f1->SetParameters(initParams);

    double initFunc[329];
    for (unsigned int i = 0; i < 329; ++i) {
        initFunc[i] = initParams[0] + initParams[2]*X[i] - exp(initParams[3]*X[i])*initParams[1];
    }


    ROOT::Fit::BinData hdata(329, X, Y, NULL, yErr);
    ROOT::Math::WrappedMultiTF1 fitFunction(*f1, f1->GetNdim());
    ROOT::Math::MinimizerOptions::SetDefaultPrintLevel(1);
    //ROOT::Math::MinimizerOptions::SetDefaultTolerance(0.1);
    //ROOT::Math::MinimizerOptions::SetDefaultPrecision(1E-8);
    //ROOT::Math::MinimizerOptions::SetDefaultMaxFunctionCalls(1000);

    ROOT::Math::MinimizerOptions opts;
    //opts.SetMinimizerAlgorithm("least_squares");
    opts.SetStrategy(0);    

    ROOT::Fit::Fitter fitter;
    fitter.SetFunction(fitFunction, f1->IsLinear());
    fitter.Config().SetMinimizerOptions(opts);
    fitter.Config().ParSettings(0).SetStepSize(1e-6);
    fitter.Config().ParSettings(1).SetStepSize(1e-6);
    fitter.Config().ParSettings(2).SetStepSize(1e-6);
    fitter.Config().ParSettings(3).SetStepSize(1e-7);
    fitter.Fit(hdata);
    fitter.Result().Print(std::cout);


    double outfit[329];
    for (unsigned int i=0; i < 329; ++i) {
        outfit[i] = fitter.Result().GetParams()[0] + fitter.Result().GetParams()[2]*X[i] - exp(fitter.Result().GetParams()[3]*X[i])*fitter.Result().GetParams()[1];
    }

    TCanvas *c1 = new TCanvas();

    TMultiGraph  *mg  = new TMultiGraph();
    TGraph *gr1 = new TGraph (329, X, Y);
    mg->Add(gr1);
    TGraph *gr2 = new TGraph (329, X, initFunc);
    gr2->SetLineColor(2);
    mg->Add(gr2);

    TGraph *gr3 = new TGraph (329, X, outfit);
    gr3->SetLineColor(3);
    mg->Add(gr3);

    mg->Draw("a");
}

Welcome to the ROOT forum.
I guess @moneta can help.

If anyone has a suggestion about this case, or can tell me why this topic got no attention or how I could improve my description, please share !

Sorry for the lack of attention.

I tried your examples. test1.C gives me:

Processing test1.C...
 FCN=824846 FROM HESSE     STATUS=FAILED         11 CALLS          80 TOTAL
                     EDM=2.58498e-16    STRATEGY= 1  ERROR MATRIX UNCERTAINTY 100.0 per cent
  EXT PARAMETER                APPROXIMATE        STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  p0          -7.53806e+04   3.01927e+05   3.01927e+03   3.57670e-06
   2  p1           9.00174e+02   1.41421e+00   1.41421e-02   9.00410e-01
   3  p2           4.08984e+00   1.41421e+00   1.41421e-02  -9.32398e-01
   4  p3          -1.24458e+03   3.02353e+07   3.02353e+05  -1.10715e-09

and test2.C gives me:

Processing test2.C...
 **********
 **    1 **SET PRINT           0
 **********
 **********
 **    2 **SET NOGRAD
 **********
 PARAMETER DEFINITIONS:
    NO.   NAME         VALUE      STEP SIZE      LIMITS
     1 p0           6.78185e+00  1.00000e-06     no limits
     2 p1           1.87597e+02  1.00000e-06     no limits
     3 p2           1.84653e+00  1.00000e-06     no limits
     4 p3           1.49555e+00  1.00000e-07     no limits
 **********
 **    3 **SET ERR           1
 **********
 **********
 **    4 **SET PRINT           0
 **********
 **********
 **    5 **SET STR           0
 **********
 **********
 **    6 **MIGRAD        1480        0.01
 **********
 MINUIT WARNING IN MIGRAD  
 ============== Negative diagonal element 2 in Error Matrix
 MINUIT WARNING IN MIGRAD  
 ============== 1 added to diagonal of error matrix
 MIGRAD MINIMIZATION HAS CONVERGED.
 FCN=824846 FROM MIGRAD    STATUS=CONVERGED     120 CALLS         121 TOTAL
                     EDM=0    STRATEGY= 0      NO ERROR MATRIX       
  EXT PARAMETER               CURRENT GUESS       STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  p0           5.10936e+00   1.00000e-06   1.56503e-01   0.00000e+00
   2  p1           4.90539e+13   1.00000e-06   4.90539e+13   0.00000e+00
   3  p2           3.43866e+00   1.00000e-06   1.06086e+00   0.00000e+00
   4  p3          -3.58699e+00   1.00000e-07  -4.22043e+00   0.00000e+00

****************************************
Minimizer is Minuit / Migrad
Chi2                      =       824846
NDf                       =          325
Edm                       =            0
NCalls                    =          121
p0                        =      5.10936   +/-   1e-06       
p1                        =  4.90539e+13   +/-   1e-06       
p2                        =      3.43866   +/-   1e-06       
p3                        =     -3.58699   +/-   1e-07       

It might be the initial parameters are too far from the real value. I let @moneta gives his advice.

I don’t think your “myfunc” can describe your data.
For example, your “Y” values are negative, but “return TMath::Exp(arg);” is always positive (try with “return arg;”)

1 Like

Thank you so much @Wile_E_Coyote , that was the root cause and now it works fine.