void addhisto(){ TH2* hist = new TH2F("herr_dipAngle_stt","",200,-0.05,0.05,10,0,10); std::ifstream listroot("reco.txt"); string filename; if(listroot.is_open()){ while(listroot){ std::getline(listroot,filename); TFile* f1 = new TFile(filename.c_str()); TH2* histo = (TH2F*)f1->Get("herr_dipAngle_stt"); hist->Add(histo); } } TFile* outfile = new TFile("th2out.root","recreate"); hist->Write(); outfile->Close(); }