#include "TMap.h" #include "TVectorD.h" #include "TTree.h" #include "TFile.h" #include "TRandom3.h" #include "TObject.h" #include "Rtypes.h" #include const double keyvals[] = {12,5,100}; class MyKey : public TObject, public TArrayD { ClassDef(MyKey, 1); public: // inline MyKey() {values = TArrayD();}; MyKey(TArrayD arr) : TArrayD(arr) {}; MyKey(void) : TArrayD(0) {}; ULong_t Hash() const { TString values; for(int i = 0;i < this->GetSize();i++) { values.Append(TString::Format("%e,",this->operator[](i))); } return values.Hash(); }; }; void writetree(void) { TFile outfile("outfile.root","RECREATE"); outfile.cd(); TTree * outtree = new TTree("outtree","Test tree with TMap branch"); TMap * map = new TMap(2800); map->SetOwnerKeyValue(kTRUE,kTRUE); outtree->Branch("map","TMap",&map,32000,0); TRandom3 tr; TArrayD arr(3,keyvals); for(int j = 0;j<10;j++) { map->DeleteAll(); MyKey * key = new MyKey(arr); TVectorD * value = new TVectorD(10); for(int i = 0;i<10;i++) (*value)[i] = tr.Gaus(); map->Add(key,value); std::cout << "Key hash value when writing: " << key->Hash() << std::endl; outtree->Fill(); } outfile.cd(); gDirectory->Write(); outfile.Close(); } void readtree(void) { TFile infile("outfile.root","READ"); infile.cd(); TTree * intree = (TTree*) infile.Get("outtree"); TMap * map = new TMap(2800); intree->SetBranchAddress("map",&map); TArrayD arr(3,keyvals); MyKey * key = new MyKey(arr); std::cout << "Key hash value when reading: " << key->Hash() << std::endl; for(int j = 0;jGetEntries();j++) { intree->GetEntry(j); TVectorD * value = (TVectorD*) map->GetValue(key); if(! value) std::cout << "Could not get value for key"; else std::cout << "Found value for key"; key->Print(); std::cout << "TMap printout: "; map->Print(); std::cout << std::endl; } } void maptest(void) { writetree(); readtree(); }