#include using namespace std; #include "TFile.h" #include "TTree.h" #include "TEvent_Det.h" void WriteFile() { TEvent_Det* e = new TEvent_Det; TFile* OutFile = new TFile("File1.root", "RECREATE"); TTree* OutTree = new TTree("Tree1", "Tree containing nonsense"); OutTree->Branch("Event", "TEvent_Det", &e); for (int i=0; i<1E3; i++) { e->Clear(); e->FillRandom(); OutTree->Fill(); } OutFile->Write(); delete e; delete OutFile; } void CopySomeEvents() { //Reading: TEvent_Det* Evt = new TEvent_Det; TFile* InFile = new TFile("File1.root", "READ"); TTree* InTree = (TTree*)InFile->Get("Tree1"); InTree->SetBranchAddress("Event", &Evt); int nEvents = InTree->GetEntries(); cout << "There are " << nEvents << " events in the tree." << endl; if (nEvents>1000) nEvents = 1000; cout << "Going to read " << nEvents << endl; //Writing: TFile* OutFile = new TFile("File2.root", "RECREATE"); TTree* OutTree = new TTree("Tree1", "Tree containing nonsense"); OutTree->Branch("Event", "TEvent_Det", &Evt); for (int i=0; iGetEvent(i); if (fabs(Evt->Delays[0]) < 0.2) OutTree->Fill(); } OutFile->Write(); OutFile->Close(); InFile->Close(); } void eventproblem(bool Write) { if (Write) { cout << "Writing some random data..."; WriteFile(); cout << "... done." << endl << "Now trying to read some stuff from the file and copy it to a new one:" << endl; } else cout << "Trying to read data from existing file and copy it to a new one." << endl; CopySomeEvents(); cout << "The end." << endl; }