#include #include #include #include #include #include #include #include using namespace std; int m_global_counter = 0; int m_copy_max = 10000; void chaos(TDirectory*); void test2(bool cp=true){ if(cp) { cout << "Copying File " << endl; gSystem->Exec("cp top.root test.root"); } TFile* f = TFile::Open("test.root", "UPDATE"); chaos(f); f->Close(); } void chaos(TDirectory* dir) { TList* keys = dir->GetListOfKeys(); int nkeys = keys->GetEntries(); cout << "In Directory " << dir->GetPath() /*<< dir->GetName()*/ << " " << dir->GetNkeys() << " Keys" << endl; dir->cd(); std::vector all_keys; for(int ikey = 0; ikey != nkeys; ++ikey) all_keys.push_back(keys->At(ikey)); for(unsigned int ikey = 0; ikey != all_keys.size(); ++ikey){ // cout << all_keys[ikey]->GetName() << endl; TObject* obj = dynamic_cast ( dynamic_cast (all_keys.at(ikey))->ReadObj()); TDirectoryFile* sub_dir = dynamic_cast (obj); if(sub_dir){ chaos(sub_dir); delete obj; continue; } TH1* h = dynamic_cast (obj); if(!h){ delete obj; continue; } h->SetDirectory(0); TH1* h2 = dynamic_cast (h->Clone()); h2->SetDirectory(0); //Either of the next two lines seem to cause trouble h2->Reset(); // h2->Fill(1); //Way to only over-write sub-set of objects if(m_global_counter < m_copy_max){ h2->Write( "", TObject::kOverwrite); //h2->Write( "", TObject::kWriteDelete); // cout << sizeof(*h) << " " << sizeof(*h2) << endl; m_global_counter++; } delete obj; delete h2; }//loop over keys }