//*-- Author : Jochen Markert 18.07.2007 #include "hflexfiller.h" #include "hades.h" #include "haddef.h" #include "hruntimedb.h" #include "hevent.h" #include "hcategorymanager.h" #include "htool.h" #include using namespace std; //_HADES_CLASS_DESCRIPTION //////////////////////////////////////////////////////////////////////////////// // // // HHistMap // helper class to hold a map of histogram pointers // // HHistMap hM("test.root"); // // hM.addHist(new TH1F("h2","h2",100,0,100),"hitst/h2"); // single hist // // TH1F* ht[6]; // for(Int_t sector=0;sector<6;sector++){ // ht[sector]=new TH1F(Form("ht_s%i",sector),Form("ht_s%i",sector),100,0,100); // } // // // create a linear array for 2dim indexing // TH1F* hd[24]; // 6x4 like hd[6][4] // for(Int_t sector=0;sector<6;sector++){ // for(Int_t module=0;module<4;module++){ // hd[HTool::getLinearIndex(sector,6,module,4)]= new TH1F(Form("hd_s%i_m%i",sector,module),Form("hd_s%i_m%i",sector,module),100,0,100); // } // } // // // hM.addHistArray(ht,"ht","hists/ht",6); // 1dim array size 6 // hM.addHistArray(hd,"hd","hists/hd",6,4); // 2dim array size 6x4 // // // hM.printMap(); // // hM.get("h2") ->FillRandom("gaus",1000); // single // hM.get("ht",1)->FillRandom("gaus",1000); // 1dim // // hM.get("hd",1,0)->FillRandom("gaus",1000);// 2dim // // // hM.writeHists(); // //////////////////////////////////////////////////////////////////////////////// ClassImp(HHistMap) HHistMap::HHistMap(TString outputname) { output=NULL; setOutputFile(outputname); } HHistMap::~HHistMap() { for(map< TString, TObjArray*>::iterator iter = hM.begin(); iter != hM.end(); ++iter ) { if((*iter).second ) delete (*iter).second; } for(map< TString, TArrayI*>::iterator iter = hD.begin(); iter != hD.end(); ++iter ) { if((*iter).second ) delete (*iter).second; } }; Bool_t HHistMap::setOutputFile(TString name) { // set the output file for the histograms // already existing file will be overwritten if(name!=""){ if(!output){ output = new TFile(name.Data(),"RECREATE"); } else { Error("setOutputFile()","Already other file opened! will be ignored..."); return kFALSE; } } else { Error("setOutputFile()","Name of file not specified! will be ignored...."); return kFALSE; } return kTRUE; } void HHistMap::printMap() { // print he list of available histtograms Int_t ct = 0; cout<<"------------------------------------------------------"<::iterator iter = hM.begin(); iter != hM.end(); ++iter ) { ct++; TString classname = "unused"; if((*iter).second ) classname = (*iter).second->At(0)->ClassName(); Int_t size = (*iter).second->GetEntries(); Int_t dim = hD[(*iter).first]->GetSize(); cout <GetName(); h->SetDirectory(0); map::iterator iter = hM.find(name); if( iter == hM.end() ){ // new variable TObjArray* a = new TObjArray(); a->Add(h); TArrayI* dim = new TArrayI(1); dim->Reset(0); // no array hM [name] = a; hD [name] = dim; hDir[name] = dir; } else { // exists already Error("addHist()","hist with name \"%s\" has been used already!",name.Data()); return kFALSE; } return kTRUE; } Bool_t HHistMap::addHistArray(TH1* h[],TString name,TString dir,Int_t i1max,Int_t i2max,Int_t i3max,Int_t i4max,Int_t i5max) { // add and array of histograms. the size of the array and // a lable "name" to access the array later has to be specified // Only 1-dim arrays are supported. For more dimensional histogram // arrays (up to 5 indices) one should use linearized versions // using index made by HTool::getLinearIndex(....) // i1max to i5max give the size in the dimension // the histograms can later be retrieved by get("name",i1,i2....) if(!h[0]) { Error("addHistArray()","hist pointer = NULL!"); return kFALSE; } if(i1max < 0) { Error("addHistArray()","array size < 0!"); return kFALSE; } if(name == "") { Error("addHistArray()","name is empty < 0!"); return kFALSE; } Int_t dim = -1; if (i1max >= 0 && i2max < 0 && i3max < 0 && i4max < 0 && i5max < 0 ) dim = 1; else if(i1max >= 0 && i2max >= 0 && i3max < 0 && i4max < 0 && i5max < 0 ) dim = 2; else if(i1max >= 0 && i2max >= 0 && i3max >= 0 && i4max < 0 && i5max < 0 ) dim = 3; else if(i1max >= 0 && i2max >= 0 && i3max >= 0 && i4max >= 0 && i5max < 0 ) dim = 4; else if(i1max >= 0 && i2max >= 0 && i3max >= 0 && i4max >= 0 && i5max >= 0 ) dim = 5; if(dim<0) { Error("addHistArray()","problem with dimension for hist %s : ind %i %i %i %i %i !",name.Data(),i1max,i2max,i3max,i4max,i5max); return kFALSE; } Int_t size = 0; if (dim == 1) size = i1max; else if(dim == 2) size = i1max*i2max; else if(dim == 3) size = i1max*i2max*i3max; else if(dim == 4) size = i1max*i2max*i3max*i4max; else if(dim == 5) size = i1max*i2max*i3max*i4max*i5max; map::iterator iter = hM.find(name); if( iter == hM.end() ){ // new variable TObjArray* a = new TObjArray(size); for(Int_t i=0;iSetDirectory(0); else { Error("addHistArray()","hist pointer = NULL for index = %i!",i); } a->Add(h[i]); } TArrayI* d = new TArrayI(dim); if(dim >= 1) (*d)[0] = i1max; if(dim >= 2) (*d)[1] = i2max; if(dim >= 3) (*d)[2] = i3max; if(dim >= 4) (*d)[3] = i4max; if(dim >= 5) (*d)[4] = i5max; hM [name] = a; hD [name] = d; hDir[name] = dir; } else { // exists already Error("addHistArray()","hist with name \"%s\" has been used already!",name.Data()); return kFALSE; } return kTRUE; } TH1* HHistMap::get(TString name,Int_t i1,Int_t i2,Int_t i3,Int_t i4,Int_t i5) { // get histogram from map. print error and map // if not existing. the map has to be set before // If the requested object is an array of histograms // the index has to specified. map::iterator iter = hM.find(name.Data()); if( iter != hM.end() ) { TArrayI* ar = hD[(*iter).first]; Int_t s = ar->GetSize(); Int_t index = 0 ; if (s == 1 && ar->At(0) > 0 ) index = i1; else if(s == 2 ) index = HTool::getLinearIndex(i1,ar->At(0),i2,ar->At(1)); else if(s == 3 ) index = HTool::getLinearIndex(i1,ar->At(0),i2,ar->At(1),i3,ar->At(2)); else if(s == 4 ) index = HTool::getLinearIndex(i1,ar->At(0),i2,ar->At(1),i3,ar->At(2),i4,ar->At(3)); else if(s == 5 ) index = HTool::getLinearIndex(i1,ar->At(0),i2,ar->At(1),i4,ar->At(3),i5,ar->At(4)); if(index<0) { Error("get()","Retrieved NULL pointer for %s , index = ind %i %i %i %i %i !",name.Data(),i1,i2,i3,i4,i5); return 0; } return (TH1*)(*iter).second->At(index) ; } else { printMap(); Error("get(), No matching histogram found for name = %s",name.Data()); } return 0; } Bool_t HHistMap::writeHists() { // write he histograms to the output file // the histograms will be stored in subdirs as // specified in addHist(....) or in the main // directory if no dir is specified if(output) output->cd(); else { Error("writeHists()","No file opened! will be ignored..."); return kFALSE; } for(map< TString, TObjArray*>::iterator iter = hM.begin(); iter != hM.end(); ++iter ) { output->cd(); if(hDir[(*iter).first] != "") HTool::changeToDir(hDir[(*iter).first]); if((*iter).second ) (*iter).second->Write(); } output->Save(); output->Close(); return kTRUE; }