StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestFileIter.C
1 // $Id: TestFileIter.C,v 1.3 2003/04/30 20:40:19 perev Exp $
2 // Author: Valery Fine(fine@bnl.gov) 01/03/2001
3 // Copyright(c) 2001 [BNL] Brookhaven National Laboratory, Valeri Fine (fine@bnl.gov). All right reserved",
4 void TestFileIter(){
5 // This macros tests the various methods of TFileIter class.
6  gSystem->Load("libTable");
7 
8  //First create simple ROOT file
9  TDataSet *ds = new TDataSet("event");
10  TObject *nextObject = 0;
11  TRandom run;
12  TRandom event;
13  {
14  TFileIter *outSet = new TFileIter("test.root","RECREATE");
15  UInt_t totalEvent = 4;
16  UInt_t runNumber = 20010301;
17  Int_t i=0;
18  Int_t j=0;
19  for (;j < 3;j++) {
20  for (i = 1;i<totalEvent;i++) {
21  outSet->NextEventPut(ds,UInt_t(i),UInt_t(runNumber+j+10*run.Rndm()-5));
22  }
23  }
24  delete outSet;
25  }
26  printf(" ----------------------> TFile has been created <--------------------\n");
27  TFile *f = new TFile("test.root");
28  TFileIter readObj(f);
29  // the number of the object available directly from "MyDataSet.root"
30  Int_t size = readObj.TotalKeys();
31  printf(" The total number of the objects: %d\n",size);
32 
33  //-----------------------------------------------------------------------
34  // Loop over all objects, read them in to memory one by one
35 
36  printf(" -- > Loop over all objects, read them in to memory one by one < -- \n");
37  for( readObj = 0; int(readObj) < size; readObj.SkipObjects() ){
38  nextObject = *readObj;
39  printf(" %d bytes of the object \"%s\" of class \"%s\" written with TKey \"%s\" has been read from file\n"
40  ,readObj.GetObjlen()
41  ,nextObject->GetName()
42  ,nextObject->IsA()->GetName()
43  ,(const char *)readObj
44  );
45  delete nextObject;
46  }
47 //-----------------------------------------------------------------------
48 // Now loop over all objects in inverse order
49  printf(" -- > Now loop over all objects in inverse order < -- \n");
50  for( readObj = size-1; (int)readObj >= 0; readObj.SkipObjects(-1))
51  {
52  nextObject = *readObj;
53  if (nextObject) {
54  printf(" Object \"%s\" of class \"%s\" written with TKey \"%s\" has been read from file\n"
55  ,nextObject->GetName()
56  , nextObject->IsA()->GetName()
57  ,(const char *)readObj
58  );
59  delete nextObject;
60  } else {
61  printf("Error reading file by index\n");
62  }
63  }
64 //-----------------------------------------------------------------------
65 // Loop over the objects starting from the object with the key name "event.02.01"
66  printf(" -- > Loop over the objects starting from the object with the key name \"event.02.01\" < -- \n");
67  for( readObj = "event.02.01"; (const char *)readObj != 0; readObj.SkipObjects()){
68  nextObject = *readObj;
69  printf(" Object \"%s\" of class \"%s\" written with Tkey \"%s\" has been read from file\n"
70  , nextObject->GetName()
71  , nextObject->IsA()->GetName()
72  , (const char *)readObj
73  );
74  delete nextObject;
75  }
76 // Loop over the objects starting from the object with the key name "event.02.01"
77  printf(" -- > Loop over the objects starting from the 86-th object < -- \n");
78  for( readObj = (const char *)(readObj = 86); (const char *)readObj != 0; readObj.SkipObjects()){
79  nextObject = *readObj;
80  printf(" Object \"%s\" of class \"%s\" written with Tkey \"%s\" has been read from file\n"
81  , nextObject->GetName()
82  , nextObject->IsA()->GetName()
83  , (const char *)readObj
84  );
85  delete nextObject;
86  }
87 
88 }
virtual Int_t NextEventPut(TObject *obj, UInt_t eventNum, UInt_t runNumber, const char *name=0)
Create a special TKey name with obj provided and write it out.
Definition: TFileIter.cxx:515