From fcd6ddd107c49a74c0f8f97ecdbbfb7fe69b42c1 Mon Sep 17 00:00:00 2001
From: zoujh <zoujh@ihep.ac.cn>
Date: Wed, 14 Aug 2019 11:20:48 +0800
Subject: [PATCH] add writing test

---
 Event/CMakeLists.txt  | 23 ++++++++++++++++-------
 Event/tests/write.cpp | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 7 deletions(-)
 create mode 100644 Event/tests/write.cpp

diff --git a/Event/CMakeLists.txt b/Event/CMakeLists.txt
index bb764a76..1d168589 100644
--- a/Event/CMakeLists.txt
+++ b/Event/CMakeLists.txt
@@ -30,13 +30,15 @@ file(GLOB headers Event/*.h)
 include_directories(Event)
 
 # Dictionary and Modules
+add_library(Event SHARED ${sources} ${headers})
+target_link_libraries(Event podio)
+install(TARGETS Event DESTINATION lib)
+
 REFLEX_GENERATE_DICTIONARY(Event ${headers} SELECTION src/selection.xml)
 add_library(EventDict SHARED Event.cxx)
 add_dependencies(EventDict Event-dictgen)
-target_link_libraries(EventDict Event ROOT::Core)
-
-add_library(Event SHARED ${sources} ${headers})
-target_link_libraries(Event podio ROOT::RIO ROOT::Tree)
+target_link_libraries(EventDict Event)
+install(TARGETS EventDict DESTINATION lib)
 
 # Headers and Libraries
 gaudi_install_headers(Event)
@@ -44,6 +46,13 @@ gaudi_install_headers(Event)
 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/EventDict.rootmap DESTINATION lib)
 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/Event_rdict.pcm DESTINATION lib)
 
-# Unit tests
-#gaudi_add_test(HelloAlg
-#               FRAMEWORK options/helloalg.py)
+# tests
+add_executable( write tests/write.cpp )
+target_link_libraries( write EventDict )
+install(TARGETS write DESTINATION tests)
+add_test(NAME write COMMAND write)
+set_property(TEST write PROPERTY ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_INSTALL_PREFIX}/lib:$ENV{LD_LIBRARY_PATH})
+
+#add_test(NAME read COMMAND read)
+#set_property(TEST read PROPERTY ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_INSTALL_PREFIX}/lib:$ENV{LD_LIBRARY_PATH})
+#set_property(TEST read PROPERTY DEPENDS write)
diff --git a/Event/tests/write.cpp b/Event/tests/write.cpp
new file mode 100644
index 00000000..68109a85
--- /dev/null
+++ b/Event/tests/write.cpp
@@ -0,0 +1,41 @@
+#include "EventHeaderCollection.h"
+#include "MCParticleCollection.h"
+
+#include "podio/EventStore.h"
+#include "podio/ROOTWriter.h"
+
+#include <vector>
+#include <iostream>
+
+int main(int argc, char* argv[])
+{
+    std::cout << "start writing test" << std::endl;
+
+    auto store = podio::EventStore();
+    auto writer = podio::ROOTWriter("test.root", &store);
+
+    auto& ehc = store.create<plcio::EventHeaderCollection>("EvtHeaders");
+    auto& mcc = store.create<plcio::MCParticleCollection>("MCParticles");
+
+    writer.registerForWrite("EvtHeaders");
+    writer.registerForWrite("MCParticles");
+
+    const unsigned int nEvt = 100;
+
+    for ( unsigned int i = 0; i < nEvt; ++i ) {
+        if ( i%10 == 0 ) {
+            std::cout << "processing event " << i << std::endl;
+        }
+
+        auto header = plcio::EventHeader(i, -99, 9999, "SimDet");
+        ehc.push_back(header);
+
+        //////////
+        writer.writeEvent();
+        store.clearCollections();
+    }
+
+    writer.finish();
+
+    return 0;
+}
-- 
GitLab