Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:22

0001 /*
0002  *  recordwriter.cppunit.cc
0003  *  CMSSW
0004  *
0005  *  Created by Chris Jones on 09/12/09.
0006  *
0007  */
0008 #include <vector>
0009 #include <iostream>
0010 
0011 #include "TFile.h"
0012 #include "TTree.h"
0013 
0014 #include <cppunit/extensions/HelperMacros.h>
0015 #include "PhysicsTools/CondLiteIO/interface/RecordWriter.h"
0016 #include "DataFormats/Provenance/interface/ESRecordAuxiliary.h"
0017 #include "DataFormats/FWLite/interface/format_type_name.h"
0018 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0019 
0020 class testRecordWriter : public CppUnit::TestFixture {
0021   CPPUNIT_TEST_SUITE(testRecordWriter);
0022 
0023   CPPUNIT_TEST(testNoInheritance);
0024   CPPUNIT_TEST(testInheritance);
0025 
0026   CPPUNIT_TEST_SUITE_END();
0027 
0028 public:
0029   void setUp() {}
0030   void tearDown() {}
0031 
0032   void testNoInheritance();
0033   void testInheritance();
0034 };
0035 
0036 ///registration of the test so that the runner can find it
0037 CPPUNIT_TEST_SUITE_REGISTRATION(testRecordWriter);
0038 
0039 void testRecordWriter::testNoInheritance() {
0040   {
0041     TFile f("testRecordWriter.root", "RECREATE");
0042     fwlite::RecordWriter w("TestRecord", &f);
0043 
0044     std::vector<int> v;
0045     for (int index = 0; index < 10; ++index) {
0046       //std::cout <<" index "<<index<<std::endl;
0047       v.push_back(index);
0048       w.update(&v, typeid(v), "");
0049       w.fill(edm::ESRecordAuxiliary(edm::EventID(index + 1, 0, 0), edm::Timestamp()));
0050     }
0051     w.write();
0052     f.Close();
0053   }
0054 
0055   {
0056     TFile f("testRecordWriter.root", "READ");
0057     TTree* recordTree = reinterpret_cast<TTree*>(f.Get("TestRecord"));
0058     CPPUNIT_ASSERT(0 != recordTree);
0059 
0060     edm::ESRecordAuxiliary* aux = 0;
0061     recordTree->SetBranchAddress("ESRecordAuxiliary", &aux);
0062 
0063     std::vector<int>* pV = 0;
0064     recordTree->SetBranchAddress((fwlite::format_type_to_mangled("std::vector<int>") + "__").c_str(), &pV);
0065 
0066     for (int index = 0; index < recordTree->GetEntries(); ++index) {
0067       recordTree->GetEntry(index);
0068       CPPUNIT_ASSERT(aux->eventID().run() == static_cast<unsigned int>(index + 1));
0069       CPPUNIT_ASSERT(pV->size() == static_cast<size_t>(index + 1));
0070     }
0071   }
0072 }
0073 
0074 void testRecordWriter::testInheritance() {
0075   {
0076     TFile f("testRecordWriter.root", "RECREATE");
0077     fwlite::RecordWriter w("TestRecord", &f);
0078 
0079     edmtest::SimpleDerived d;
0080     for (int index = 0; index < 10; ++index) {
0081       //std::cout <<" index "<<index<<std::endl;
0082       d.key = index;
0083       d.value = index;
0084       d.dummy = index;
0085       w.update(&d, typeid(edmtest::Simple), "");
0086       w.fill(edm::ESRecordAuxiliary(edm::EventID(index + 1, 0, 0), edm::Timestamp()));
0087     }
0088     w.write();
0089     f.Close();
0090   }
0091 
0092   {
0093     TFile f("testRecordWriter.root", "READ");
0094     TTree* recordTree = reinterpret_cast<TTree*>(f.Get("TestRecord"));
0095     CPPUNIT_ASSERT(0 != recordTree);
0096 
0097     edm::ESRecordAuxiliary* aux = 0;
0098     recordTree->SetBranchAddress("ESRecordAuxiliary", &aux);
0099 
0100     edmtest::Simple* pS = 0;
0101     recordTree->SetBranchAddress((fwlite::format_type_to_mangled("edmtest::Simple") + "__").c_str(), &pS);
0102 
0103     for (int index = 0; index < recordTree->GetEntries(); ++index) {
0104       recordTree->GetEntry(index);
0105       CPPUNIT_ASSERT(aux->eventID().run() == static_cast<unsigned int>(index + 1));
0106       CPPUNIT_ASSERT(pS->key == index);
0107       CPPUNIT_ASSERT(0 != dynamic_cast<edmtest::SimpleDerived*>(pS));
0108       CPPUNIT_ASSERT(index == dynamic_cast<edmtest::SimpleDerived*>(pS)->dummy);
0109     }
0110   }
0111 }
0112 
0113 #include <Utilities/Testing/interface/CppUnit_testdriver.icpp>