Commit 05e9f54b authored by Matthieu Dorier's avatar Matthieu Dorier

added many examples

parent c70bfcba
......@@ -59,6 +59,10 @@ endif (CPPUNIT_FOUND)
add_subdirectory (src)
add_subdirectory (bin)
if(ENABLE_EXAMPLES)
message(STATUS "Examples will be built")
add_subdirectory (examples)
endif(ENABLE_EXAMPLES)
if(CPPUNIT_FOUND AND ENABLE_TESTS)
message(STATUS "Unit tests are enabled, will be built")
......
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos)
......@@ -8,7 +8,7 @@ int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <configfile>" << std::endl;
exit(-1)
exit(-1);
}
std::string configFile(argv[1]);
......@@ -17,6 +17,6 @@ int main(int argc, char** argv) {
// ...
// only if you want to shutdown HEPnOS
// only if you want to shutdown the HEPnOS service
datastore.shutdown();
}
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos)
#include <iostream>
#include <string>
#include <hepnos.hpp>
using namespace hepnos;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <configfile>" << std::endl;
exit(-1);
}
std::string configFile(argv[1]);
DataStore datastore = DataStore::connect(configFile);
// get the root of the DataStore
DataSet root = datastore.root();
// ----------------------------------------------------------------
{
// create a DataSet
DataSet example2 = root.createDataSet("example2");
std::cout << "Created dataset " << example2.fullname() << std::endl;
// create a DataSet inside the example2 DataSet
DataSet exp1 = example2.createDataSet("exp1");
std::cout << "Create dataset " << exp1.fullname() << std::endl;
// create a Run with number 36 inside the DataSet
Run r = exp1.createRun(36);
std::cout << "Created run " << r.number() << std::endl;
// create a SubRun with number 42 inside this Run
SubRun sr = r.createSubRun(42);
std::cout << "Created subrun " << sr.number() << std::endl;
// create an Event with number 13 inside this SubRun
Event ev = sr.createEvent(13);
std::cout << "Create event " << ev.number() << std::endl;
}
// ----------------------------------------------------------------
{
// accessing example2 dataset
DataSet example2 = root["example2"];
std::cerr << "DataSet retrieved: " << example2.name() << std::endl;
// accessing exp1 dataset
DataSet exp1 = example2["exp1"];
std::cerr << "DataSet retrieved: " << exp1.name() << std::endl;
// accessing run 36
Run r = exp1[36];
std::cerr << "Run retrieved: " << r.number() << std::endl;
// accessing subrun 42
SubRun sr = r[42];
std::cerr << "Event retrieved: " << sr.number() << std::endl;
// accessing event 13
Event ev = sr[13];
std::cerr << "Event retrieved: " << ev.number() << std::endl;
}
}
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos)
#include <iostream>
#include <string>
#include <hepnos.hpp>
using namespace hepnos;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <configfile>" << std::endl;
exit(-1);
}
std::string configFile(argv[1]);
DataStore datastore = DataStore::connect(configFile);
// Get the root of the DataStore
DataSet root = datastore.root();
// Create a DataSet
DataSet example3 = root.createDataSet("example3");
// Create 5 DataSets in example3
for(unsigned i = 0; i < 5; i++) {
std::string datasetName = "sub";
datasetName += std::to_string(i+1);
example3.createDataSet(datasetName);
}
// Iterate over the child datasets
// This is equivalent to using begin() and end()
std::cout << "Datasets in example3: " << std::endl;
for(auto& dataset : example3) {
std::cout << dataset.name() << std::endl;
}
// access a DataSet by its full name
DataSet sub2 = root["example3/sub2"];
// find the sub3 DataSet
DataSet::iterator it = example3.find("sub3");
std::cout << it->fullname() << std::endl;
// lower_bound("sub3") will point to the sub3 dataset
DataSet::iterator lb = example3.lower_bound("sub3");
// upper_bound("sub3") will point to the sub4 dataset
DataSet::iterator ub = example3.upper_bound("sub3");
}
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos)
#include <iostream>
#include <string>
#include <hepnos.hpp>
using namespace hepnos;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <configfile>" << std::endl;
exit(-1);
}
std::string configFile(argv[1]);
DataStore datastore = DataStore::connect(configFile);
// Get the root of the DataStore
DataSet root = datastore.root();
// Create a DataSet
DataSet example4 = root.createDataSet("example4");
// Create 5 Runs 42 ... 46
for(unsigned i = 0; i < 5; i++) {
example4.createRun(i+42);
}
// Iterate over the Runs
std::cout << "Runs:" << std::endl;
for(auto& run : example4.runs()) {
std::cout << run.number() << std::endl;
}
// access a Run by its number
Run run43 = example4[43];
// find the Run 43
RunSet::iterator it = example4.runs().find(43);
std::cout << it->number() << std::endl;
// lower_bound(43) will point to the Run 43
RunSet::iterator lb = example4.runs().lower_bound(43);
// upper_bound(43) will point to the Run 44
RunSet::iterator ub = example4.runs().upper_bound(43);
}
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos)
#include <iostream>
#include <string>
#include <hepnos.hpp>
using namespace hepnos;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <configfile>" << std::endl;
exit(-1);
}
std::string configFile(argv[1]);
DataStore datastore = DataStore::connect(configFile);
// Get the root of the DataStore
DataSet root = datastore.root();
// Create a DataSet
DataSet example5 = root.createDataSet("example5");
// Create a Run 0
Run run = example5.createRun(0);
// Create 5 SubRuns 42 ... 46
for(unsigned i = 0; i < 5; i++) {
run.createSubRun(i+42);
}
// Iterate over the SubRuns
std::cout << "SubRuns:" << std::endl;
for(auto& subrun : run) {
std::cout << subrun.number() << std::endl;
}
// access a SubRun by its number
SubRun subrun43 = run[43];
// find the SubRun 43
Run::iterator it = run.find(43);
std::cout << it->number() << std::endl;
// lower_bound(43) will point to the SubRun 43
Run::iterator lb = run.lower_bound(43);
// upper_bound(43) will point to the SubRun 44
Run::iterator ub = run.upper_bound(43);
}
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos)
#include <iostream>
#include <string>
#include <hepnos.hpp>
using namespace hepnos;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <configfile>" << std::endl;
exit(-1);
}
std::string configFile(argv[1]);
DataStore datastore = DataStore::connect(configFile);
// Get the root of the DataStore
DataSet root = datastore.root();
// Create a DataSet
DataSet example6 = root.createDataSet("example6");
// Create a Run 0
Run run = example6.createRun(0);
// Create a SubRun 13
SubRun subrun = run.createSubRun(13);
// Create 5 Events 42 ... 46
for(unsigned i = 0; i < 5; i++) {
subrun.createEvent(i+42);
}
// Iterate over the Events
std::cout << "Events:" << std::endl;
for(auto& event : subrun) {
std::cout << event.number() << std::endl;
}
// access a Event by its number
Event event43 = subrun[43];
// find the Event 43
SubRun::iterator it = subrun.find(43);
std::cout << it->number() << std::endl;
// lower_bound(43) will point to the Event 43
SubRun::iterator lb = subrun.lower_bound(43);
// upper_bound(43) will point to the Event 44
SubRun::iterator ub = subrun.upper_bound(43);
}
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos)
#include <iostream>
#include <string>
#include <hepnos.hpp>
using namespace hepnos;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <configfile>" << std::endl;
exit(-1);
}
std::string configFile(argv[1]);
DataStore datastore = DataStore::connect(configFile);
// Get the root of the DataStore
DataSet root = datastore.root();
// Create a DataSet
DataSet example7 = root.createDataSet("example7");
// Create 5 Runs with 5 SubRuns with 5 Events
for(unsigned i=0; i < 5; i++) {
auto run = example7.createRun(i);
for(unsigned j=0; j < 5; j++) {
auto subrun = run.createSubRun(j);
for(unsigned k=0; k < 5; k++) {
auto event = subrun.createEvent(k);
}
}
}
// Iterate over the events directly from the example7 DataSet
for(auto& event : example7.events()) {
SubRun subrun = event.subrun();
Run run = subrun.run();
std::cout << "Run " << run.number()
<< ", SubRun " << subrun.number()
<< ", Event " << event.number()
<< std::endl;
}
// Iterate target by target
unsigned numTargets = datastore.numTargets(ItemType::EVENT);
for(unsigned target = 0; target < numTargets; target++) {
for(auto& event : example7.events()) {
SubRun subrun = event.subrun();
Run run = subrun.run();
std::cout << "Run " << run.number()
<< ", SubRun " << subrun.number()
<< ", Event " << event.number()
<< std::endl;
}
}
}
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos Boost::serialization)
#include <iostream>
#include <string>
#include <hepnos.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
using namespace hepnos;
struct Particle {
std::string name;
double x, y, z;
Particle() {}
Particle(const std::string& name, double x, double y, double z)
: name(name), x(x), y(y), z(z) {}
template<typename Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & name;
ar & x;
ar & y;
ar & z;
}
};
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <configfile>" << std::endl;
exit(-1);
}
std::string configFile(argv[1]);
DataStore datastore = DataStore::connect(configFile);
// Get the root of the DataStore
DataSet root = datastore.root();
// Create a DataSet, a Run, a SubRun, and an Event
DataSet example8 = root.createDataSet("example8");
Run run = example8.createRun(1);
SubRun subrun = run.createSubRun(4);
Event event = subrun.createEvent(32);
// Store a product into the event
{
Particle p("electron", 3.4, 4.5, 5.6);
ProductID pid = event.store("mylabel", p);
}
// Reload a product from the event
{
Particle p;
bool b = event.load("mylabel", p);
if(b) std::cout << "Particle loaded succesfully" << std::endl;
else std::cout << "Particle wasn't loaded" << std::endl;
}
// Store a section of a vector into the event
{
std::vector<Particle> v;
for(unsigned i=0; i < 5; i++) {
v.emplace_back("electron", i*4, i*2, i+1);
}
// store only the sub-vector [1,3[ (2 elements)
event.store("myvec", v, 1, 3);
}
// Load the vector
{
std::vector<Particle> v;
event.load("myvec", v);
std::cout << "Reloaded " << v.size() << " particles" << std::endl;
}
}
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos Boost::serialization)
#include <iostream>
#include <string>
#include <hepnos.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
using namespace hepnos;
struct Particle {
std::string name;
double x, y, z;
Particle() {}
Particle(const std::string& name, double x, double y, double z)
: name(name), x(x), y(y), z(z) {}
template<typename Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & name;
ar & x;
ar & y;
ar & z;
}
};
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <configfile>" << std::endl;
exit(-1);
}
std::string configFile(argv[1]);
DataStore datastore = DataStore::connect(configFile);
// Get the root of the DataStore
DataSet root = datastore.root();
// Create a DataSet, a Run, a SubRun, and an Event
DataSet example9 = root.createDataSet("example9");
Run run = example9.createRun(1);
SubRun subrun = run.createSubRun(4);
Event event = subrun.createEvent(32);
// Store a product into the event
{
Particle p("electron", 3.4, 4.5, 5.6);
ProductID pid = event.store("mylabel", p);
Ptr<Particle> ptr = datastore.makePtr<Particle>(pid);
event.store("myptr", ptr);
}
// Reload a product from the event
{
Particle p;
Ptr<Particle> ptr;
bool b = event.load("myptr", ptr);
if(b) {
std::cout << "Pointer loaded succesfully" << std::endl;
p = *ptr;
std::cout << "Particle pointed : "
<< p.name << " "
<< p.x << ", " << p.y << ", " << p.z << std::endl;
} else {
std::cout << "Pointer wasn't loaded" << std::endl;
}
}
// Store a vector into the event and a pointer to an element
{
std::vector<Particle> v;
for(unsigned i=0; i < 5; i++) {
v.emplace_back("electron", i*4, i*2, i+1);
}
auto pid = event.store("myvec", v);
// pointer to particle at index 3
Ptr<Particle> ptr = datastore.makePtr<Particle>(pid,3);
event.store("myptr2vec", ptr);
}
// Load the particle from its underlying vector
{
Ptr<Particle> ptr;
event.load("myptr2vec", ptr);
Particle p = *ptr;
}
}
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos)
#include <iostream>
#include <string>
#include <hepnos.hpp>
using namespace hepnos;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <configfile>" << std::endl;
exit(-1);
}
std::string configFile(argv[1]);
DataStore datastore = DataStore::connect(configFile);
RunDescriptor runDescriptor;
SubRunDescriptor subrunDescriptor;
EventDescriptor eventDescriptor;
// get the root of the DataStore
DataSet root = datastore.root();
// ----------------------------------------------------------------
{
// create a DataSet
DataSet example10 = root.createDataSet("example10");
std::cout << "Created dataset " << example10.fullname() << std::endl;
// create a DataSet inside the example10 DataSet
DataSet exp1 = example10.createDataSet("exp1");
std::cout << "Create dataset " << exp1.fullname() << std::endl;
// create a Run with number 36 inside the DataSet
Run r = exp1.createRun(36);
std::cout << "Created run " << r.number() << std::endl;
// create a SubRun with number 42 inside this Run
SubRun sr = r.createSubRun(42);
std::cout << "Created subrun " << sr.number() << std::endl;
// create an Event with number 13 inside this SubRun
Event ev = sr.createEvent(13);
std::cout << "Created event " << ev.number() << std::endl;
// serialize the descriptors
r.toDescriptor(runDescriptor);
sr.toDescriptor(subrunDescriptor);
ev.toDescriptor(eventDescriptor);
}
// ----------------------------------------------------------------
{
// accessing example10 dataset
DataSet example10 = root["example10"];
std::cerr << "DataSet retrieved: " << example10.name() << std::endl;
// accessing exp1 dataset
DataSet exp1 = example10["exp1"];
std::cerr << "DataSet retrieved: " << exp1.name() << std::endl;
// rebuilding run 36
Run r = Run::fromDescriptor(datastore, runDescriptor);
std::cerr << "Run retrieved: " << r.number() << std::endl;
// rebuilding subrun 42
SubRun sr = SubRun::fromDescriptor(datastore, subrunDescriptor);
std::cerr << "Event retrieved: " << sr.number() << std::endl;
// rebuilding event 13
Event ev = Event::fromDescriptor(datastore, eventDescriptor);
std::cerr << "Event retrieved: " << ev.number() << std::endl;
}
}
# Little trick to name the executable the same as the directory that contains it
get_filename_component(THISDIR ${CMAKE_CURRENT_LIST_FILE}/.. ABSOLUTE)
get_filename_component(EXECNAME ${THISDIR} NAME)
# Fin all cpp files in the current directory
file(GLOB SOURCES
"*.hpp"
"*.cpp"
)
add_executable(${EXECNAME} ${SOURCES})
target_link_libraries(${EXECNAME} hepnos Boost::serialization)
#include <iostream>
#include <string>
#include <hepnos.hpp>