StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestFileIter.C
1 // $Id: TestFileIter.C,v 1.1 2004/10/06 16:45:37 fisyak Exp $
2 // Author: Valery Fine(fine@bnl.gov) 01/03/2001
3 //
5 // This macros tests the various methods of TFileIter class.
7 //
8 // Copyright(c) 2001 [BNL] Brookhaven National Laboratory, Valeri Fine (fine@bnl.gov). All right reserved",
9 //
10 // Permission to use, copy, modify and distribute this software and its
11 // documentation for any purpose is hereby granted without fee,
12 // provided that the above copyright notice appear in all copies and
13 // that both that copyright notice and this permission notice appear
14 // in supporting documentation. The author makes no
15 // representations about the suitability of this software for any
16 // purpose. It is provided "as is" without express or implied warranty.
18 
19 void TestFileIter(){
20 // This macros tests the various methods of TFileIter class.
21  gSystem->Load("libTable");
22 
23  //First create simple ROOT file
24  TDataSet *ds = new TDataSet("event");
25  TObject *nextObject = 0;
26  TRandom run;
27  TRandom event;
28  {
29  TFileIter *outSet = new TFileIter("test.root","RECREATE");
30  UInt_t totalEvent = 4;
31  UInt_t runNumber = 20010301;
32  Int_t i=0;
33  Int_t j=0;
34  for (;j < 3;j++) {
35  for (i = 1;i<totalEvent;i++) {
36  outSet->NextEventPut(ds,UInt_t(i),UInt_t(runNumber+j+10*run.Rndm()-5));
37  }
38  }
39  delete outSet;
40  }
41  printf(" ----------------------> TFile has been created <--------------------\n");
42  TFile *f = new TFile("test.root");
43  TFileIter readObj(f);
44  // the number of the object available directly from "MyDataSet.root"
45  Int_t size = readObj.TotalKeys();
46  printf(" The total number of the objects: %d\n",size);
47 
48  //-----------------------------------------------------------------------
49  // Loop over all objects, read them in to memory one by one
50 
51  printf(" -- > Loop over all objects, read them in to memory one by one < -- \n");
52  printf(" -- > Use the second iterator that with the pace twice as large as the first one < -- \n");
53  {
54  TFileIter secondIter = readObj;
55  for( readObj = 0; int(readObj) < size; ++readObj,secondIter += 2)
56  {
57  nextObject = *readObj;
58  printf(" -- %d bytes of the object \"%s\" of class \"%s\" written with TKey \"%s\" has been read from file\n"
59  ,readObj.GetObjlen()
60  ,nextObject->GetName()
61  ,nextObject->IsA()->GetName()
62  ,(const char *)readObj
63  );
64  delete nextObject; nextObject = 0;
65  nextObject = *secondIter;
66  if (nextObject) {
67  printf(" %d bytes of the object \"%s\" of class \"%s\" written with TKey \"%s\" has been read from file\n\n"
68  ,secondIter.GetObjlen()
69  ,nextObject->GetName()
70  ,nextObject->IsA()->GetName()
71  ,(const char *)secondIter
72  );
73  delete nextObject;
74  }
75  }
76  };
77  printf(" Leaving the scope to destroy the stack allocated secondIter\n");
78 
79 //-----------------------------------------------------------------------
80 // Now loop over all objects in inverse order
81  printf(" -- > Now loop over all objects in inverse order < -- \n");
82  for( readObj = size-1; (int)readObj >= 0; --readObj)
83  {
84  nextObject = *readObj;
85  if (nextObject) {
86  printf(" Object \"%s\" of class \"%s\" written with TKey \"%s\" has been read from file\n"
87  ,nextObject->GetName()
88  , nextObject->IsA()->GetName()
89  ,(const char *)readObj
90  );
91  delete nextObject;
92  } else {
93  printf("Error reading file by index\n");
94  }
95  }
96 //-----------------------------------------------------------------------
97 // Loop over the objects starting from the object with the key name "event.02.01"
98  printf(" -- > Loop over the objects starting from the object with the key name \"event.02.01\" < -- \n");
99  for( readObj = "event.02.01"; (const char *)readObj != 0; ++readObj){
100  nextObject = *readObj;
101  printf(" Object \"%s\" of class \"%s\" written with Tkey \"%s\" has been read from file\n"
102  , nextObject->GetName()
103  , nextObject->IsA()->GetName()
104  , (const char *)readObj
105  );
106  delete nextObject;
107  }
108 // Loop over the objects starting from the object with the key name "event.02.01"
109  printf(" -- > Loop over the objects starting from the 86-th object < -- \n");
110  for( readObj = (const char *)(readObj = 86); (const char *)readObj != 0; ++readObj){
111  nextObject = *readObj;
112  printf(" Object \"%s\" of class \"%s\" written with Tkey \"%s\" has been read from file\n"
113  , nextObject->GetName()
114  , nextObject->IsA()->GetName()
115  , (const char *)readObj
116  );
117  delete nextObject;
118  }
119  // Check file name mapping facility.
120  printf(" -- > Check file name mapping facility. < -- \n");
121  // creat the "Map resource file"
122  ofstream resource;
123  resource.open(TFileIter::GetDefaultMapFileName());
124  resource << TFileIter::GetLocalFileNameKey()<< " /hpss/test" << endl;
125  resource << TFileIter::GetForeignFileSystemKey() << " test" << endl;
126  resource.close();
127  // Check mapping
128  TFileIter testMapping("/hpss/test.root");
129  if (testMapping.TotalKeys() == readObj.TotalKeys()) {
130  printf(" -- > the fake file \"/hpss/test.root\" has been mapped to the real file \"test.root\"\n");
131  } else {
132  printf(" -- > the fake file \"/hpss/test.root\" failed to be mapped to the real file \"test.root\"\n");
133  }
134 
135 }
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