#include #include #include "SealKernel/Exception.h" #include "PersistencySvc/Placement.h" #include "FileCatalog/IFileCatalog.h" #include "DataSvc/IDataSvc.h" namespace pool_tutorial { class Sector; } class PoolApplication { public: // Constructor. Initializes all the services. PoolApplication(); // Destructor ~PoolApplication(); // main method void run(); private: // The file catalog pool::IFileCatalog* fileCatalog; // The data service pool::IDataSvc* dataSvc; // The clustering hints pool::Placement placementECAL; pool::Placement placementHCAL; pool::Placement placementEvent; // Private method to generate clusters in the two sectors void generateClusters( pool_tutorial::Sector& ecal, pool_tutorial::Sector& hcal ); }; // The program main. int main( int, char** ) { std::cout << "Writer application starts." << std::endl; try { PoolApplication app; app.run(); } catch ( seal::Exception& e ) { std::cerr << e.what() << std::endl; return 1; } catch ( std::exception& e ) { std::cerr << e.what() << std::endl; return 1; } catch ( ... ) { std::cerr << "Funny error" << std::endl; return 1; } std::cout << "Writer application successfully finished." << std::endl; return 0; } #include #include "SealBase/SharedLibrary.h" #include "SealBase/SharedLibraryError.h" #include "POOLCore/POOLContext.h" #include "FileCatalog/URIParser.h" #include "StorageSvc/DbType.h" #include "PersistencySvc/ISession.h" #include "PersistencySvc/DatabaseConnectionPolicy.h" #include "DataSvc/DataSvcFactory.h" // Constructor of PoolApplication PoolApplication::PoolApplication(): fileCatalog( 0 ), dataSvc( 0 ) { // Loads the seal message stream pool::POOLContext::loadComponent( "SEAL/Services/MessageService" ); // Set the verbosity threshold to warnings pool::POOLContext::setMessageVerbosityLevel( seal::Msg::Warning ); // Loads the dictionary const std::string dictlibrary = "EventModelDict"; try { seal::SharedLibrary::load( seal::SharedLibrary::libname( dictlibrary ) ); } catch ( seal::SharedLibraryError *error) { throw std::runtime_error( error->explainSelf().c_str() ); } // Create a file catalog and connect to it pool::URIParser p; p.parse(); fileCatalog = new pool::IFileCatalog; fileCatalog->setWriteCatalog( p.contactstring() ); fileCatalog->connect(); // create a data service and tell it which catalog to use dataSvc = pool::DataSvcFactory::instance( fileCatalog ); // Define the policy for the implicit file handling pool::DatabaseConnectionPolicy policy; // If the file does not exist, create a new one. policy.setWriteModeForNonExisting( pool::DatabaseConnectionPolicy::CREATE ); // If the file already exists, overwrite it. policy.setWriteModeForExisting( pool::DatabaseConnectionPolicy::OVERWRITE ); // set the policy dataSvc->session().setDefaultConnectionPolicy( policy ); // ECAL Sector objects will be written in a container called "ecal", using a ROOT Tree storage technology. placementECAL.setContainerName( "ecal" ); placementECAL.setTechnology( pool::ROOTTREE_StorageType.type() ); // HCAL Sector objects will be written in a container called "hcal", using a ROOT Tree storage technology. placementHCAL.setContainerName( "hcal" ); placementHCAL.setTechnology( pool::ROOTTREE_StorageType.type() ); // Event objects will be written in a container called "eventHeaders", // using a ROOT Tree storage technology. placementEvent.setContainerName( "eventHeaders" ); placementEvent.setTechnology( pool::ROOTTREE_StorageType.type() ); } // Destructor of PoolApplication PoolApplication::~PoolApplication() { if ( dataSvc ) delete dataSvc; if ( fileCatalog ) delete fileCatalog; } #include #include #include "PersistencySvc/ITransaction.h" #include "DataSvc/Ref.h" #include "Event.h" // Main method of PoolApplication void PoolApplication::run() { // Initialize the random number generator ::srand( 0 ); for ( int iRun = 0; iRun < 2; ++iRun ) { std::cout << "Writing run " << iRun + 1 << std::endl; // A run in a different database file std::ostringstream osrun; osrun << "events_run_" << iRun + 1 << ".root"; placementEvent.setDatabase( osrun.str(), pool::DatabaseSpecification::PFN ); std::ostringstream oscluster; oscluster << "clusters_run_" << iRun + 1 << ".root"; placementECAL.setDatabase( oscluster.str(), pool::DatabaseSpecification::PFN ); placementHCAL.setDatabase( oscluster.str(), pool::DatabaseSpecification::PFN ); // Start a transaction for the file catalog fileCatalog->start(); // Start a new transaction per run dataSvc->transaction().start( pool::ITransaction::UPDATE ); // Now generate 1000 events for ( int i = 0; i < 1000; ++i ) { pool::Ref< pool_tutorial::Event > event( dataSvc, new pool_tutorial::Event( i + 1 ) ); event.markWrite( placementEvent ); // Create the two sectors and attach them to the event pool::Ref< pool_tutorial::Sector > ecal( dataSvc, new pool_tutorial::Sector( "ECAL" ) ); pool::Ref< pool_tutorial::Sector > hcal( dataSvc, new pool_tutorial::Sector( "HCAL" ) ); ecal.markWrite( placementECAL ); hcal.markWrite( placementHCAL ); event->setECalData( ecal ); event->setHCalData( hcal ); // Fill up to four clusters in each sector int nClusters = static_cast( ::floor( static_cast( 5.0 * ::rand() ) / RAND_MAX ) ); for ( int iCluster = 0; iCluster < nClusters; ++iCluster ) { this->generateClusters( *ecal, *hcal ); } } // commit the transaction dataSvc->transaction().commit(); // Commit the file catalog transaction fileCatalog->commit(); } } // Private method to generate clusters in the two sectors void PoolApplication::generateClusters( pool_tutorial::Sector& ecal, pool_tutorial::Sector& hcal ) { // Nominal position of the centre double x = -100.0 + ( 200.0 * ::rand() ) / RAND_MAX; double y = -100.0 + ( 200.0 * ::rand() ) / RAND_MAX; // Decide whether the ecal will respond. // That will be in 3/4 of the cases. if ( ::rand() > RAND_MAX / 4 ) { double xc = x + ( -1.0 + (2.0 * ::rand() ) / RAND_MAX ); double yc = y + ( -1.0 + (2.0 * ::rand() ) / RAND_MAX ); double zc = ( 100.0 * ::rand() ) / RAND_MAX; double de = 50 + ( 10.0 * ::rand() ) / RAND_MAX; pool_tutorial::Cluster cluster; cluster.setEnergyDeposited( de ); cluster.setPosition( xc, yc, zc ); ecal.addCluster( cluster ); } // Decide whether the hcal will respond. // That will be in 1/2 of the cases. if ( ::rand() > RAND_MAX / 2 ) { double xc = x + ( -5.0 + (10.0 * ::rand() ) / RAND_MAX ); double yc = y + ( -5.0 + (10.0 * ::rand() ) / RAND_MAX ); double zc = 100 + ( 100.0 * ::rand() ) / RAND_MAX; double de = 30 + ( 5.0 * ::rand() ) / RAND_MAX; pool_tutorial::Cluster cluster; cluster.setEnergyDeposited( de ); cluster.setPosition( xc, yc, zc ); hcal.addCluster( cluster ); } }