TH1D* MakeHistoFromFile(TString filename, TString hname, TString title) { ifstream inp; double x,x1,x2,xp,y,bw; inp.open(filename.Data()); int i = 0; while (inp >> x >> y) { if (i == 0) { x1 = x; xp = x; } else { bw = x - xp; xp = x; } i++; } x2 = x; inp.close(); printf("Computed bin width = %g\n", bw); if (TMath::Abs(1-bw) < 0.001 ) bw = 1.; printf("Bin width = %g \n", bw); int nb = i+1; // Number of bins auto h = new TH1D(hname.Data(),title.Data(),nb,x1-bw/2.,x2+bw/2.); h->GetXaxis()->SetTitle("Energy (keV)"); h->GetYaxis()->SetTitle("Count"); inp.open(filename.Data()); while (inp >> x >> y) { h->Fill(x,y); } inp.close(); return h; } void Devices() { auto C = new TCanvas("C","C",600,600); C->Divide(1,2); auto h1 = MakeHistoFromFile("Device-1-005.txt","h1", "Device 1"); C->cd(1); h1->Draw("hist"); h1->SetFillColor(kRed); auto h2 = MakeHistoFromFile("Device-2-005.txt","h2", "Device 2"); C->cd(2); h2->Draw("hist"); h2->SetFillColor(kBlue); h1->Add(h2); }