// open_root.C // // David Adams // November 2004 // // ROOT macro to Extract a root file from a DIAL result and open // it in the current root session. // // open_root(filename) Opens and returns reference to the opened root file. // If another file is open, it is closed. // open_root("close") closes the current file // open_root() returns a reference to the last file opened. // // February 2005: Enable opening of dataset with multiple files. //********************************************************************** // Open a named ROOT file. // // Maintains an array of files. The index pos indicates the position // in that array. // com = open, close, or "close all" const TFile* open_root(string com, dset::Url purl= Url(), bool verbose =false, int pos =0) { const int maxpos = 20; if ( pos >= maxpos ) return 0; // Create file array. static TFile* files[20]; static bool first = true; if ( first ) { for ( int ipos=0; iposIsOpen() ) { pfile->Close(); } } } TFile*& rpfile = files[pos]; // Take action if rname is not blank. if ( com.size() ) { // Delete the last file. if ( rpfile != 0 && rpfile->IsOpen() ) { rpfile->Close(); rpfile = 0; } // Attempt to open the file Unless this is a request to close. if ( com != "close" ) { // Open the file. rpfile = TFile::Open(purl.c_str(), "READ"); } } // Display histograms. if ( rpfile != 0 ) rpfile->ls(); return rpfile; } //********************************************************************** // Extract the ROOT file from a DIAL result and open the file. const TFile* open_root(const dset::Dataset& res, bool verbose=false, int pos =0) { // Root dataset: open associated file if ( res.fulltype() == "RootHistogramDataset" ) { const dset::RootHistogramDataset* pres = dynamic_cast(&res); // Extract the file name. dset::Url purl = pres->staged_file(); if ( ! purl.is_valid() ) { cout << "Unable to extract file name from result" << endl; cout << res << endl; return open_root("close"); } // Open the file. return open_root("open", purl, verbose, pos); // Compound dataset: open each constituent } else if ( res.fulltype() == "SimpleCompoundDataset" ) { // For now, there may only be one level of nesting. if ( pos != 0 ) { cout << "Dataset structure is too complex" << endl; return open_root("close all", dset::Url(), verbose); } TFile* pfile = 0; const dset::DatasetList& dsts = res.constituents(); int count = 0; for ( dset::DatasetList::const_iterator idst=dsts.begin(); idst!=dsts.end(); ++idst ) { const dset::Dataset* pdst = *idst; if ( pdst == 0 ) { cout << "NULL constituent dataset" << endl; } else { if ( verbose ) { cout << "Constituent " << count << ":\n" << *pdst << endl; } pfile = open_root(*pdst, verbose, count); ++count; } } return pfile; // Event dataset: ignore. } else if ( res.is_event_dataset() ) { return 0; // Text dataset: ignore. } else if ( res.fulltype() == "TextDataset" ) { return 0; } else { cout << "Ignoring non-event dataset of unknown type:\n " << res.fulltype() << endl; return 0; } // Unknown dataset. cout << "Unknown dataset type" << endl; return open_root("close all", dset::Url(), verbose); } //********************************************************************** // Extract the ROOT file from a DIAL result and open the file. // Returns 0 if job is inactive. const TFile* open_root(bool verbose =false) { if ( ! jid.is_valid() ) { cout << "Job ID jid is not valid" << endl; return 0; } const dial::Job job3 = msch.job(jid); cout << endl; // Acquire mutex so that result is consistent with job description. job3.lock_mutex(); if ( job3.is_valid() ) { cout << job3 << endl; if ( job3.has_result() ) { if ( job3.result() == 0 ) { cout << "Unable to access job result dataset." << endl; cout << " Dataset ID is " << job3.result_id() << endl; return 0; } const dset::Dataset* pres = job3.result(); if ( pres == 0 ) { cout << "Unable to access result for job " << jid << endl; return 0; } return open_root(*pres, verbose); } else { cout << "Job " << jid << " does not have a result" << endl; } } else { cout << "Job " << jid << " is not valid" << endl; } job3.unlock_mutex(); return 0; } //**********************************************************************