If you still have problems, attach your “ww_nocut-1.root” file here for inspection.
#include "TH1.h"
#include "TFile.h"
#include "TDirectory.h"
#include "TCollection.h"
#include "TKey.h"
#include "TClass.h"
#include "TROOT.h"
#include <iostream>
#include <string>
#include <vector>
void Normalize(TH1 *h, Double_t norm) {
if ((!h) || (norm == 0.)) return; // just a precaution
Double_t integral = h->Integral();
if (integral != 0.) h->Scale(norm / integral);
else std::cout << h->GetName() << " is non-normalizable." << std::endl;
}
void histo_norm() {
std::vector<std::string> input_vector;
std::string input_string;
std::cout << "Name the files to read (When you're finished, enter *):" << std::endl;
while(std::cin >> input_string) {
if (input_string == "*") break;
input_vector.push_back(input_string);
}
for(unsigned int n = 0; n < input_vector.size(); n++) {
TFile *output = TFile::Open(TString::Format("output_%s", (input_vector[n]).c_str()), "RECREATE");
std::cout << "Opening " << output->GetName() << std::endl;
TFile *input_file = TFile::Open((input_vector[n]).c_str());
//f->ls();
TDirectory *input_dir = gDirectory;
TIter next_dir(input_dir->GetListOfKeys());
TKey *dir_key;
while((dir_key = (TKey*) next_dir())){
if(dir_key->IsFolder()){ // Warning: TTree and TNtuple are also "folders"
input_file->cd();
input_dir->cd(dir_key->GetName());
//input_file->cd();
TIter next_folder(gDirectory->GetListOfKeys());
TKey *key_folder;
while((key_folder = (TKey*) next_folder())){
TClass *cl = gROOT->GetClass(key_folder->GetClassName());
if(!cl->InheritsFrom("TH1")) continue;
TH1 *h = (TH1*)key_folder->ReadObj();
if (h) {
h->SetDirectory(output);
std::cout << "Connecting " << h->GetName() << std::endl;
Normalize(h, 1.0);
}
}
}
}
delete input_file;
output->Write();
std::cout << "Closing " << output->GetName() << std::endl;
delete output;
}
}