Hi Ben, I think your algorithm should work (except that you close mySimFile to early). I think that you proposal (for TTree::ChangeFile) makes sense, I am still looking into potential side-effects. Instead of using the filename to trigger the switch, you can also use the tree number (TChain::GetTreeNumber). You can also attach an object to the TChain to be notified whenever the file of the chain changes: MySwitch *obj; fChain->SetNotify(obj); And MySwitch::Notify will be called each time a new file is opened in the chain or one of its friend. Cheers, Philippe. -----Original Message----- From: owner-roottalk@pcroot.cern.ch [mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Ben Kilminster Sent: Wednesday, March 03, 2004 10:42 AM To: roottalk@pcroot.cern.ch Subject: [ROOT] TChain:: switch file method and Tree :: Change File method Hi, I'm looking for a simple way to keep a one to one correspondence between a file in a TChain and an output file generated from the TChain. Here is the application: 1) I have a TTree called DataTree with branches representing data. 2) I want to resimulate part of the DataTree and write it into another TTree called SimTree, storing the results in a separate file. 3) Later, the analysis code will loop over both DataTree and SimTree on an event by event basis (using TFriend) and make comparisons. The above steps are straight forward. The complicated parts are : 1) DataTree is stored in a chain of multiple files. 2) I want to keep a one-to-one correspondence between the number of files in DataTree and SimTree. This is to avoid confusion when the user loops over different sets of files when they are simulating as compared to when they are analyzing. It seems like the way to do this is something like this (excuse my sloppy pseudo-code) : 1) Loop through DataTree to simulate SimTree, and for each event save the name of each filename that is encountered in DataTree by something like: oldfile = DataTree->GetCurrentFile()->GetName() 2) At the start of each event, check whether a new file has been opened with if (DataTree->GetCurrentFile()->GetName() != oldfile) call method SwitchFile() 3) Run a SwitchFile method to close the SimTree file, open a new one, and add a construction for a new tree - something like this : SwitchFile() { // store structure of SimTree // follow example at : http://root.cern.ch/root/html/src/TTree.cxx.html#TTree:ChangeFile MySimFile->Write(); MySimFile->Close(); char newDatafile[100] = DataTree->GetCurrentFile()->GetName(); char newSimFile[104]; sprintf(newSimFile,"sim.%s",&newDataFile[0]); MySimFile = new TFile(newSimFile,"WRITE"); TTree *SimTree = new TTree("SimTree","simulated tree"); // remake structure of SimTree and store in MySimFile // follow example at : http://root.cern.ch/root/html/src/TTree.cxx.html#TTree:ChangeFile } Is there a better way to do this ? Ideally, I think this would be fairly easy if in the code there was a : TChain::SwitchFile() method which was called whenever a file was closed and a new one opened, and a new ChangeFile method where you could specify the new file name, as in TTree::ChangeFile(TTree *file, TString *newFileName). Thanks, Ben ################################################################# ################################################################# ################################################################# ##### ##### ##### ################################################################# ################################################################# #################################################################
This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:06 MET