Getting fitting parameter inside a loop

Hello,
I have tried to get a width of many histograms, each one from a loop. My code is:

TH1D *dEdx_mu_width = new TH1D("dedx mu width","",70,0,1);
TF1 *landau_mu = new TF1("Landau mu","[0]*Landau(x,[1],[2])",0,2);	

Double_t norm = 1;
landau_mu->SetParName(0,"height");
landau_mu->SetParName(1,"peak");
landau_mu->SetParName(2,"width");
landau_mu->SetParameters(1,2,0.1);
   
nentries = 1000;

for(int j=0; j<nentries; j++){ //Read each event
		//myReader->GetEntry(j); //Read branches from the event

        for(int i=0; i<20; i++) landau_mu->GetRandom();
                
		dEdx_mu->Scale(norm/dEdx_mu->Integral(), "width");
		landau_mu->SetParameters(1,2,0.1);
		dEdx_mu->Fit(landau_mu,"Q");
		
		cout << landau_mu->GetParameter("width")) << endl;
}

The problem is that my “width” output remains my guess (0.1). Actually, I’m reading a ROOT file and looping over events. How could I get the “width” value for each loop?


ROOT Version: 5.34/34


Hi,

if I understand correctly, this is a mock of the real code. What are you actually running on what file? Can you share that?

Cheers,
D

Hi dpiparo, sure, my code:

TFile *my_file = new TFile("anatree50000.root","READ");
TTree *myReader = (TTree*) my_file->Get("analysistree/anatree");

TH1D *dEdx_mu = new TH1D("dedx of a track muon","",70,0,1);
TF1 *landau_mu = new TF1("Landau","[0]*Landau(x,[1],[2])",0,2);

Short_t ntrkhits_pmalgtrackmaker[79][3], ntracks_pmalgtrackmaker;
Float_t trkdedx_pmalgtrackmaker[79][3][2000];
Int_t trkpdgtruth_pmalgtrackmaker[79][3];

Double_t norm = 1;
landau_mu->SetParName(0,"height");
landau_mu->SetParName(1,"peak");
landau_mu->SetParName(2,"width");
landau_mu->SetParameters(1,2,0.1);

//hits from a track
myReader->SetBranchAddress("ntrkhits_pmalgtrackmaker",&ntrkhits_pmalgtrackmaker);
//number of tracks
myReader->SetBranchAddress("ntracks_pmalgtrackmaker",&ntracks_pmalgtrackmaker);
//dedx of the hit
myReader->SetBranchAddress("trkdedx_pmalgtrackmaker",&trkdedx_pmalgtrackmaker);
//pdg number of the track
myReader->SetBranchAddress("trkpdgtruth_pmalgtrackmaker",&trkpdgtruth_pmalgtrackmaker);

//number of entries of the tree
Long64_t nentries = myReader->GetEntriesFast();

myReader->GetEntry(0);

for(int j=0; j<nentries; j++){
		myReader->GetEntry(j); 
        // Loop over tracks of the event
		for(int track=0;track<ntracks_pmalgtrackmaker;track++){ 
        // Loop over hits of the track
                for(int trkhit=1;trkhit<ntrkhits_pmalgtrackmaker[track][2];trkhit++){ 	
        // Fill histogram with dEdx of the muon track-hit of the event
                       if(trkpdgtruth_pmalgtrackmaker[track][2] == 13) dEdx_mu->Fill(trkdedx_pmalgtrackmaker[track][2][trkhit]);                      		               
                }//end trkhits
         }//end tracks	
	     //Normalizing the histogram 
         dEdx_mu->Scale(norm/dEdx_mu->Integral(), "width");
         //First guess for the fit
         landau_mu->SetParameters(1,2,0.1);
         //Fit
		 dEdx_mu->Fit(landau_mu,"Q");

		 cout << landau_mu->GetParameter("height") << endl;
         cout << landau_mu->GetParameter("peak") << endl;
         cout << landau_mu->GetParameter("width") << endl;
}//end entries

I want to fill an histogram with the “width” parameter, but the I only get my guess values. Unfortunatelly I don’t have permission to share the root file and its size makes it impossible. But it’s a TTree with 50000 entries:

Hi,

thanks. It is hard to debug without even 10 entries of that tree.
My advice would be to set nentries to 1 in order to simplify the problem and check if the fit actually converges or if there are issues with it, for example removing the “q” parameter.

Cheers,
D

1 Like

Hi,

Based on your description, TFitResult seems to be what you want.

https://root.cern.ch/root/htmldoc/guides/users-guide/FittingHistograms.html

Cheers,
Afiq

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.