// make_event_merge_dataset.cxx // // David Adams // June 2003 // // Create an event merge dataset from a collection of datasets. // The latter are specified by ID. #include #include #include #include "dataset_util/FileStatus.h" #include "dataset_util/Environment.h" #include "dataset_util/XmlElement.h" #include "dataset_file/FileCatalog.h" #include "dataset_base/EventMergeDataset.h" #include "dataset_xml/XmlParser.h" #include "dataset_xml/SimpleXmlDatasetDb.h" #include "dial_com/dial_init.h" #include "dial_com/registrations.h" using std::string; using std::cout; using std::endl; using std::vector; using dset::FileId; using dset::FileCatalog; using dset::Dataset; using dset::EventDataset; using dset::EventMergeDataset; using dset::SimpleXmlDatasetDb; int main(int argc, char* argv[]) { // Fetch arguments. string arg0 = argv[0]; // List if ID's. vector ids; // Print help and exit. bool help = false; // Set dataset ID to this value. DatasetId dstid; // Write XML to a file. string xfile = "dataset.xml"; // Record dataset in SimpleXmlDatasetDb:DIAL_DATASET_DB bool db = false; assert( ! dstid.is_valid() ); for ( int iarg=1; iarg dsts; ContentIdList cids; for ( vector::const_iterator idid=ids.begin(); idid!=ids.end(); ++idid ) { DatasetId did = *idid; // Retrieve dataset and check retrieval. const Dataset* pdst = ddb.extract(did); if ( pdst == 0 ) { cout << "Unable to retrieve dataset with ID " << did.to_string() << endl; return 3; } // Check validity. if ( ! pdst->is_valid() ) { cout << "Invalid dataset with ID " << did.to_string() << endl; return 4; } // Check this is an event dataset. if ( ! pdst->is() ) { cout << "Non-event dataset with ID " << did.to_string() << endl; return 5; } // First entry, record the content. const EventDataset& edst = pdst->cast(); if ( idid == ids.begin() ) { cids = edst.content_ids(); cout << cids << endl; // All others, verify the content. } else { if ( edst.content_ids() != cids ) { cout << "Inconsistent content in dataset with ID " << did.to_string() << endl; return 6; } } // Add dataset to list. dsts.push_back(&edst); } assert( dsts.size() == ids.size() ); // Create dataset. EventMergeDataset mdst(cids); assert( mdst.is_valid() ); for ( vector::const_iterator idst=dsts.begin(); idst!=dsts.end(); ++idst ) { const EventDataset& edst = **idst; cout << "...Adding dataset " << edst.id().to_string() << " with " << edst.event_ids().size() << " event"; if ( edst.event_ids().size() != 1 ) cout << "s"; cout << endl; int mstat = mdst.add_events(**idst); if ( mstat != 0 ) { cout << "Event merge failed with error " << mstat << endl; return 7; } } if ( ! mdst.is_valid() ) { cout << "Unable to create dataset" << endl; return 8; } // Set ID. if ( dstid.is_valid() ) { if ( ! mdst.set_id(dstid).is_valid() ) { cout << "Error setting dataset ID " << dstid << endl; return 9; } } mdst.lock(); cout << "Merged dataset has " << mdst.event_ids().size() << " event"; if ( mdst.event_ids().size() != 1 ) cout << "s"; cout << endl; // Create XML. if ( ! mdst.id().is_global() ) { cout << "Dataset has local ID." << endl; cout << "Define Dataset ID generator or use option -i" << endl; return 10; } const XmlElement* pele = mdst.xml(); if ( pele == 0 ) { cout << "Unable to create XML" << endl; return 11; } // Write XML description. if ( xfile.size() ) { XmlParser parser; parser.write(xfile, *pele); delete pele; cout << "Output in " << xfile << endl; } // Write to database. if ( db ) { if ( ddb.insert(&mdst, false).is_valid() ) { cout << "Dataset inserted in database\n " << ddb << endl; } else { cout << "Dataset insertion failed for\n " << ddb << endl; return 12; } } }