# Plottiing a percentage in Y-axis instead of count

**URL:** <https://root-forum.cern.ch/t/plottiing-a-percentage-in-y-axis-instead-of-count/49158>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [March 13, 2022, 5:49pm UTC](https://root-forum.cern.ch/t/plottiing-a-percentage-in-y-axis-instead-of-count/49158 "2022-03-13T17:49:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/roy/32/30362_2.png) [@Roy](https://root-forum.cern.ch/u/Roy)\
**Post date:** [March 13, 2022, 5:49pm UTC](https://root-forum.cern.ch/t/plottiing-a-percentage-in-y-axis-instead-of-count/49158/1 "2022-03-13T17:49:34Z")

</div>

I am trying to create a percentage in Y-axis instead of count.  
planning to use for loop but how can I accomplish that;

```auto
    TH1F *hist = new TH1F("hist","Histogram",100,0,100);
    ifstream file;
    file.open("C07.txt", ios::in);
    double value;
    while(1)
    {
        file>>value;
        hist -> Fill(value);
        if (file.eof())break;

    }
    hist ->GetXaxis() ->SetTitle("data");
    hist ->GetYaxis()->SetTitle("Percentage");    
    

    TCanvas*c1 = new TCanvas();
    hist ->Draw();
    

}

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 13, 2022, 5:53pm UTC](https://root-forum.cern.ch/t/plottiing-a-percentage-in-y-axis-instead-of-count/49158/2 "2022-03-13T17:53:33Z")

</div>

Maybe you want (before drawing): `hist->Scale(100. / hist->Integral());`

---

<div class="post-metadata">

**Author:** ![Roy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/roy/32/30362_2.png) [@Roy](https://root-forum.cern.ch/u/Roy)\
**Post date:** [March 14, 2022, 4:34pm UTC](https://root-forum.cern.ch/t/plottiing-a-percentage-in-y-axis-instead-of-count/49158/4 "2022-03-14T16:34:05Z")

</div>

shows an error (no debug info)  
`cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)`

```cpp
void CurrentAnalysis()
{
        Float_t Threshold;
	TFile* file_input = new TFile("January_data.root", "read");
	TTree* currentAna = (TTree*)file_input->Get("voltage_current_ntuple");

	Float_t Curr;
         Int_t NumBins = 100;
	currentAna->SetBranchAddress("Current", &Curr);

	ULong64_t nEntries = currentAna->GetEntries();
	cout << "This tree contains " << nEntries << " events!" << endl;
	cout << "Loop over " << nEntries << " events!" << endl;
    
    TH1D* curr_vals = new TH1D("curr_vals", "",NumBins, 0, NumBins);
    curr_vals -> GetXaxis()-> SetTitle("Current [#muA] ");
    curr_vals -> GetYaxis()-> SetTitle("Frequency");
    curr_vals -> SetTitle("Current Distribution");
    for(Int_t f = 0; f < nEntries; f++)
    {
        currentAna->GetEntry(f);
        curr_vals->Fill(abs(Curr));
    }
   
    TCanvas* c1 = new TCanvas("c1");
    curr_vals->Draw();
    c1->SetLogy();
    c1->Update();
 
    
    TH1D* beyondThreshold = new TH1D("beyondThreshold", "",NumBins, 0, NumBins);
    beyondThreshold -> GetXaxis()-> SetTitle("Current [#muA] ");
    beyondThreshold -> GetYaxis()-> SetTitle("Percentage [%]");
    beyondThreshold->Sumw2(kTRUE);
    for(ULong64_t i = 0; i < NumBins; i++)
    {
        Threshold = i;
        Int_t count = 0;

        for(ULong64_t e = 0; e < nEntries; e++)
            {
             currentAna->GetEntry(e);
            if( abs(Curr) < Threshold ) continue;
            {
                count++;
            }
        }
        beyondThreshold->SetBinContent(i+1, count*100/nEntries);
        cout << Threshold << "\t \t" << count << endl;
    }
    TCanvas* c2 = new TCanvas("c2");
    //c2->SetLogy();
    beyondThreshold->Draw();
    beyondThreshold->SetTitle("Percentage of current beyond the threshold value.");
   // beyondThreshold->GetYaxis()->SetRangeUser(0, 0.5);
   // beyondThreshold->GetXaxis()->SetRangeUser(0, 5);
    c2->Update();
    
   
}

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 14, 2022, 4:59pm UTC](https://root-forum.cern.ch/t/plottiing-a-percentage-in-y-axis-instead-of-count/49158/5 "2022-03-14T16:59:01Z")

</div>

At least: `Double_t(count) * 100. / Double_t(nEntries)`
