]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/TTreeStream.cxx
Fix fixed-string length bug
[u/mrichter/AliRoot.git] / STEER / TTreeStream.cxx
index 7904ef5ab492a75eeafd818610bcd0838124525d..2287454cc2d547bb73d2acd071c97f16970298bd 100644 (file)
@@ -1,29 +1,46 @@
-#include "TObjArray.h"
-#include "TFile.h"
-#include "TTree.h"
-#include "TBrowser.h"
-#include "TTreeStream.h"
-
+/**************************************************************************
+ * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ *                                                                        *
+ * Author: The ALICE Off-line Project.                                    *
+ * Contributors are mentioned in the code where appropriate.              *
+ *                                                                        *
+ * Permission to use, copy, modify and distribute this software and its   *
+ * documentation strictly for non-commercial purposes is hereby granted   *
+ * without fee, provided that the above copyright notice appears in all   *
+ * copies and that both the copyright notice and this permission notice   *
+ * appear in the supporting documentation. The authors make no claims     *
+ * about the suitability of this software for any purpose. It is          *
+ * provided "as is" without express or implied warranty.                  *
+ **************************************************************************/
+
+/* $Id$ */
+
+//
+//  marian.ivanov@cern.ch
+//
+//  ------------------------------------------------------------------------------------------------
+//  TTreeStream
+//  Standard stream (cout) like input for the tree
+//  Run and see TTreeStreamer::Test() - to see TTreeStreamer functionality
+//  ------------------------------------------------------------------------------------------------  
+//
+//  -------------------------------------------------------------------------------------------------
+//  TTreeSRedirector
+//  Redirect file to  different TTreeStreams  
+//  Run and see   TTreeSRedirector::Test() as an example of TTreeSRedirectorer functionality 
+// 
+
+#include <TClass.h>
+#include <TFile.h>
+#include <TDirectory.h>
+#include <TObjArray.h>
+#include <TTree.h>
+#include <TTreeStream.h>
 
 ClassImp(TTreeDataElement)
 ClassImp(TTreeStream)
 ClassImp(TTreeSRedirector)
 
-/*
-  marian.ivanov@cern.ch
-  //
-  ------------------------------------------------------------------------------------------------
-  TTreeStream
-  Standard stream (cout) like input for the tree
-  Run and see TTreeStreamer::Test() - to see TTreeStreamer functionality
-  ------------------------------------------------------------------------------------------------  
-  //
-  -------------------------------------------------------------------------------------------------
-  TTreeSRedirector
-  Redirect file to  different TTreeStreams  
-  Run and see   TTreeSRedirector::Test() as an example of TTreeSRedirectorer functionality 
-  // 
-*/
 
 
 void TTreeStream::Test()
@@ -143,6 +160,17 @@ TTreeSRedirector::~TTreeSRedirector()
   fFile->Close();
   delete fFile;
 }
+void TTreeSRedirector::StoreObject(TObject* object){
+  //
+  //
+  //
+  TDirectory * backup = gDirectory;
+  fFile->cd();
+  object->Write();
+  if (backup) backup->cd();
+}
+
+
 
 TTreeStream  & TTreeSRedirector::operator<<(Int_t id)
 {
@@ -161,12 +189,14 @@ TTreeStream  & TTreeSRedirector::operator<<(Int_t id)
     }
   }
   if (!clayout){
+    TDirectory * backup = gDirectory;
     fFile->cd();
     char chname[100];
     sprintf(chname,"Tree%d",id);
     clayout = new TTreeStream(chname);
     clayout->fId=id;
     fDataLayouts->AddAt(clayout,entries);
+    if (backup) backup->cd();
   }
   return *clayout;
 }
@@ -182,11 +212,13 @@ TTreeStream  & TTreeSRedirector::operator<<(const char* name)
   Int_t entries = fDataLayouts->GetEntriesFast();
 
   if (!clayout){
+    TDirectory * backup = gDirectory;
     fFile->cd();
     clayout = new TTreeStream(name);
     clayout->fId=-1;
     clayout->SetName(name);
     fDataLayouts->AddAt(clayout,entries);    
+    if (backup) backup->cd();
   }
   return *clayout;
 }
