2 // marian.ivanov@cern.ch
4 // ------------------------------------------------------------------------------------------------
6 // Standard stream (cout) like input for the tree
7 // Run and see TTreeStreamer::Test() - to see TTreeStreamer functionality
8 // ------------------------------------------------------------------------------------------------
10 // -------------------------------------------------------------------------------------------------
12 // Redirect file to different TTreeStreams
13 // Run and see TTreeSRedirector::Test() as an example of TTreeSRedirectorer functionality
19 #include "TObjArray.h"
21 #include "TTreeStream.h"
24 ClassImp(TTreeDataElement)
26 ClassImp(TTreeSRedirector)
30 void TTreeStream::Test()
34 TFile *ftest = new TFile("teststreamer.root","recreate");
35 if (!ftest) ftest = new TFile("teststreamer.root","new");
37 //create to streems Tree1 and Tree2
38 TTreeStream stream1("Tree1");
39 TTreeStream stream2("Tree2");
44 TObject *po = new TObject;
45 TObject *po2 = new TObject;
46 for (Int_t i=0;i<100000;i++) {
49 po2->SetUniqueID(i*100);
53 // The data layout of stream is defined during first invocation of streamer.
54 // Endl is the trigger which define the end of structure.
56 // The name of branch can be specified using strings with = at the the end
57 // if string is not specified automatic convention is u (sed B0, B1, ...Bn)
58 stream1<<"i="<<i<<"ch="<<ch<<"f="<<f<<"po="<<po<<"\n";
61 //3.) just another example - we can fill the same tree with different objects
64 stream2<<f2<<po2<<"\n";
67 //4.) Close the streeamers (Write the streamed tree's to the file) and close the corresponding file.
74 //5.) and now see results in file tteststreamer.root
79 void TTreeSRedirector::Test()
82 //Example test function to show functionality of TTreeSRedirector
85 //1.)create the redirector associated with file (testredirector.root)
88 TTreeSRedirector *pmistream= new TTreeSRedirector("testredirector.root");
89 TTreeSRedirector &mistream = *pmistream;
93 TObject *po = new TObject;
94 TObject *po2 = new TObject;
95 for (Int_t i=0;i<100000;i++) {
98 po2->SetUniqueID(i*100);
101 //2.) create the tree with identifier specified by first argument
102 // layout specified by sequence of arguments
103 // Tree identifier has to be specified as first argument !!!
104 // if the tree and layout was already defined the consistency if layout is checked
105 // if the data are consisten fill given tree
106 // the name of branch can be specified using strings with = at the the end
107 // if string is not specified use automatic convention B0, B1, ...Bn
108 mistream<<"TreeIdentifier"<<"i="<<i<<"ch="<<ch<<"f="<<f<<"po="<<po<<"\n";
112 //3.) just another example - we can fill the same tree with different objects
114 mistream<<"TreeK"<<f<<po<<"\n";
115 mistream<<"TreeK"<<f2<<po2<<"\n";
118 //4.) write the streamed tree's to the file and close the corresponding file in destructor
122 //5.) and now see results in file testredirector.root
126 TTreeSRedirector::TTreeSRedirector(const char *fname) :
127 fFile(new TFile(fname,"recreate")),
134 fFile = new TFile(fname,"new");
138 TTreeSRedirector::~TTreeSRedirector()
143 Close(); //write the tree to the selected file
148 TTreeStream & TTreeSRedirector::operator<<(Int_t id)
151 // return reference to the data layout with given identifier
152 // if not existing - creates new
153 if (!fDataLayouts) fDataLayouts = new TObjArray(10000);
154 TTreeStream *clayout=0;
155 Int_t entries = fDataLayouts->GetEntriesFast();
156 for (Int_t i=0;i<entries;i++){
157 TTreeStream * layout = (TTreeStream*)fDataLayouts->At(i);
158 if (!layout) continue;
159 if (layout->fId==id) {
167 sprintf(chname,"Tree%d",id);
168 clayout = new TTreeStream(chname);
170 fDataLayouts->AddAt(clayout,entries);
176 TTreeStream & TTreeSRedirector::operator<<(const char* name)
179 // return reference to the data layout with given identifier
180 // if not existing - creates new
181 if (!fDataLayouts) fDataLayouts = new TObjArray(10000);
182 TTreeStream *clayout=(TTreeStream*)fDataLayouts->FindObject(name);
183 Int_t entries = fDataLayouts->GetEntriesFast();
187 clayout = new TTreeStream(name);
189 clayout->SetName(name);
190 fDataLayouts->AddAt(clayout,entries);
198 void TTreeSRedirector::Close(){
201 TFile * backup = gFile;
204 Int_t entries = fDataLayouts->GetEntriesFast();
205 for (Int_t i=0;i<entries;i++){
206 TTreeStream * layout = (TTreeStream*)fDataLayouts->At(i);
208 if (layout->fTree) layout->fTree->Write(layout->GetName());
214 if (backup) backup->cd();
219 //-------------------------------------------------------------
220 TTreeDataElement:: TTreeDataElement(Char_t type) :
232 TTreeDataElement:: TTreeDataElement(TDataType* type) :
244 TTreeDataElement:: TTreeDataElement(TClass* cl) :
256 //-------------------------------------------------------------------
257 TTreeStream::TTreeStream(const char *treename):
258 TNamed(treename,treename),
261 fTree(new TTree(treename, treename)),
272 TTreeStream::~TTreeStream()
283 void TTreeStream::Close()
286 // Flush data to disk and close
291 Int_t TTreeStream::CheckIn(Char_t type, void *pointer)
294 // Insert object of given type
296 if (!fElements) fElements = new TObjArray(1000);
297 TTreeDataElement* element = (TTreeDataElement*)fElements->At(fCurrentIndex);
299 element = new TTreeDataElement(type);
302 if (fNextName.Length()>0){
303 if (fNextNameCounter==0){
304 sprintf(name,"%s",(const char*)fNextName);
306 if (fNextNameCounter>0){
307 sprintf(name,"%s%d",(const char*)fNextName,fNextNameCounter);
311 sprintf(name,"B%d.",fCurrentIndex);
313 element->SetName(name);
315 element->SetPointer(pointer);
316 fElements->AddAt(element,fCurrentIndex);
318 return 0; //new element added
320 if (element->GetType()!=type){
322 return 1; //mismatched data element
324 element->SetPointer(pointer);
329 Int_t TTreeStream::CheckIn(TObject *o){
334 if (!fElements) fElements = new TObjArray(1000);
335 TTreeDataElement* element = (TTreeDataElement*)fElements->At(fCurrentIndex);
337 element = new TTreeDataElement(o->IsA());
340 if (fNextName.Length()>0){
341 if (fNextNameCounter==0){
342 sprintf(name,"%s",(const char*)fNextName);
344 if (fNextNameCounter>0){
345 sprintf(name,"%s%d",(const char*)fNextName,fNextNameCounter);
349 sprintf(name,"B%d",fCurrentIndex);
351 element->SetName(name);
353 element->SetPointer(o);
354 fElements->AddAt(element,fCurrentIndex);
356 return 0; //new element added
358 if (element->fClass!=o->IsA()){
360 return 1; //mismatched data element
362 element->SetPointer(o);
367 void TTreeStream::BuildTree(){
371 if (fTree->GetEntries()>0) return;
372 fTree = new TTree(GetName(),GetName());
373 Int_t entries = fElements->GetEntriesFast();
374 fBranches = new TObjArray(entries);
376 for (Int_t i=0;i<entries;i++){
378 TTreeDataElement* element = (TTreeDataElement*)fElements->At(i);
380 if (element->GetName()[0]==0){
381 sprintf(bname1,"B%d",i);
384 sprintf(bname1,element->GetName());
386 if (element->fClass){
387 if (element->fClass->GetBaseClass("TClonesArray")){
388 TBranch * br = fTree->Branch(bname1,element->fClass->GetName(),&(element->fPointer));
389 fBranches->AddAt(br,i);
392 TBranch * br = fTree->Branch(bname1,element->fClass->GetName(),&(element->fPointer));
393 fBranches->AddAt(br,i);
396 if (element->GetType()>0){
398 sprintf(bname2,"B%d/%c",i,element->GetType());
399 TBranch * br = fTree->Branch(bname1,element->fPointer,bname2);
400 fBranches->AddAt(br,i);
405 void TTreeStream::Fill(){
410 Int_t entries=fElements->GetEntriesFast();
411 if (entries>fTree->GetNbranches()) BuildTree();
412 for (Int_t i=0;i<entries;i++){
413 TTreeDataElement* el = (TTreeDataElement*)fElements->At(i);
415 if (!el->GetType()) continue;
416 TBranch * br = (TBranch*)fBranches->At(i);
418 if (el->GetType()) br->SetAddress(el->fPointer);
421 if (fStatus==0) fTree->Fill(); //fill only in case of non conflicts
426 TTreeStream & TTreeStream::Endl()
429 // Perform pseudo endl operation
431 if (fTree->GetNbranches()==0) BuildTree();
439 TTreeStream &TTreeStream::operator<<(Char_t *name)
448 //if tree was already defined ignore
449 if (fTree->GetEntries()>0) return *this;
450 //check branch name if tree was not
453 for (last=0;;last++){
454 if (name[last]==0) break;
457 if (last>0&&name[last-1]=='='){