00001
00002 #include <mcRootData/McEvent.h>
00003 #include <commonRootData/RootDataUtil.h>
00004 #include "Riostream.h"
00005 #include "TROOT.h"
00006 #include "TFile.h"
00007 #include "TTree.h"
00008 #include "TRandom.h"
00009
00023 const UInt_t RUN_NUM = 1 ;
00024 Float_t RAND_NUM ;
00025
00026 int read( char * fileName, unsigned int numEvents) {
00027
00028
00029
00030 TFile *f = new TFile(fileName, "READ");
00031 TTree *t = (TTree*)f->Get("Mc");
00032 McEvent *evt = 0;
00033 t->SetBranchAddress("McEvent", &evt);
00034
00035 std::cout << "Opened the ROOT file for reading" << std::endl;
00036
00037 UInt_t iEvent ;
00038 for ( iEvent = 0 ; iEvent < numEvents ; ++iEvent ) {
00039
00040 t->GetEvent(iEvent);
00041
00042 std::cout << "McEvent iEvent = " << iEvent << std::endl;
00043 evt->Print() ;
00044
00045
00046
00047
00048
00049 if (!evt->CompareToFake(iEvent,RUN_NUM,RAND_NUM)) {
00050 return -1 ;
00051 }
00052
00053 }
00054
00055 f->Close();
00056 delete f;
00057
00058 return(0);
00059 }
00060
00062 int write( char * fileName, UInt_t numEvents ) {
00063
00064 Int_t buffer = 64000;
00065 Int_t splitLevel = 1;
00066
00067 TFile *f = new TFile(fileName, "RECREATE");
00068 TTree *t = new TTree("Mc", "Mc");
00069 McEvent * evt = new McEvent() ;
00070 t->Branch("McEvent", "McEvent", &evt, buffer, splitLevel);
00071 std::cout << "Created new ROOT file" << std::endl;
00072
00073 UInt_t iEvent ;
00074 for (iEvent = 0; iEvent < numEvents; iEvent++) {
00075 evt->Fake(iEvent,RUN_NUM,RAND_NUM) ;
00076 t->Fill() ;
00077 }
00078
00079 std::cout << "Filled ROOT file with " << numEvents << " events" << std::endl;
00080 delete evt ;
00081
00082 f->Write();
00083 f->Close();
00084 delete f;
00085 return(0);
00086 }
00087
00088
00092 int main(int argc, char **argv) {
00093
00094 char *fileName = "mc.root";
00095 int n =1 ;
00096 unsigned int numEvents =10 ;
00097 if (argc > 1) {
00098 fileName = argv[n++];
00099 }
00100 if (argc > 2) {
00101 numEvents = atoi(argv[n++]);
00102 }
00103
00104 TRandom randGen ;
00105 RAND_NUM = randGen.Rndm() ;
00106
00107 int sc = 0;
00108 try
00109 {
00110 sc = write(fileName, numEvents);
00111 sc = read(fileName, numEvents);
00112 }
00113 catch (...)
00114 {
00115 std::cout<<"AN UNKNOWN EXCEPTION HAS BEEN RAISED"<<std::endl ;
00116 sc = 1 ;
00117 }
00118
00119 if (sc == 0) {
00120 std::cout << "MC ROOT file writing and reading suceeded!" << std::endl;
00121 } else {
00122 std::cout << "FAILED" << std::endl;
00123 }
00124
00125 return(sc);
00126 }
00127
00128