@@ -197,7 +229,7 @@ TTreeStream  & TTreeSRedirector::operator<<(const char* name)
 void TTreeSRedirector::Close(){
   //
   //
-  TFile * backup = gFile;
+  TDirectory * backup = gDirectory;
   fFile->cd();
   if (fDataLayouts){
     Int_t entries = fDataLayouts->GetEntriesFast();
@@ -210,14 +242,14 @@ void TTreeSRedirector::Close(){
     delete fDataLayouts;
     fDataLayouts=0;
   }
-  backup->cd();
+  if (backup) backup->cd();
 }
 
 
 
 //-------------------------------------------------------------
 TTreeDataElement:: TTreeDataElement(Char_t type) :
-  fName(),
+  TNamed(),
   fType(type),
   fDType(0),
   fClass(0),
@@ -229,8 +261,8 @@ TTreeDataElement:: TTreeDataElement(Char_t type) :
 }
 
 TTreeDataElement:: TTreeDataElement(TDataType* type) :
-  fName(),
-  fType(' '),
+  TNamed(),
+  fType(0),
   fDType(type),
   fClass(0),
   fPointer(0)
@@ -241,8 +273,8 @@ TTreeDataElement:: TTreeDataElement(TDataType* type) :
 }
 
 TTreeDataElement:: TTreeDataElement(TClass* cl) :
-  fName(),
-  fType(' '),
+  TNamed(),
+  fType(0),
   fDType(0),
   fClass(cl),
   fPointer(0)
@@ -259,14 +291,21 @@ TTreeStream::TTreeStream(const char *treename):
   fBranches(0),
   fTree(new TTree(treename, treename)),
   fCurrentIndex(0),
+  fId(0),
   fNextName(),
   fNextNameCounter(),
   fStatus(0)
 {
+  //
+  // Standard ctor
+  //
 }
 
 TTreeStream::~TTreeStream()
 {
+  //
+  // Class dtor
+  //
   fElements->Delete();
   fBranches->Clear();
   delete fElements;
@@ -275,12 +314,17 @@ TTreeStream::~TTreeStream()
 
 void TTreeStream::Close()
 {
+  //
+  // Flush data to disk and close
   //
   fTree->Write();
 }
 
 Int_t TTreeStream::CheckIn(Char_t type, void *pointer)
 {
+  //
+  // Insert object of given type
+  //
   if (!fElements) fElements = new TObjArray(1000);
   TTreeDataElement* element = (TTreeDataElement*)fElements->At(fCurrentIndex);
   if (!element) {
@@ -316,6 +360,7 @@ Int_t TTreeStream::CheckIn(Char_t type, void *pointer)
 
 Int_t TTreeStream::CheckIn(TObject *o){
   //
+  // Insert TObject
   //
   if (!o) return 0;
   if (!fElements) fElements = new TObjArray(1000);
@@ -353,9 +398,10 @@ Int_t TTreeStream::CheckIn(TObject *o){
 
 void TTreeStream::BuildTree(){
   //
+  // Build the Tree
   //
-  if (fTree->GetEntries()>0) return;
-  fTree = new TTree(GetName(),GetName());
+  if (fTree && fTree->GetEntries()>0) return;
+  if (!fTree)  fTree = new TTree(GetName(),GetName());
   Int_t entries = fElements->GetEntriesFast();  
   fBranches = new TObjArray(entries);
   
@@ -371,7 +417,7 @@ void TTreeStream::BuildTree(){
     }
     if (element->fClass){
       if (element->fClass->GetBaseClass("TClonesArray")){
-       TBranch * br = fTree->Branch(bname1,&(element->fPointer));
+       TBranch * br = fTree->Branch(bname1,element->fClass->GetName(),&(element->fPointer));
        fBranches->AddAt(br,i);
       }else
        {
@@ -390,6 +436,7 @@ void TTreeStream::BuildTree(){
 
 void TTreeStream::Fill(){
   //
+  // Fill the tree
   //
   if (fTree) { 
     Int_t entries=fElements->GetEntriesFast();
@@ -408,7 +455,11 @@ void TTreeStream::Fill(){
   }
 }
 
-TTreeStream & TTreeStream::Endl(){
+TTreeStream & TTreeStream::Endl()
+{
+  //
+  // Perform pseudo endl operation
+  //
   if (fTree->GetNbranches()==0) BuildTree();
   Fill();
   fStatus =0;
@@ -420,9 +471,8 @@ TTreeStream & TTreeStream::Endl(){
 TTreeStream  &TTreeStream::operator<<(Char_t *name)
 {
   //
+  // Endl 
   //
-  //
-  //Endl 
   if (name[0]=='\n'){
     return Endl();
   }