//*-- AUTHOR : J. Markert //_HADES_CLASS_DESCRIPTION //////////////////////////////////////////////////////////////////////////// // HTool // Tool Class //////////////////////////////////////////////////////////////////////////// using namespace std; #include #include #include #include #include #include // for backtrace #include #include "htool.h" #include "TDirectory.h" #include "TFile.h" #include "TString.h" #include "TObjString.h" #include "TList.h" #include "TROOT.h" #include "TObject.h" #include "TKey.h" #include "TDataMember.h" #include "TAxis.h" #include "TBaseClass.h" #include "TTree.h" #include "TNtuple.h" #include "TTreeFormula.h" #include "TLeaf.h" #include "TGraphErrors.h" #include "TProfile.h" #include "TSystem.h" #include "TMatrixD.h" #include "TMath.h" ClassImp(HTool) HTool::HTool(const Char_t* name,const Char_t* title) : TNamed(name,title) { // constructor for HTool initVariables(); } HTool::~HTool() { // destructor of HTool } void HTool::initVariables() { // inits all variables } //--------------------------Files--------------------------------------- Bool_t HTool::open(TFile** file,TString fName,TString option) { // Opens root file "fName" to the pointer "file" with option // New,Read,Recreate and Update. Checks if file exists or is // already opened. If everything works fine kTRUE is returned. if(*file) { // checking if file is opened if((*file)->IsOpen()){ cout<<"Error(HTool::open() , SPECIFIED INPUT FILE IS ALREADY OPEN!"<GetListOfFiles()->FindObject(fName.Data()); if (!*file){ *file= new TFile(fName.Data(),"READ"); cout<<"HTool::open() : Reading from "<GetListOfFiles()->FindObject(fName.Data()); if (!*file){ *file= new TFile(fName.Data(),"UPDATE"); cout<<"HTool::open() : Updating "<IsOpen()){ opt=(*file)->GetOption(); if(opt.CompareTo("READ")!=0)(*file)->Save(); (*file)->Close(); *file=0; return kTRUE; }else{ cout<<"Warning(HTool::close() , SPECIFIED FILE IS NOT OPEN!"<GetListOfFiles()->FindObject(fName.Data()); if (!*file){ *file= fopen(fName.Data(),"r"); cout<<"HTool::openAscii() : Reading from "<MakeIterator(); // while ((name = (TObjString*)file->Next())) // { // Char_t* input = name->GetString.Data(); ... // } // delete files; // } wordexp_t file_list; Char_t** file; TObjArray* filenames = NULL; if (pattern.IsNull()) return NULL; if (::wordexp( pattern.Data(), &file_list, 0 )) { ::wordfree( &file_list ); return NULL; } file = file_list.we_wordv; filenames = new TObjArray; for (UInt_t i = 0; i < file_list.we_wordc; i++) { // check if files real exist, since ::wordexp(3) returns the unexpanded // pattern, if e.g. the path does not exist if (!gSystem->AccessPathName( file[i] )) filenames->Add( new TObjString( file[i] ) ); } ::wordfree( &file_list ); if (filenames->GetEntries() == 0) { delete filenames; filenames = NULL; } return filenames; } Bool_t HTool::writeNtupleToAscii(TString Input , TString ntuple, TString Output, TString separator, TString selection, TString condition, Long64_t entries , Long64_t startEntry, Int_t ColWidth ) { // Util function for writing ntuple data to ascii file: // Input : Input File (*.root) // ntuple : name of ntuple in Input File // Output : Output ascii File // separator : data field separator (" " or "," or "\t" .....) // selection : comma separated list of leaf names ( like x,y,z,mom ...) // - if empty all leafs will be written (default) // - if list contains "-" all leafs but the listed ones will be written // condition : like Tree->Draw() selection. Only rows which fulfill the condition will be written // - if empty no condition is applied (default) // entries : number of entries which should be read // - if == -1 all entries are analyzied (default) // startEntry: first entry to strt in in ntuple // ColWidth : format width for the data field // - if ==0 no format is applied (default) if(gSystem->AccessPathName(Input.Data())){ cout<<"HTool::writeNtupleToAscii(): Error::InputFile: "<Get(ntuple.Data()); if(tuple==0){ cout<<"HTool::writeNtupleToAscii(): Error::Ntuple: "<GetListOfLeaves(); Int_t nvar=tuple->GetNvar(); if(entries<0) entries =tuple->GetEntries(); //------------------------------------------ TArrayI flag(nvar); flag.Reset(0); cout<<"selection --------------"<GetLast()+1; //------------------------------------------ //------------------------------------------ // create a list of flags for the // columns (variables) which should // be written out // flag=0 will be ignored if(nselect!=0) { // only if a selection was made for(Int_t i=0;iGetLast()+1;i++) { TLeaf* l=(TLeaf*)arr->At(i); for(Int_t j=0;jGetLast()+1;j++) { TObjString* obj=(TObjString*)useList->At(j); TString lable=obj->GetString(); lable.ReplaceAll(" ",""); if(lable.CompareTo(l->GetName())==0) { if(positivList) { // mark the leafs from the selection // as used flag[i]=1; } else { // mark the leafs from the selection // as not used flag[i]=0; } break; } } } } //------------------------------------------ //------------------------------------------ // open output stream for writing // ascii output ofstream out; out.open(Output.Data()); if(!out.is_open()) { cout<<"HTool::writeNtupleToAscii(): Error: Could not open Output File!"<At(i); if(written==0) { if(ColWidth<=0) out<GetName(); else out<GetName(); } else { if(ColWidth<=0) out<GetName(); else out<GetName(); } written++; } out<GetNdim()) { cout<<"HTool::writeNtupleToAscii(): TTreeFormular dimension == 0!"<GetEntry(lines); //------------------------------------------ // aply condition on the line if (select) { select->GetNdata(); if (select->EvalInstance(0) == 0) continue; } //------------------------------------------ Float_t* dat=tuple->GetArgs(); written=0; for(Int_t j=0;jDelete(); delete useList; //------------------------------------------ return kTRUE; } void HTool::createNtupleMap(TString infile, TString prefix, TString ntupleName, TString outfile) { // create all variables to map the ntuple in oufile. opens root file, retrieves // the ntuple pointer. sets all branch addresses. // "infile" = input root file containing the ntuple // "prefix" = this string will be prepended to the normal variable names (needed for multiple identical ntuples) // "ntupleName" = name of the ntuple in the root file // "outfile" = macro file which will be created. // this macro can be loaded via gROOT->LoadMaco(....) // + execute function as macro name (without .C) // TNtuple* ntuple = mymacro(); to get the ntuple pointer // in compiled code you have to use "#include mymacro.C" of the // before created macro and remove gROOT->LoadMaco(....) // // // // example : // // //-------------------------------- // // comment in if you want to compile // // and the macros exist already! // //#include "mymacro.C" // //#include "mymacro2.C" // //-------------------------------- // // // void testNtupleMap() // { // //-------------------------------- // comment out if you want to compile and // the macros are in the #include // // create the first macro for ntuple // HTool::createNtupleMap("myfile.root","test_" ,"myntuple","mymacro.C"); // HTool::createNtupleMap("myfile.root","test2_","myntuple","mymacro2.C"); // gROOT->LoadMacro("mymacro.C"); // gROOT->LoadMacro("mymacro.C"); // //------------------------------------- // // TNtuple* ntuple = mymacro (); // functions defined in marco // TNtuple* ntuple2 = mymacro2(); // functions defined in marco // // Int_t n = ntuple->GetEntries(); // Float_t* dat = ntuple->GetArgs(); // for(Int_t i = 0; i < n ; i ++){ // ntuple->GetEntry(i); // cout<GetEntries(); // for(Int_t i = 0; i < n2 ; i ++){ // ntuple2->GetEntry(i); // cout<Get(ntupleName.Data()); if(!ntuple) { cout<<"HTool::createNtupleMap(), Ntuple not found!"<GetListOfLeaves(); if(!listLeave) { cout<<"HTool::createNtupleMap(), Could not get list of leaves!"<GetLast() + 1; i ++){ TLeaf* leaf = (TLeaf*) listLeave->At(i); out<GetTypeName()<<" "<GetName())<<";"<BaseName(outfile); if(!func.Contains(".C")){ cout<<"HTool::createNtupleMap(), Macro Name does not contain .C!"<Get(\"%s\");",ntupleName.Data(),ntupleName.Data())<GetLast() + 1; i ++){ TLeaf* leaf = (TLeaf*) listLeave->At(i); TString tmp = Form("->SetBranchAddress(\"%s\",&%s%s);",leaf->GetName(),prefix.Data(),leaf->GetName()); out<Which("c++filt","/dev/null")==0) { // do nice print out using c++filt from gnu binutils TString symbol=strings[i]; TString lib =symbol; lib.Replace (lib.First('(') ,lib.Length()-lib.First('('),""); symbol.Replace(0 ,symbol.First('(')+1 ,""); symbol.Replace(symbol.First('+'),symbol.Length() ,""); cout<Exec(Form("c++filt %s",symbol.Data())); } else { // do normal print out cout<cd(); TTree* T = (TTree*)in->Get(treename.Data()); TObjArray* classarray = new TObjArray(); TString keyname; TString keyclassname; TString branch; TString histname; TString classname; TString varname; Int_t totBytes = 0; Int_t totzipBytes = 0; Int_t totSize = 0; TObjArray* a = T->GetListOfLeaves(); TString oldclass = ""; TString myclass; Int_t ct = 0; Int_t ctClassList = 0; for(Int_t j=0;jLastIndex()+1;j++) { TLeaf* l = (TLeaf*) a->At(j); TString myclass = l->GetName(); TBranch* b = l->GetBranch(); classname = b->GetClassName(); //---------------------------------------------------------------- myclass.Replace(myclass.First("."),myclass.Length()-myclass.First("."),""); if(classarray->FindObject(myclass.Data())){ continue; } if(useClassList) { if(HTool::findString(&myclassNames[0],numberclasses,myclass)) { classarray->Add(new TObjString(myclass)); cout<Add(new TObjString(myclass)); } } classarray->Expand(classarray->GetEntries()); cout<<"Number of Categories: "<GetEntries() <SetBottomMargin(0.25); cstat->SetLeftMargin(0.2); cstat->SetGridx(); cstat->SetGridy(); TH1F* hstat=new TH1F("hstat","hstat",classarray->GetEntries(),0,classarray->GetEntries()); hstat->SetYTitle(Form("disk space [%s]",label.Data())); for(Int_t i=0;iGetEntries();i++){ hstat->GetXaxis()->SetBinLabel(i+1,((TKey*)classarray->At(i))->GetName()); } hstat->GetXaxis()->LabelsOption("v"); hstat->GetYaxis()->SetTitleOffset(2); hstat->SetFillColor(4); hstat->SetDirectory(0); hstat->SetStats(kFALSE); //--------------------looping over categories to get all data members---------------------------- cout<<"-----------------------------------------------------------"<LastIndex()+1;j++) { TLeaf* l = (TLeaf*) a->At(j); TString myclass = l->GetName(); TBranch* b = l->GetBranch(); classname = b->GetClassName(); //---------------------------------------------------------------- myclass.Replace(myclass.First("."),myclass.Length()-myclass.First("."),""); if(myclass.CompareTo(oldclass.Data()) != 0 && oldclass != "") { if(oldclass.CompareTo("") == 0) keyname = myclass; else keyname = oldclass; //---------------------------------------------------------------- // print summed size of object if(totBytes) { cout<<"###########################################################"<SetBinContent(ctClassList+1,totzipBytes/factor); ctClassList++; } } else { cout<<" \n\t total size : "<SetBinContent(ct+1,totzipBytes/factor); } if(jLastIndex()+1){ totBytes = 0; totzipBytes = 0; totSize = 0; } } ct++; //---------------------------------------------------------------- } else { if(useClassList) { if(!HTool::findString(&myclassNames[0],numberclasses,myclass)) { continue; } } TBranch* b = l->GetBranch(); if(b){ totSize += b->GetTotalSize(); totBytes += b->GetTotBytes(); totzipBytes += b->GetZipBytes(); } else { cout<<"ZERO pointer retrieved for branch: "<GetName()<Integral()<Close(); delete classarray; hstat->Draw(); return kTRUE; } void HTool::printProgress(Int_t ct,Int_t total,Int_t step, TString comment ) { // print the progress of a loop in %, where ct is the loop // counter, total the total number of loops and step the // step size in precent for which the print should be done. if(total <= 0 ) { cout<<"Error::printProgress(), total is <=0 !"< 0 ) cout<<"\b\b\b\b\b"<= 100) cout<FindKey(sDir)) { dirNew = dirOld->mkdir(sDir); } else { dirNew=(TDirectory*)dirOld->Get(sDir); } dirOld->cd(sDir); return dirNew; } TDirectory* HTool::changeToDir(TString p) { // Changes into the given path. If the Directory is not existing // it will be created. The pointer to the new directory will be returned. TDirectory *dirNew=0; TString s1=p; Ssiz_t len=s1.Length(); if(len!=0) { Char_t* mystring=(Char_t*)s1.Data(); Char_t* buffer; TList myarguments; TObjString *stemp; TString argument; Int_t count=0; while(1) // find all token in option string and put them to a list { if(count==0) { buffer=strtok(mystring,"/"); stemp=new TObjString(buffer); myarguments.Add(stemp); } if(!(buffer=strtok(NULL,"/")))break; stemp=new TObjString(buffer); myarguments.Add(stemp); count++; } TIterator* myiter=myarguments.MakeIterator(); // iterate over the list of arguments while ((stemp=(TObjString*)myiter->Next())!= 0) { argument=stemp->GetString(); dirNew=HTool::Mkdir(gDirectory,argument.Data(),-99); } } return dirNew; } Bool_t HTool::checkDir(TString p,TFile* input) { // Checks if a given path "p" in file "input" exists. // The actual directory will not be changed. Returns // kTRUE if OK. TDirectory* dirSave=gDirectory; TDirectory *dirNew=input; TString s1=p; Ssiz_t len=s1.Length(); if(len!=0) { Char_t* mystring=(Char_t*)s1.Data(); Char_t* buffer; TList myarguments; TObjString *stemp; TString argument; Int_t count=0; while(1) // find all token in option string and put them to a list { if(count==0) { buffer=strtok(mystring,"/"); stemp=new TObjString(buffer); myarguments.Add(stemp); } if(!(buffer=strtok(NULL,"/")))break; stemp=new TObjString(buffer); myarguments.Add(stemp); count++; } TIterator* myiter=myarguments.MakeIterator(); // iterate over the list of arguments while ((stemp=(TObjString*)myiter->Next())!= 0) { argument=stemp->GetString(); if(dirNew->FindKey(argument.Data())) { dirNew->cd(argument.Data()); dirNew=gDirectory; } else { dirSave->cd(); return kFALSE; } } } dirSave->cd(); return kTRUE; } //------------------------misc-------------------------------- TList* HTool::getListOfAllDataMembers(TClass* cl) { // return a list of all data members of a class. // contains all data members of bass classes. TList* list=cl->GetListOfDataMembers(); TIter next_BaseClass(cl->GetListOfBases()); TBaseClass *pB; while ((pB = (TBaseClass*) next_BaseClass())) { if (!pB->GetClassPointer()) continue; list->AddAll(pB->GetClassPointer()->GetListOfDataMembers() ); } return list; } void HTool::scanOracle(TString inputname,TString outputname) { // scans oracle runs table source code to extract // file names, run ids and number of events FILE* input =0; FILE* output=0; if(!HTool::openAscii(&input,inputname ,"r")) { exit(1); } if(!HTool::openAscii(&output,outputname,"w")) { exit(1); } Char_t line[4000]; TString buffer=""; TString filename; TString fileRunId; TString fileNEvents; TString newLine; Int_t count=0; Int_t sumEvts=0; Int_t Evts=0; while(1) { if(feof(input)) break; fgets(line, sizeof(line), input); buffer=line; if (buffer.Contains("100")) { buffer.ReplaceAll("",""); buffer.ReplaceAll("",""); buffer.ReplaceAll("\n",""); fileRunId=buffer; fgets(line, sizeof(line), input); fgets(line, sizeof(line), input); fgets(line, sizeof(line), input); fgets(line, sizeof(line), input); buffer=line; buffer.ReplaceAll("",""); buffer.ReplaceAll("",""); buffer.ReplaceAll("\n",""); fileNEvents=buffer; newLine= filename + " " + fileRunId + " " + fileNEvents; sscanf(newLine.Data(),"%*s%*s%i",&Evts); sumEvts=sumEvts+Evts; count++; fprintf(output,"%s%s",newLine.Data(),"\n"); } } cout<&1; " ); pipe = gSystem->OpenPipe( command.Data(), "r" ); output = ""; while (line.Gets( pipe )) output += line; return gSystem->ClosePipe( pipe ); } Int_t HTool::getLinearIndex(Int_t x1, UInt_t x1max, Int_t x2, UInt_t x2max, Int_t x3, UInt_t x3max, Int_t x4, UInt_t x4max, Int_t x5, UInt_t x5max) { // Translates i.e. array coordinate 0,2,3 of array[3][8][1] // in a linear unique coordinate starting from 0 according // to standard memory layout of c++. Supports 2-5 // dimensional addressing. Returns -1 in case of error. // // // index = // 2-dim = x2 + x1*x2max // 3-dim = x3 + x1*x2max*x3max + x2*x3max // 4-dim = x4 + x1*x2max*x3max*x4max + x2*x3max*x4max + x3*x4max // 5-dim = x5 + x1*x2max*x3max*x4max*x5max + x2*x3max*x4max*x5max + x3*x4max*x5max + x4*x5max Int_t dim = 2; if(x1 < 0 || x1max <= 0 || x1 >= (Int_t)x1max) dim = -1; if(x2 < 0 || x2max <= 0 || x2 >= (Int_t)x2max) dim = -1; if(dim > 0) { if(x3 != -1 ){ if(x3max > 0 && x3 < (Int_t)x3max) dim ++; else dim = -1; } if(x3 < 0 && x3max > 0) dim = -1; if(x4 != -1){ if (dim == 3 && x4max > 0 && x4 < (Int_t)x4max) dim ++; else dim = -1; } if(x4 < 0 && x4max > 0) dim = -1; if(x5 != -1){ if(dim == 4 && x5max > 0 && x5 < (Int_t)x5max) dim ++; else dim = -1; } if(x5 < 0 && x5max > 0) dim = -1; } if(dim < 0){ printf("getLinearIndex(): index : %4i %4i %4i %4i %4i, maxindex : %4i %4i %4i %4i %4i\n" ,x1,x2,x3,x4,x5,x1max,x2max,x3max,x4max,x5max); return -1; } if (dim == 2) return x2 + x1*x2max; else if(dim == 3) return x3 + x1*x2max*x3max + x2*x3max; else if(dim == 4) return x4 + x1*x2max*x3max*x4max + x2*x3max*x4max + x3*x4max; else if(dim == 5) return x5 + x1*x2max*x3max*x4max*x5max + x2*x3max*x4max*x5max + x3*x4max*x5max + x4*x5max; return 0; } Int_t HTool::getDimIndex(Int_t index, Int_t& x1, Int_t& x2, Int_t& x3, Int_t& x4, Int_t& x5, UInt_t x1max, UInt_t x2max, UInt_t x3max, UInt_t x4max, UInt_t x5max) { // Translates linear array coordinate (like produced with HTool::getLinearIndex()) // back to multi dimensional indices . Returns -1 in case of error. // // linear index // index = x2 + x1*x2max 2dim // x3 + x1*x2max*x3max + x2*x3max 3dim // x4 + x1*x2max*x3max*x4max + x2*x3max*x4max + x3*x4max 4dim // x5 + x1*x2max*x3max*x4max*x5max + x2*x3max*x4max*x5max + x3*x4max*x5max + x4*x5max 5dim x1=x2=x3=x4=x5=-1; Int_t dim = 2; if(x1max <= 0) dim = -1; if(x2max <= 0) dim = -1; if(dim > 0){ if(x3max > 0) dim ++; if(dim == 3 && x4max > 0) dim ++; if(dim == 4 && x5max > 0) dim ++; } if(dim < 0){ printf("getDimIndex(): maxindex : %4i %4i %4i %4i %4i! \n",x1max,x2max,x3max,x4max,x5max); return -1; } Int_t max = 0; if (dim == 2) max = x1max*x2max; else if(dim == 3) max = x1max*x2max*x3max; else if(dim == 4) max = x1max*x2max*x3max*x4max; else if(dim == 5) max = x1max*x2max*x3max*x4max*x5max; if(index < 0 || index >= max ){ printf("getDimIndex(): index >= maxindex || index < 0 : index =%i! \n",index); return -1; } if (dim == 2) { x1 = index / x2max; x2 = index - x1*x2max; } else if(dim == 3){ Int_t ind2 = x2max*x3max; x1 = index / ind2; x2 = (index - x1*ind2) / x3max; x3 = index - x1*ind2 - x2*x3max; } else if(dim == 4) { Int_t ind2 = x2max*x3max*x4max; Int_t ind3 = x3max*x4max; x1 = index / ind2; x2 = (index - x1*ind2) / ind3; x3 = (index - x1*ind2 - x2*ind3) / x4max; x4 = index - x1*ind2 - x2*ind3 - x3*x4max; } else if(dim == 5) { Int_t ind2 = x2max*x3max*x4max*x5max; Int_t ind3 = x3max*x4max*x5max; Int_t ind4 = x4max*x5max; x1 = index / ind2; x2 = (index - x1*ind2) / ind3; x3 = (index - x1*ind2 - x2*ind3) / ind4; x4 = (index - x1*ind2 - x2*ind3 - x3*ind4) / x5max; x5 = index - x1*ind2 - x2*ind3 - x3*ind4 - x4*x5max; } return 0; } void HTool::sort(Int_t n, Char_t* arrIn,Int_t* index,Bool_t down,Bool_t overwrite) { // sorts array arrIn. If down=kTRUE sort in decreasing order. // If overwrite=kTRUE input array is sorted otherwise not and // the result can be obtained from the index array (has to be provided // by the caller with size >=n ) If index=NULL the index array is not used. // if index=NULL and overwrite=kFALSE the function returns // without doing anything. if(!overwrite&&!index) return; if(n<2) return; register Int_t a,b,c; Int_t exchange,bin=0; Char_t val; Char_t* arr=0; Char_t* arrNew=0; if(!overwrite) { arrNew = new Char_t [n]; memcpy(arrNew,arrIn,n*sizeof(Char_t)); arr=arrNew; } else { arr=arrIn; } if(index) for(Int_t i=0;ival) // > decreasing order { c = b; val = arr[b]; exchange = 1; if(index) bin = index[b]; } if(!down&&arr[b]=n ) If index=NULL the index array is not used. // if index=NULL and overwrite=kFALSE the function returns // without doing anything. if(!overwrite&&!index) return; if(n<2) return; register Int_t a,b,c; Int_t exchange,bin=0; Short_t val; Short_t* arr=0; Short_t* arrNew=0; if(!overwrite) { arrNew = new Short_t [n]; memcpy(arrNew,arrIn,n*sizeof(Short_t)); arr=arrNew; } else { arr=arrIn; } if(index) for(Int_t i=0;ival) // > decreasing order { c = b; val = arr[b]; exchange = 1; if(index) bin = index[b]; } if(!down&&arr[b]=n ) If index=NULL the index array is not used. // if index=NULL and overwrite=kFALSE the function returns // without doing anything. if(!overwrite&&!index) return; if(n<2) return; register Int_t a,b,c; Int_t exchange,bin=0; Int_t val; Int_t* arr=0; Int_t* arrNew=0; if(!overwrite) { arrNew = new Int_t [n]; memcpy(arrNew,arrIn,n*sizeof(Int_t)); arr=arrNew; } else { arr=arrIn; } if(index) for(Int_t i=0;ival) // > decreasing order { c = b; val = arr[b]; exchange = 1; if(index) bin = index[b]; } if(!down&&arr[b]=n ) If index=NULL the index array is not used. // if index=NULL and overwrite=kFALSE the function returns // without doing anything. if(!overwrite&&!index) return; if(n<2) return; register Int_t a,b,c; Int_t exchange,bin=0; Float_t val; Float_t* arr=0; Float_t* arrNew=0; if(!overwrite) { arrNew = new Float_t [n]; memcpy(arrNew,arrIn,n*sizeof(Float_t)); arr=arrNew; } else { arr=arrIn; } if(index) for(Int_t i=0;ival) // > decreasing order { c = b; val = arr[b]; exchange = 1; if(index) bin = index[b]; } if(!down&&arr[b]=n ) If index=NULL the index array is not used. // if index=NULL and overwrite=kFALSE the function returns // without doing anything. if(!overwrite&&!index) return; if(n<2) return; register Int_t a,b,c; Int_t exchange,bin=0; Double_t val; Double_t* arr=0; Double_t* arrNew=0; if(!overwrite) { arrNew = new Double_t [n]; memcpy(arrNew,arrIn,n*sizeof(Double_t)); arr=arrNew; } else { arr=arrIn; } if(index) for(Int_t i=0;ival) // > decreasing order { c = b; val = arr[b]; exchange = 1; if(index) bin = index[b]; } if(!down&&arr[b]=n ) If index=NULL the index array is not used. // if index=NULL and overwrite=kFALSE the function returns // without doing anything. if(n<2) return; register Int_t a,b,c; Int_t exchange,bin=0; Char_t* arr=0; Char_t* tempVar =new Char_t [nArrays]; arr=arrIn; // prepare index array if(index) { for(Int_t i=0;itempVar[leading]) // > decreasing order { c = b; for(Int_t i=0;i=n ) If index=NULL the index array is not used. // if index=NULL and overwrite=kFALSE the function returns // without doing anything. if(n<2) return; register Int_t a,b,c; Int_t exchange,bin=0; Short_t* arr=0; Short_t* tempVar =new Short_t [nArrays]; arr=arrIn; // prepare index array if(index) { for(Int_t i=0;itempVar[leading]) // > decreasing order { c = b; for(Int_t i=0;i=n ) If index=NULL the index array is not used. // if index=NULL and overwrite=kFALSE the function returns // without doing anything. if(n<2) return; register Int_t a,b,c; Int_t exchange,bin=0; Int_t* arr=0; Int_t* tempVar =new Int_t [nArrays]; arr=arrIn; // prepare index array if(index) { for(Int_t i=0;itempVar[leading]) // > decreasing order { c = b; for(Int_t i=0;i=n ) If index=NULL the index array is not used. // if index=NULL and overwrite=kFALSE the function returns // without doing anything. if(n<2) return; register Int_t a,b,c; Int_t exchange,bin=0; Float_t* arr=0; Float_t* tempVar =new Float_t [nArrays]; arr=arrIn; // prepare index array if(index) { for(Int_t i=0;itempVar[leading]) // > decreasing order { c = b; for(Int_t i=0;i=n ) If index=NULL the index array is not used. // if index=NULL and overwrite=kFALSE the function returns // without doing anything. if(n<2) return; register Int_t a,b,c; Int_t exchange,bin=0; Double_t* arr=0; Double_t* tempVar =new Double_t [nArrays]; arr=arrIn; // prepare index array if(index) { for(Int_t i=0;itempVar[leading]) // > decreasing order { c = b; for(Int_t i=0;i 0 for distributions // compared wider than a normal distribution and // and < 0 for distributions beeing peaking stronger if(n<1) return -999; Double_t mean = TMath::Mean(n,data); Double_t sigma= TMath::RMS (n,data); Double_t sum=0; for(Int_t i=0;i 0 for distributions // compared wider than a normal distribution and // and < 0 for distributions beeing peaking stronger if(n<1) return -999; Double_t mean = TMath::Mean(n,data); Double_t sigma= TMath::RMS (n,data); Double_t sum=0; for(Int_t i=0;i 0 for distributions // compared wider than a normal distribution and // and < 0 for distributions beeing peaking stronger if(n<1) return -999; Double_t mean = TMath::Mean(n,data); Double_t sigma= TMath::RMS (n,data); Double_t sum=0; for(Int_t i=0;i 0 for distributions // compared wider than a normal distribution and // and < 0 for distributions beeing peaking stronger if(n<1) return -999; Double_t mean = TMath::Mean(n,data); Double_t sigma= TMath::RMS (n,data); Double_t sum=0; for(Int_t i=0;i 0 for distributions // haveing more entries in the tails larger than mean // and < 0 for distributions haveing more entries small // than mean if(n<1) return -999; Double_t mean = TMath::Mean(n,data); Double_t sigma= TMath::RMS (n,data); Double_t sum=0; for(Int_t i=0;i 0 for distributions // haveing more entries in the tails larger than mean // and < 0 for distributions haveing more entries small // than mean if(n<1) return -999; Double_t mean = TMath::Mean(n,data); Double_t sigma= TMath::RMS (n,data); Double_t sum=0; for(Int_t i=0;i 0 for distributions // haveing more entries in the tails larger than mean // and < 0 for distributions haveing more entries small // than mean if(n<1) return -999; Double_t mean = TMath::Mean(n,data); Double_t sigma= TMath::RMS (n,data); Double_t sum=0; for(Int_t i=0;i 0 for distributions // haveing more entries in the tails larger than mean // and < 0 for distributions haveing more entries small // than mean if(n<1) return -999; Double_t mean = TMath::Mean(n,data); Double_t sigma= TMath::RMS (n,data); Double_t sum=0; for(Int_t i=0;i= 0.5) { cout<<"HTool::truncatedIndex() : truncation fraction >= 0.5 not possible!"<= n) lower -= 1; if(lower < 0) lower = 0; return lower; } Double_t HTool::chi2DistributionNorm(Double_t *x,Double_t *par) { // Chisquare density distribution for nrFree degrees of freedom // normalized by nrFree Double_t nrFree = par[0]; Double_t chi2 = x[0] * nrFree; if (chi2 > 0) { Double_t lambda = nrFree / 2.; Double_t norm = TMath::Gamma(lambda) * TMath::Power(2.,lambda); return par[1] * nrFree * (TMath::Power(chi2,lambda-1) * TMath::Exp(-0.5 * chi2) / norm); } else return 0.0; } Double_t HTool::chi2Distribution(Double_t *x,Double_t *par) { // Chisquare density distribution for nrFree degrees of freedom Double_t nrFree = par[0]; Double_t chi2 = x[0]; if (chi2 > 0) { Double_t lambda = nrFree / 2.; Double_t norm = TMath::Gamma(lambda) * TMath::Power(2.,lambda); return par[1] * (TMath::Power(chi2,lambda-1) * TMath::Exp(-0.5 * chi2) / norm); } else return 0.0; } TF1* HTool::chi2Distribution(Int_t nDegreeOfFreedom,TString name,Bool_t norm,Double_t scale) { // Return TF1 for Chisquare density distribution for nDegreeOfFreedom // degrees of freedom. The distribution will be normalize if norm == kTRUE // The function will be named name. The user will be responible of deleting // the object after usage. TF1 params : par[0]=nDegreeOfFreedom , par[1]=scale // The scale typical should be binwidth*integral of the histogram if the // TF1 should be plotted on top TF1 *f1=0; if(norm) f1 = new TF1(name.Data(),HTool::chi2DistributionNorm,0.01,50,2); else f1 = new TF1(name.Data(),HTool::chi2Distribution ,0.01,50,2); f1->SetParameter(0,Double_t(nDegreeOfFreedom)); f1->SetParameter(1,Double_t(scale)); return f1; } //--------------------------hists------------------------- void HTool::roundHist(TH2* h,Int_t ndigit,Int_t ndigiterr) { // Round bin content to ndigit and binerror to ndigterr digits. // for ndigit or ndigiterr =-1 the rounding will be skipped for // the corresponding value. if(!h) { cout<<"HTool::roundHist(): Zero pointer received!"<GetNbinsX(); Int_t biny=h->GetNbinsY(); Double_t val; for(Int_t i=1;i<=binx;i++) { for(Int_t j=1;j<=biny;j++) { if(ndigit!=-1) { val=h->GetBinContent(i,j); h->SetBinContent(i,j,HTool::roundDigits(val,ndigit)); } if(ndigiterr!=-1) { val=h->GetBinError(i,j); h->SetBinError(i,j,HTool::roundDigits(val,ndigiterr)); } } } } TH1* HTool::getHist(TFile* inp,TString name) { // gets histogram with name name from root file inp->cd(); TH1* h=(TH1*)inp->Get(name.Data()); if(h==0) { cout<<"getHist(): Hist "<GetXaxis()->GetNbins(); if (firstbin < 0) firstbin = 1; if (lastbin < 0) lastbin = nbins; if (lastbin > nbins+1) lastbin = nbins; nbins = lastbin - firstbin + 1; Double_t *XX = new Double_t[nbins]; Int_t i; for (i=0;iGetBinContent(i+firstbin); } TH1::SmoothArray(nbins,XX,ntimes); for (i=0;iSetBinContent(i+firstbin,XX[i]); } delete [] XX; } TObjArray* HTool::slices(TH2* h2,TF1* f,TString axis,Int_t firstbin,Int_t lastbin,Int_t cut,TString opt,TString suffix,Int_t markerstyle,Int_t markercolor,Float_t markersize) { // calls internal the fitslice of root. Suffix can be added to name of fitted hists. // Returns a TObjArray pointer to an array containing the result hists of the fit. // Numbering is the same as the the parameters of the fitfunction + chi2 at last place. TObjArray* array=0; if(h2==0) { cout<<"Warning: HTool::slices : ZERO pointer retrieved for histogram!"<FitSlicesX(f,firstbin,lastbin,cut,opt); else if(axis.CompareTo("y")==0) h2->FitSlicesY(f,firstbin,lastbin,cut,opt); else { cout<<"Warning: HTool::slices : Unknown argument for axis : "<GetNpar()+1); TString name=""; name=h2->GetName(); Char_t histname[300]; for(Int_t i=0;iGetNpar();i++) { sprintf(histname,"%s%s%i",name.Data(),"_",i); h= (TH1D*)gDirectory->Get(histname); if(suffix.CompareTo("")!=0) { sprintf(histname,"%s%s%s%s%i",name.Data(),"_",suffix.Data(),"_",i); h->SetName(histname); } array->AddAt(h,i); } sprintf(histname,"%s%s",name.Data(),"_chi2"); h=(TH1D*)gDirectory->Get(histname); if(suffix.CompareTo("")!=0) { sprintf(histname,"%s%s%s%s",name.Data(),"_",suffix.Data(),"_chi2"); h->SetName(histname); } array->AddAt(h,f->GetNpar()); for(Int_t i=0;iLastIndex()+1;i++) { h =(TH1D*)array->At(i); h->SetLineColor(markercolor); h->SetMarkerColor(markercolor); h->SetMarkerStyle(markerstyle); h->SetMarkerSize(markersize); } if(owner==1) delete f; return array; } TObjArray* HTool::projections(TH2* h2,TString axis,Int_t firstbin,Int_t lastbin,Int_t nsteps,TString opt,TString suffix,Int_t markerstyle,Int_t markercolor,Float_t markersize) { // calls internal the projections function of the hist. // Suffix can be added to name of fitted hists. // Returns a TObjArray pointer to an array containing // the result hists of the loop of projections. TObjArray* array=0; if(h2==0) { cout<<"Warning: HTool::projections : ZERO pointer retrieved for histogram!"<GetNbinsY(); if(axis.CompareTo("y")==0)bin2=h2->GetNbinsX(); }else { // part of range bin1 = firstbin; bin2 = lastbin; } array=new TObjArray(0); //---------------------defining stepsize-------------------- if(nsteps==-99)stepsize = 1; else stepsize = nsteps; //---------------------projecting--------------------------- for(Int_t i=bin1;iGetName(),"_px_",i); else sprintf(name,"%s%s%s%s%i",h2->GetName(),"_",suffix.Data(),"_px_",i); array->AddLast(h2->ProjectionX(name,i,i+stepsize-1,opt)); } else if(axis.CompareTo("y")==0){ if(suffix.CompareTo("")==0)sprintf(name,"%s%s%i" ,h2->GetName(),"_py_",i); else sprintf(name,"%s%s%s%s%i",h2->GetName(),"_",suffix.Data(),"_py_",i); array->AddLast(h2->ProjectionY(name,i,i+stepsize-1,opt)); } else{ cout<<"Warning: HTool::slices : Unknown argument for axis : "<Expand(array->GetLast()+1); cout<<"number of hists "<LastIndex()+1<LastIndex()+1;i++) { ((TH1D*)array->At(i))->SetLineColor(markercolor); ((TH1D*)array->At(i))->SetMarkerColor(markercolor); ((TH1D*)array->At(i))->SetMarkerStyle(markerstyle); ((TH1D*)array->At(i))->SetMarkerSize(markersize); } return array; } TObjArray* HTool::fitArray(TObjArray* array,TF1* fit,TString name,Float_t min,Float_t max,Int_t opt,Float_t x1,Float_t x2,Int_t markerstyle,Int_t markercolor,Float_t markersize) { // Applies fit fit to all hists in array. The result of the fit // will be returned in a TObjArray which contains hists for all parameters // from fit (with corresponding errors set). The number of hists is equal npar+1. // The ordering inside the array starts from par0 to parN and the last hist // will contain the chi2. The number of bins in the hists will be equal to the // the number of fitted hists. The range of the result hists can be set via // min and max. If these values ar not specified the range will be 0-1. // If opt=1 fit will use range x1 x2 arround max of hists if(array==0){ cout<<"HTool::fitArray(): array pointer =0!"<GetEntries(); Int_t nPars =fit->GetNpar(); TObjArray* result=new TObjArray(nPars+1); TH1D* htmp=0; TString myname=name; Int_t nbinsx=size; Float_t xmin,xmax; if(min==0&&max==0) { xmin=0.; xmax=1.; } else { xmin=min; xmax=max; } for(Int_t i=0;iSetLineColor(1); htmp->SetMarkerColor(markercolor); htmp->SetMarkerStyle(markerstyle); htmp->SetMarkerSize (markersize); result->AddAt(htmp,i); } Double_t val=0,valerr=0; for(Int_t j=0;jAt(j); if(opt==1) { Float_t mean=h->GetXaxis()->GetBinCenter(h->GetMaximumBin()); fit->SetRange(mean-x1,mean+x2); } h->Fit(fit,"QNR"); if(h->Integral()>2) { for(Int_t i=0;iAt(i); val =fit->GetParameter(i); valerr=fit->GetParError (i); if(finite(val) !=0&& finite(valerr)!=0) { htmp->SetBinContent(j+1,val); htmp->SetBinError (j+1,valerr); } } val=fit->GetChisquare(); if(finite(val)!=0) { ((TH1*)result->At(nPars))->SetBinContent(j+1,val); } } } return result; } TH1D* HTool::projectY(TH1* h,Int_t xbin1,Int_t xbin2,TString suff, Int_t ybin,Float_t ymin,Float_t ymax, Int_t markerstyle,Int_t markercolor,Float_t markersize) { if(h==0) { cout<<"HTool::projectY(): ZERO pointer for hists received!"<GetName(); TString title=""; if(suff.CompareTo("")==0) name+="_projY"; else name+=suff; if(ybin==0||(ymin==ymax)) { ymin=h->GetMinimum(); ymax=h->GetMaximum(); ybin=100; } if(xbin1<1 )xbin1=1; if(xbin2>h->GetNbinsX())xbin2=h->GetNbinsX(); if(xbin2==0)xbin2=h->GetNbinsX(); TH1D* hproj=new TH1D(name,title,ybin,ymin,ymax); hproj->SetMarkerStyle(markerstyle); hproj->SetMarkerColor(markercolor); hproj->SetMarkerSize(markersize); hproj->SetLineColor(markercolor); hproj->SetXTitle(h->GetXaxis()->GetTitle()); for(Int_t i=xbin1;i<=xbin2;i++) { hproj->Fill(h->GetBinContent(i)); } return hproj; } TGraphErrors* HTool::fitScatter(TH2* h,TF1* f, TString opt, Bool_t silent, Float_t xmin,Float_t xmax, Float_t ymin,Float_t ymax, Float_t window,Bool_t clean) { // Fits h with TF1 f. Convert the hist to TGraphErrors // first and than perfom the fit on the graph. // With xmin/xmax/ymin,ymax a region can be selected. // If window is !=0 the graph is fitted with f and // in an second iteration the graph is rebuild to just // contain values inside the window arround f. If clean // is true all values of the hist arround the window of // f are set to 0. String opt are options for the Fit. // silent=kTRUE switches the prints off. if(h==0) return 0; if(f==0) return 0; Bool_t quiet=silent; if(!quiet) { cout<<"----------------------------------------------------"<GetName()<GetNbinsX(); Int_t nbinsy=h->GetNbinsY(); Bool_t windowx=kFALSE; if(xmin!=0||xmax!=0) windowx=kTRUE; Bool_t windowy=kFALSE; if(ymin!=0||ymax!=0) windowy=kTRUE; TString name=h->GetName(); name+="_gFit"; TGraphErrors* g=0; g=new TGraphErrors(); g->SetName(name.Data()); Double_t max=h->GetMaximum(); if(max==0) { delete g; return 0; } Int_t ctpoint=0; Double_t x,y,val,err; for(Int_t i=1;i<=nbinsx;i++){ x=h->GetXaxis()->GetBinCenter(i); if(windowx) { if(xxmax) continue;} for(Int_t j=1;j<=nbinsy;j++){ y=h->GetYaxis()->GetBinCenter(j); if(windowy) { if(yymax) continue;} val=h->GetBinContent(i,j); if(val==0)continue; g->SetPoint(ctpoint,x,y); err=h->GetBinError(i,j); if(err==0) err=1e-10; g->SetPointError(ctpoint,err,err); ctpoint++; } } if(!quiet) { cout<<"----------------------------------------------------"<Fit(f,opt.Data()); if(usewindow) { if(!quiet) { cout<<"----------------------------------------------------"<GetChisquare()<SetName(name.Data()); Double_t ref; ctpoint=0; for(Int_t i=1;i<=nbinsx;i++){ x=h->GetXaxis()->GetBinCenter(i); if(windowx) { if(xxmax) continue;} for(Int_t j=1;j<=nbinsy;j++){ y=h->GetYaxis()->GetBinCenter(j); if(windowy) { if(yymax) continue;} ref=f->Eval(x); if(yref+window) { continue; } val=h->GetBinContent(i,j); if(val==0)continue; g->SetPoint(ctpoint,x,y); err=h->GetBinError(i,j); if(err==0) err=1e-10; g->SetPointError(ctpoint,0.,err); ctpoint++; } } if(!quiet) { cout<<"----------------------------------------------------"<Fit(f,opt.Data()); g->Print(); //delete g; } if(clean) { Double_t ref; for(Int_t i=1;i<=nbinsx;i++) { x=h->GetXaxis()->GetBinCenter(i); for(Int_t j=1;j<=nbinsy;j++){ y=h->GetYaxis()->GetBinCenter(j); ref=f->Eval(x); if(yref+window) { h->SetBinContent(i,j,0); h->SetBinError(i,j,0); continue; } } } } if(!quiet) { cout<<"----------------------------------------------------"<GetChisquare()<GetName(); newname+="_remove"; } if(last<0)last= h->GetNbinsX(); TString classname=h->ClassName(); TH1* hnew=0; if(classname.CompareTo("TH1I")==0)hnew=(TH1*)new TH1I(*(TH1I*)(h)); if(classname.CompareTo("TH1S")==0)hnew=(TH1*)new TH1S(*(TH1S*)(h)); if(classname.CompareTo("TH1F")==0)hnew=(TH1*)new TH1F(*(TH1F*)(h)); if(classname.CompareTo("TH1D")==0)hnew=(TH1*)new TH1D(*(TH1D*)(h)); if(classname.CompareTo("TProfile")==0)hnew=(TH1*)new TProfile(*(TProfile*)(h)); if(hnew==0) { hnew->SetName(newname); hnew->SetBins(h->GetNbinsX()-(first-1)-last,h->GetXaxis()->GetBinLowEdge(first),h->GetXaxis()->GetBinUpEdge(last)); for(Int_t i=1;i<=hnew->GetNbinsX();i++) { hnew->SetBinContent(i,h->GetBinContent(i,(first-1)+1)); hnew->SetBinError(i,h->GetBinError(i,(first-1)+1)); } }else cout<<"Error in HTool::removeEnds(): receiving zero pointer"<GetName(); newname+="_remove"; if(last<0)last= h->GetNbinsX(); TString classname=h->ClassName(); TH1* hnew=0; //if(classname.CompareTo("TH1I")==0)hnew=(TH1*)new TH1I(*(const TH1I*)(h)); //if(classname.CompareTo("TH1S")==0)hnew=(TH1*)new TH1S(*(const TH1S*)(h)); //if(classname.CompareTo("TH1F")==0)hnew=(TH1*)new TH1F(*(const TH1F*)(h)); //if(classname.CompareTo("TH1D")==0)hnew=(TH1*)new TH1D(*(const TH1D*)(h)); //if(classname.CompareTo("TProfile")==0)hnew=(TH1*)new TProfile(*(const TProfile*)(h)); cout<Dump(); cout<<"test2"<SetName(newname); h->SetBins(hnew->GetNbinsX()-(first-1)-last,hnew->GetXaxis()->GetBinLowEdge(first),hnew->GetXaxis()->GetBinUpEdge(last)); for(Int_t i=1;i<=h->GetNbinsX();i++) { h->SetBinContent(i,hnew->GetBinContent(i,(first-1)+1)); h->SetBinError(i,hnew->GetBinError(i,(first-1)+1)); } }else cout<<"Error in HTool::removeEnds(): receiving zero pointer"<GetNbinsX(); Int_t f,l; f=0; l=0; first=0; last=nbins; for(Int_t i=1;i<=nbins;i++) { if(f==0 && h->GetBinContent(i)==0) first=i+1; else f++; if(l==0 && h->GetBinContent(nbins-i)==0) last=nbins-i-1; else l++; if(l>0&&f>0) return 0; } return 0; } void HTool::cleanHist(TH1* h,Double_t threshold,Double_t val) { // clean hist if bincontent is below a certain value (threshold): // The bin content is replaced by val. if(h!=0) { TString classname=h->ClassName(); Int_t type=0; if(classname.Contains("TH1")==1)type=1; if(classname.Contains("TH2")==1)type=2; Int_t binsX=0,binsY=0; binsX=h->GetNbinsX(); if(type==2)binsY=h->GetNbinsY(); Double_t bincontent; if(type==1) { for(Int_t x=0;x<=binsX;x++) { bincontent= h->GetBinContent(x+1); if(bincontentSetBinContent(x+1,val); } } } if(type==2) { for(Int_t x=0;x<=binsX;x++) { for(Int_t y=0;y<=binsY;y++){ bincontent= h->GetBinContent(x+1,y+1); if(bincontentSetBinContent(x+1,y+1,val); } } } } } else { cout<<"Warning: HTool::cleanHist : ZERO pointer for hist recieved!"<ClassName(); Int_t type=0; if(classname.Contains("TH1")==1)type=1; if(classname.Contains("TH2")==1)type=2; Int_t binsX=0,binsY=0; binsX=h->GetNbinsX(); if(type==2)binsY=h->GetNbinsY(); if(type==1) { for(Int_t x=0;x<=binsX;x++) { if(val!=-99) h->SetBinContent(x+1,val); if(valerr!=-99)h->SetBinError (x+1,valerr); } } if(type==2) { for(Int_t x=0;x<=binsX;x++) { for(Int_t y=0;ySetBinContent(x+1,y+1,val); if(valerr!=-99)h->SetBinError (x+1,y+1,valerr); } } } } else { cout<<"Warning: HTool::resetHist : ZERO pointer for hist recieved!"<ClassName(); TString myhistname=""; Int_t type=0; if(classname.Contains("TH1")==1)type=1; if(classname.Contains("TH2")==1)type=2; if(classname.Contains("TProfile")==1)type=1; if(name.CompareTo("")==0) { // if no new name, add _copy to old name myhistname=h->GetName(); myhistname+=myhistname + "_copy"; } else { myhistname=name; } Int_t binsX=0,binsY=0; binsX=h->GetNbinsX(); if(type==2)binsY=h->GetNbinsY(); if(classname.Contains("TH1S")==1)hcp=new TH1S(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax()); //if(classname.Contains("TH1I")==1)hcp=new TH1I(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax()); if(classname.Contains("TH1F")==1)hcp=new TH1F(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax()); if(classname.Contains("TH1D")==1)hcp=new TH1D(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax()); if(classname.Contains("TProfile")==1)hcp=new TH1D(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax()); if(classname.Contains("TH2S")==1)hcp=new TH2S(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax(),binsY,h->GetYaxis()->GetXmin(),h->GetYaxis()->GetXmax()); //if(classname.Contains("TH2I")==1)hcp=new TH2I(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax(),binsY,h->GetYaxis()->GetXmin(),h->GetYaxis()->GetXmax()); if(classname.Contains("TH2F")==1)hcp=new TH2F(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax(),binsY,h->GetYaxis()->GetXmin(),h->GetYaxis()->GetXmax()); if(classname.Contains("TH2D")==1)hcp=new TH2D(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax(),binsY,h->GetYaxis()->GetXmin(),h->GetYaxis()->GetXmax()); hcp->SetXTitle(h->GetXaxis()->GetTitle()); hcp->SetYTitle(h->GetYaxis()->GetTitle()); hcp->SetOption(hcp->GetOption()); hcp->SetFillColor(hcp->GetFillColor()); hcp->SetFillStyle(hcp->GetFillStyle()); hcp->SetLineColor(hcp->GetLineColor()); hcp->SetLineStyle(hcp->GetLineStyle()); hcp->SetLineWidth(hcp->GetLineWidth()); hcp->SetMarkerColor(hcp->GetMarkerColor()); hcp->SetMarkerStyle(hcp->GetMarkerStyle()); hcp->SetMarkerSize(hcp->GetMarkerSize()); if(type==2) { hcp->SetZTitle(h->GetZaxis()->GetTitle()); } if(type==1) { for(Int_t x=0;x<=binsX;x++) { if(val!=-99) hcp->SetBinContent(x+1,h->GetBinContent(x+1)); if(valerr!=-99)hcp->SetBinError (x+1,h->GetBinError(x+1)); } } if(type==2) { for(Int_t x=0;x<=binsX;x++) { for(Int_t y=0;y<=binsY;y++){ if(val!=-99) hcp->SetBinContent(x+1,y+1,h->GetBinContent(x+1,y+1)); if(valerr!=-99)hcp->SetBinError (x+1,y+1,h->GetBinError(x+1,y+1)); } } } } else { cout<<"Warning: HTool::copyHist : ZERO pointer for hist recieved!"<ClassName(); TString myhistname=""; Int_t type=0; if(classname.Contains("TH1")==1)type=1; if(classname.Contains("TH2")==1)type=2; if(classname.Contains("TProfile")==1)type=1; if(name.CompareTo("")==0) { // if no new name, add _copy to old name myhistname=h->GetName(); myhistname+=myhistname + "_copy"; } else { myhistname = name; } Int_t binsX=0,binsY=0; if(end!=-99) { binsX = end - start; } else { binsX = h->GetNbinsX() - start; end = h->GetNbinsX(); } if(type==2)binsY=h->GetNbinsY(); if(classname.Contains("TH1S")==1)hcp=new TH1S(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetBinLowEdge(start),h->GetXaxis()->GetBinLowEdge(end)); //if(classname.Contains("TH1I")==1)hcp=new TH1I(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax()); if(classname.Contains("TH1F")==1)hcp=new TH1F(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetBinLowEdge(start),h->GetXaxis()->GetBinLowEdge(end)); if(classname.Contains("TH1D")==1)hcp=new TH1D(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetBinLowEdge(start),h->GetXaxis()->GetBinLowEdge(end)); if(classname.Contains("TProfile")==1)hcp=new TH1D(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetBinLowEdge(start),h->GetXaxis()->GetBinLowEdge(end)); if(classname.Contains("TH2S")==1)hcp=new TH2S(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetBinLowEdge(start),binsY,h->GetYaxis()->GetBinLowEdge(end),h->GetYaxis()->GetXmax()); //if(classname.Contains("TH2I")==1)hcp=new TH2I(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax(),binsY,h->GetYaxis()->GetXmin(),h->GetYaxis()->GetXmax()); if(classname.Contains("TH2F")==1)hcp=new TH2F(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetXmin(),h->GetXaxis()->GetBinLowEdge(start),binsY,h->GetYaxis()->GetBinLowEdge(end),h->GetYaxis()->GetXmax()); if(classname.Contains("TH2D")==1)hcp=new TH2D(myhistname.Data(),h->GetTitle(),binsX,h->GetXaxis()->GetBinLowEdge(start),h->GetXaxis()->GetBinLowEdge(end),binsY,h->GetYaxis()->GetXmin(),h->GetYaxis()->GetXmax()); hcp->SetXTitle(h->GetXaxis()->GetTitle()); hcp->SetYTitle(h->GetYaxis()->GetTitle()); hcp->SetOption(hcp->GetOption()); hcp->SetFillColor(hcp->GetFillColor()); hcp->SetFillStyle(hcp->GetFillStyle()); hcp->SetLineColor(hcp->GetLineColor()); hcp->SetLineStyle(hcp->GetLineStyle()); hcp->SetLineWidth(hcp->GetLineWidth()); hcp->SetMarkerColor(hcp->GetMarkerColor()); hcp->SetMarkerStyle(hcp->GetMarkerStyle()); hcp->SetMarkerSize(hcp->GetMarkerSize()); if(type==2) { hcp->SetZTitle(h->GetZaxis()->GetTitle()); } if(type==1) { for(Int_t x=start;x<=end;x++) { if(val!=-99) hcp->SetBinContent(x+1,h->GetBinContent(x+1)); if(valerr!=-99)hcp->SetBinError (x+1,h->GetBinError(x+1)); } } if(type==2) { for(Int_t x=start;x<=end;x++) { for(Int_t y=0;y<=binsY;y++){ if(val!=-99) hcp->SetBinContent(x+1,y+1,h->GetBinContent(x+1,y+1)); if(valerr!=-99)hcp->SetBinError (x+1,y+1,h->GetBinError(x+1,y+1)); } } } } else { cout<<"Warning: HTool::copyHist : ZERO pointer for hist recieved!"<At(1); TF1* fit=new TF1("tempfit",optline.Data()); h1->Fit(fit,"QN"); Int_t nbinsX=h->GetNbinsX(); Int_t nbinsY=h->GetNbinsY(); Float_t lowedgeY; Float_t myY; for(Int_t i=0;i<=nbinsX;i++) { lowedgeY=fit->Eval(h->GetBinCenter(i+1))-windowfunc; for(Int_t j=0;j<=nbinsY;j++) { myY=h->GetYaxis()->GetBinCenter(j+1); if(myYSetBinContent(i+1,j+1,0); h->SetBinError(i+1,j+1,0); } } } array->Delete(); delete array; return fit; } TF1* HTool::cleanHistArroundLine(TH2* h,TF1* f,TString axis,Int_t firstbin,Int_t lastbin,Int_t cut,TString opt,TString suffix, TString optline,Float_t windowfunc,Float_t windowfunc2) { // beforms slices HTool::operation and fits the result with a line // Resets the bins arround line equation +- windowfunc to 0. TObjArray* array =HTool::slices(h,f,axis,firstbin,lastbin,cut,opt,suffix,8,2,0.7); TH1D* h1=(TH1D*)array->At(1); TF1* fit=new TF1("tempfit",optline.Data()); h1->Fit(fit,"QN"); Int_t nbinsX=h->GetNbinsX(); Int_t nbinsY=h->GetNbinsY(); Float_t lowedgeY; Float_t upedgeY; Float_t myY; for(Int_t i=0;i<=nbinsX;i++) { lowedgeY=fit->Eval(h->GetBinCenter(i+1))-windowfunc; upedgeY =fit->Eval(h->GetBinCenter(i+1))+windowfunc2; for(Int_t j=0;j<=nbinsY;j++) { myY=h->GetYaxis()->GetBinCenter(j+1); if(myYupedgeY) { h->SetBinContent(i+1,j+1,0); h->SetBinError(i+1,j+1,0); } } } array->Delete(); delete array; return fit; } Bool_t HTool::cleanHistCutG(TH2* h,TCutG* cut) { // puts all bins outside the cut to 0. if(!h || !cut) { cout<<"HTool::cleanHistCutG():ZERO POINTER!"<GetNbinsX(); Int_t nBinsY=h->GetNbinsY(); Float_t x,y; for(Int_t i=0;i<=nBinsX;i++){ x=h->GetXaxis()->GetBinCenter(i+1); for(Int_t j=0;j<=nBinsY;j++){ y=h->GetYaxis()->GetBinCenter(j+1); if(cut->IsInside(x,y)==0) { h->SetBinContent(i+1,j+1,0); } } } return kTRUE; } void HTool::setHistErrors(TH1* h,TH1* h2) { // sets bincontent of hist h2 as errors of h if(h!=0&&h2!=0) { TString classname=h->ClassName(); Int_t type=0; if(classname.Contains("TH1")==1)type=1; if(classname.Contains("TH2")==1)type=2; Int_t binsX=0,binsY=0; binsX=h->GetNbinsX(); if(type==2)binsY=h->GetNbinsY(); Double_t bincontent; if(type==1) { for(Int_t x=0;x<=binsX;x++) { bincontent= h2->GetBinContent(x+1); h->SetBinError(x+1,bincontent); } } if(type==2) { for(Int_t x=0;x<=binsX;x++) { for(Int_t y=0;y<=binsY;y++){ bincontent= h2->GetBinContent(x+1,y+1); h->SetBinError(x+1,y+1,bincontent); } } } } else { cout<<"Warning: HTool::cleanHist : ZERO pointer for hist recieved!"<GetMaximum(); } Int_t maxindex= TMath::LocMax(size,max); index=maxindex; return max[maxindex]; } Double_t HTool::getMinHistArray(TH1** h,Int_t size,Int_t& index) { // Finds minimum of all hists in TH1* array of size size. // return min and the index of the hist inside the array // which has the lowest min. if(size==0) { cout<<"HTool::getMaxHistArray()::Size of Array is ZERO!"<GetMinimum(); } Int_t minindex= TMath::LocMin(size,min); index=minindex; return min[minindex]; } TH2* HTool::reBin(TH2* h2,Int_t groupX,Int_t groupY,TString newname) { // Rebins h2 with groups of groupX and groupY. The new created // hist with name newname (if specified) or oldname_rebin will // be returned. TH2* result=0; if(h2==0) return result; TString myname=""; if(newname.CompareTo("")==0) { // default myname=h2->GetName(); myname+="_rebin"; } else { myname=newname; } if(groupX!=0&&groupY!=0) { result=(TH2*)h2->Clone(myname.Data()); Int_t nbinsx=h2->GetNbinsX(); Int_t nbinsy=h2->GetNbinsY(); Float_t xmin=h2->GetXaxis()->GetXmin(); Float_t xmax=h2->GetXaxis()->GetXmax(); Float_t ymin=h2->GetYaxis()->GetXmin(); Float_t ymax=h2->GetYaxis()->GetXmax(); Int_t nbinsX=0; Int_t nbinsY=0; if(nbinsx%groupX!=0) { nbinsX=(Int_t)(nbinsx/groupX)+1; } else { nbinsX=nbinsx/groupX; } if(nbinsy%groupY!=0) { nbinsY=(Int_t)(nbinsy/groupY)+1; } else { nbinsY=nbinsy/groupY; } result->SetBins(nbinsX,xmin,xmax,nbinsY,ymin,ymax); result->Reset(); } else { cout<<"HTool::reBin(): groupX or groupY =0!!!"<GetNbinsX()<<" " <GetXaxis()->GetXmin()<<" " <GetXaxis()->GetXmax()<<" " <GetNbinsY()<<" " <GetYaxis()->GetXmin()<<" " <GetYaxis()->GetXmax()<GetNbinsX()<<" " <GetXaxis()->GetXmin()<<" " <GetXaxis()->GetXmax()<<" " <GetNbinsY()<<" " <GetYaxis()->GetXmin()<<" " <GetYaxis()->GetXmax()<GetNbinsX(); Int_t nBinsY=h2->GetNbinsY(); Float_t x,y; for(Int_t i=1;i<=nBinsX;i++){ for(Int_t j=1;j<=nBinsY;j++){ x=h2->GetXaxis()->GetBinCenter(i); y=h2->GetYaxis()->GetBinCenter(j); result->Fill(x,y,h2->GetBinContent(i,j)); } } return result; } TH2* HTool::exchangeXY(TH2* h2,TString newname) { // creates new hist with name newname (if specified) // or oldname_exchangeXY with exchanged // x- and y-axis TH2* result=0; if(h2==0) return result; TString myname=""; if(newname.CompareTo("")==0) { // default myname=h2->GetName(); myname+="_exchangeXY"; } else { myname=newname; } Int_t nbinsx=h2->GetNbinsX(); Int_t nbinsy=h2->GetNbinsY(); Float_t xmin=h2->GetXaxis()->GetXmin(); Float_t xmax=h2->GetXaxis()->GetXmax(); Float_t ymin=h2->GetYaxis()->GetXmin(); Float_t ymax=h2->GetYaxis()->GetXmax(); result=(TH2*)h2->Clone(myname.Data()); result->SetBins(nbinsy,ymin,ymax,nbinsx,xmin,xmax); for(Int_t i=0;i<=nbinsx;i++){ for(Int_t j=0;j<=nbinsy;j++){ result->SetBinContent(j+1,i+1,h2->GetBinContent(i+1,j+1)); result->SetBinError (j+1,i+1,h2->GetBinError(i+1,j+1)); } } return result; } Bool_t HTool::flipAxis(TH2* h2,TString opt) { // flips x or y axis or both depending on opt (x,y,xy) if(h2==0) return kFALSE; opt.ToLower(); Int_t ax=0; if(opt.CompareTo("x")==0) { ax=0; } else if(opt.CompareTo("y")==0) { ax=1; } else if(opt.CompareTo("xy")==0) { ax=2; } else { cout<<"HTool::flipAxis():Unknown opt "<GetNbinsX(); Int_t nbinsy=h2->GetNbinsY(); Float_t xmin=h2->GetXaxis()->GetXmin(); Float_t xmax=h2->GetXaxis()->GetXmax(); Float_t ymin=h2->GetYaxis()->GetXmin(); Float_t ymax=h2->GetYaxis()->GetXmax(); TH2* hclone=(TH2*)h2->Clone("temp_clone"); if(ax==0)h2->SetBins(nbinsx,-xmax,-xmin,nbinsy, ymin, ymax); if(ax==1)h2->SetBins(nbinsx, xmin, xmax,nbinsy,-ymax,-ymin); if(ax==2)h2->SetBins(nbinsx,-xmax,-xmin,nbinsy,-ymax,-ymin); for(Int_t i=1;i<=nbinsx;i++){ for(Int_t j=1;j<=nbinsy;j++){ if(ax==0||ax==2) { // flip x h2->SetBinContent(i,j,hclone->GetBinContent(nbinsx+1-i,j)); h2->SetBinError (i,j,hclone->GetBinError (nbinsx+1-i,j)); } if(ax==1||ax==2) { // flip y h2->SetBinContent(i,j,hclone->GetBinContent(i,nbinsy+1-j)); h2->SetBinError (i,j,hclone->GetBinError (i,nbinsy+1-j)); } } } delete hclone; return kTRUE; } Bool_t HTool::shiftHistByBin(TH1* h,Int_t shiftbin) { // shifts hist content by shiftbin bins if(!h) return kFALSE; Int_t nbin=h->GetNbinsX(); TH1* hclone=(TH1*)h->Clone("clone"); for(Int_t i=1;i<=nbin;i++) { if((i+shiftbin)>1&&(i+shiftbin)<=nbin) { h->SetBinContent(i+shiftbin,hclone->GetBinContent(i)); } } delete hclone; return kTRUE; } Bool_t HTool::shiftHist(TH1* h,Float_t shift) { // shifts hist content by value shift if(!h) return kFALSE; Int_t nbin=h->GetNbinsX(); TH1* hclone=(TH1*)h->Clone("clone"); Float_t xmin=h->GetXaxis()->GetXmin(); Float_t xmax=h->GetXaxis()->GetXmax(); for(Int_t i=1;i<=nbin;i++) { if((i+shift)>=xmin&&(i+shift)<=xmax) { h->Fill(hclone->GetXaxis()->GetBinCenter(i),hclone->GetBinContent(i)); } } delete hclone; return kTRUE; } Int_t HTool::normalize_max(TH2* ht,TString axis) { // Project to axis and and calc scaling factor for the bin // to maximum of total hist max_total/max_proj. The bins of // the hist are scaled by the factor. The result is 2D hist // where each bin Projection has the same maximum height. // The opration is performed on the original Hist! // arguments: // TH2* ht :pointer to hist which should be normalized // TString axis :="x" , or "y" depending on the projection // return values: // -1 : In the case of zero pointer input or unknown axis argument // 0 : If opreation has been succesful // 3 : TH2 max is 0 (most probably empty). if(!ht) { cout<<"HTool::normalize_max(TH2*,TString axis):: ZERO pointer!"<GetMaximum(); if(max==0) return 3; Int_t nbinsx=ht->GetNbinsX(); Int_t nbinsy=ht->GetNbinsY(); axis.ToLower(); if(axis.CompareTo("y")==0) { for(Int_t i=0; iProjectionX("proj",i+1,i+1,""); hproj->SetDirectory(0); Double_t max_y=hproj->GetMaximum(); if(max_y==0) continue; Double_t scale=max/max_y; for(Int_t j=0;jGetBinContent(j+1,i+1); ht->SetBinContent(j+1,i+1,scale*content); } delete hproj; } return 0; } else if(axis.CompareTo("x")==0) { for(Int_t i=0; iProjectionY("proj",i+1,i+1,""); hproj->SetDirectory(0); Double_t max_y=hproj->GetMaximum(); if(max_y==0) continue; Double_t scale=max/max_y; for(Int_t j=0;jGetBinContent(i+1,j+1); ht->SetBinContent(i+1,j+1,scale*content); } delete hproj; } return 0; } else { cout<<"HTool::normalize_max(TH2*,TString axis):: unknown option for axis :"<GetName(); myname+="_graph"; } else { myname=newname; } Int_t nBinsX=h->GetNbinsX(); Double_t x,y; result=new TGraph(); result->SetName(myname.Data()); result->SetLineColor(1); result->SetMarkerStyle(markerstyle); result->SetMarkerColor(markercolor); result->SetMarkerSize (markersize); for(Int_t i=0;iGetXaxis()->GetBinCenter(i+1); y=h->GetBinContent(i+1); if(y!=0) { if(!exchange)result->SetPoint(i,x,y); else result->SetPoint(i,y,x); } else { result->SetPoint(i,0.,0.); } } for(Int_t i=0;iGetPoint(j,x,y); else result->GetPoint(j,y,x); if(y==0||!finite(y)) { result->RemovePoint(j); break; } } } return result; } TGraphErrors* HTool::histToGraphErrors(TH1* h,TString newname,Bool_t exchange,Int_t markerstyle,Int_t markercolor,Float_t markersize) { // Creates a TGraphErrors from hist h. The axis can be excganged if // exchange=kTRUE; The Graph does not take empty bins into account. TGraphErrors* result=0; if(h==0) return result; TString myname=""; if(newname.CompareTo("")==0) { // default myname=h->GetName(); myname+="_graph"; } else { myname=newname; } Int_t nBinsX=h->GetNbinsX(); Double_t x,y; Double_t xE,yE; result=new TGraphErrors(); result->SetName(myname.Data()); result->SetLineColor(1); result->SetMarkerStyle(markerstyle); result->SetMarkerColor(markercolor); result->SetMarkerSize (markersize); for(Int_t i=0;iGetXaxis()->GetBinCenter(i+1); y =h->GetBinContent(i+1); xE=h->GetXaxis()->GetBinWidth(i+1); yE=h->GetBinError(i+1); if(y!=0) { if(!exchange) { result->SetPoint(i,x,y); result->SetPointError(i,xE,yE); } else { result->SetPoint(i,y,x); result->SetPointError(i,yE,xE); } } else { result->SetPoint(i,0.,0.); } } for(Int_t i=0;iGetPoint(j,x,y); } else { result->GetPoint(j,y,x); } if(y==0||!finite(y)) { result->RemovePoint(j); break; } } } return result; } TGraph* HTool::hist2DToGraph(TH2* h,Float_t xmin,Float_t xmax, Float_t ymin,Float_t ymax ) { // Puts h into TGraph . // With xmin/xmax/ymin,ymax a region can be selected. if(h==0) return 0; Int_t nbinsx=h->GetNbinsX(); Int_t nbinsy=h->GetNbinsY(); Bool_t windowx=kFALSE; if(xmin!=0||xmax!=0) windowx=kTRUE; Bool_t windowy=kFALSE; if(ymin!=0||ymax!=0) windowy=kTRUE; TString name=h->GetName(); name+="_gTH2"; TGraph* g=0; g=new TGraph(); g->SetName(name.Data()); Double_t max=h->GetMaximum(); if(max==0) { delete g; return 0; } Double_t x,y,val; Int_t ctpoint=0; for(Int_t i=1;i<=nbinsx;i++){ x=h->GetXaxis()->GetBinCenter(i); if(windowx) { if(xxmax) continue;} for(Int_t j=1;j<=nbinsy;j++){ y=h->GetYaxis()->GetBinCenter(j); if(windowy) { if(yymax) continue;} val=h->GetBinContent(i,j); if(val==0)continue; g->SetPoint(ctpoint,x,y); ctpoint++; } } return g; } TGraphErrors* HTool::hist2DToGraphErrors(TH2* h, Float_t xmin,Float_t xmax, Float_t ymin,Float_t ymax ) { // Puts h into TGraphErrors . // With xmin/xmax/ymin,ymax a region can be selected. if(h==0) return 0; Int_t nbinsx=h->GetNbinsX(); Int_t nbinsy=h->GetNbinsY(); Bool_t windowx=kFALSE; if(xmin!=0||xmax!=0) windowx=kTRUE; Bool_t windowy=kFALSE; if(ymin!=0||ymax!=0) windowy=kTRUE; TString name=h->GetName(); name+="_gTH2"; TGraphErrors* g=0; g=new TGraphErrors(); g->SetName(name.Data()); Double_t max=h->GetMaximum(); if(max==0) { delete g; return 0; } Double_t x,y,val,err; Int_t ctpoint=0; for(Int_t i=1;i<=nbinsx;i++){ x=h->GetXaxis()->GetBinCenter(i); if(windowx) { if(xxmax) continue;} for(Int_t j=1;j<=nbinsy;j++){ y=h->GetYaxis()->GetBinCenter(j); if(windowy) { if(yymax) continue;} val=h->GetBinContent(i,j); if(val==0)continue; g->SetPoint(ctpoint,x,y); err=h->GetBinError(i,j); g->SetPointError(ctpoint,err,err); ctpoint++; } } return g; } void HTool::histToText(TH1* h, TString filename) { // put 1D histogram to ascii file with following // format : // // Minv BinContent BinStatError BinWidth ofstream out; out.open(filename.Data()); Int_t nbinx = h->GetNbinsX(); out <<"Minv BinContent BinStatError BinWidth"<GetXaxis()->GetBinCenter(i+1)<<" "<GetBinContent(i+1) <<" "<GetBinError(i+1)<<" "<GetXaxis()->GetBinWidth(i+1)<GetXaxis()->GetBinCenter(i+1)<<" "<GetBinContent(i+1) <<" "<GetBinError(i+1)<<" "<GetXaxis()->GetBinWidth(i+1)<GetName(); myname += "_graph"; } else { myname = newname; } if(exchange) HTool::exchangeXYGraph(g); Int_t nBinX = g->GetN(); Double_t* X = g->GetX(); Double_t* Y = g->GetY(); if(nBinX < 1) return result; //------------------------------------------ // create the Matrix for the equation system // and init the values Int_t nDim = nBinX - 1; TMatrixD A(nDim,nDim); TMatrixD B(nDim,1); Double_t* aA = A.GetMatrixArray(); Double_t* aB = B.GetMatrixArray(); memset(aA,0,nDim * nDim * sizeof(Double_t)); memset(aB,0,nDim * sizeof(Double_t)); //------------------------------------------ //------------------------------------------ // setup equation system // width for 1st bin is given therefore // we shift bin parameter (column) by one to the left // to reduce the matrix size Double_t* xAxis = new Double_t [nBinX + 1]; Double_t* binW = new Double_t [nBinX ]; binW[0] = firstBinWidth; aB[0] = X[1] - X[0] - 0.5 * binW[0]; aA[0] = 0.5; for(Int_t col = 1; col < nDim ; col ++) { Int_t row = col; aB[col] = X[col + 1] - X[ col ]; aA[row * nDim + col - 1 ] = 0.5; aA[row * nDim + col ] = 0.5; } //------------------------------------------ //------------------------------------------ // solve the equations A.Invert(); TMatrixD C = A * B; //------------------------------------------ //------------------------------------------ // calculate the bin boundaries xAxis[0] = X[0] - 0.5 * binW[0]; memcpy(&binW[1],C.GetMatrixArray(),nDim * sizeof(Double_t)); for(Int_t col = 0; col < nBinX ; col ++) { xAxis[col + 1] = X[col] + 0.5 * binW[col]; } //------------------------------------------ //------------------------------------------ // setup the final hist result = new TH1D(myname.Data(),myname.Data(),nBinX, xAxis); for(Int_t i = 0; i < nBinX; i ++){ result->SetBinContent(i + 1, Y[i]); } result->SetMarkerColor(markercolor); result->SetMarkerStyle(markerstyle); result->SetMarkerSize(markersize); //------------------------------------------ delete [] xAxis; delete [] binW; return result; } TH1D* HTool::graphErrorsToHist(TGraphErrors* g, Double_t firstBinWidth, TString newname,Bool_t exchange, Int_t markerstyle,Int_t markercolor,Float_t markersize) { // Creates a TH1D from TGraph g. The binwidth of the first bin has to // specified. The others bins are calculated automatically. Supports also Graphs // with non constant x steps. The axis of the Graph can be exchanged if // exchange=kTRUE (modifies the Graph). TH1D* result = HTool::graphToHist(g,firstBinWidth,newname,exchange, markerstyle,markercolor,markersize); if( result == 0) return result; //------------------------------------------ // setup the final hist Int_t nBinX = g->GetN(); Double_t* err = g->GetEY(); // error y is still ok even if exchanged for(Int_t i = 0; i < nBinX; i ++){ result->SetBinError(i + 1, err[i]); } if(exchange){ HTool::exchangeXYGraph(g); // undo what has been done in graphToHist HTool::exchangeXYGraphErrors(g); // now exchange including errors } return result; } Bool_t HTool::exchangeXYGraph(TGraph* g) { // exchanges x-values and y-values. if(g==0) return kFALSE; Int_t nbin=g->GetN(); Double_t x,y; for(Int_t i = 0; i < nbin; i ++) { g->GetPoint(i,x,y); g->SetPoint(i,y,x); } return kTRUE; } Bool_t HTool::exchangeXYGraphErrors(TGraphErrors* g) { // exchanges x-values and y-values and // corresponding errors. if(g==0) return kFALSE; Int_t nbin=g->GetN(); Double_t x,y; Double_t ex,ey; for(Int_t i = 0; i < nbin; i ++) { g->GetPoint(i,x,y); ex = g->GetErrorX(i); ey = g->GetErrorY(i); g->SetPoint(i,y,x); g->SetPointError(i,ey,ex); } return kTRUE; } Double_t HTool::integralGraph(TGraph* g,Int_t first,Int_t last){ // calulates the integral (culmulative sum) y-values. // By default all points are used. "first" and "last" defines // the range of points to integrate (including these points, counting from 0). // If "first" and "last" are no valid numbers inside the correct // range the range will be set to 0 and last point depending // which boundary has been violated. if(g == 0) return kFALSE; Int_t nbin = g->GetN(); Double_t x,y; Double_t sum = 0; if(first > last ){ first = 0; last = nbin - 1; cout<<"integralGraph(): lower bound range larger as upper bound ! setting first and last point instead."< nbin - 1) { last = nbin - 1; cout<<"integralGraph(): upper bound range not valid! setting last point instead."<GetPoint(i,x,y); sum += y; } return sum; } Bool_t HTool::scaleGraph(TGraph* g,Double_t xscale,Double_t yscale) { // scales x-values and y-values with xscale,yscale. if(g==0) return kFALSE; Int_t nbin=g->GetN(); Double_t x,y; for(Int_t i=0;iGetPoint(i,x,y); g->SetPoint(i,x*xscale,y*yscale); } return kTRUE; } Bool_t HTool::scaleGraphErrors(TGraphErrors* g, Double_t xscale,Double_t yscale, Double_t xErrscale,Double_t yErrscale) { // scales x-values and y-values with xscale,yscale. // scales x Err-values and yErr-values with xErrscale,yErrscale. if(g==0) return kFALSE; Int_t nbin=g->GetN(); Double_t x,y; Double_t xErr,yErr; for(Int_t i=0;iGetPoint(i,x,y); xErr = g->GetErrorX(i); yErr = g->GetErrorY(i); g->SetPoint(i,x*xscale,y*yscale); g->SetPointError(i,xErr * xErrscale, yErr * yErrscale); } return kTRUE; } Bool_t HTool::shiftGraph(TGraph* g,Double_t xshift,Double_t yshift) { // add shift to x-values and y-values if(g==0) return kFALSE; Int_t nbin=g->GetN(); Double_t x,y; for(Int_t i=0;iGetPoint(i,x,y); g->SetPoint(i,x+xshift,y+yshift); } return kTRUE; } //---------------------strings-------------------------------------- TString* HTool::parseString(TString options,Int_t& size,TString separator,Bool_t tolower) { // loops over TString options and find substrings separated by separator // and puts them to an array of TStrings. size will hold the size of this // array and the pointer to the array is returned. If tolower is kTRUE options // will be made toLower. if(tolower)options.ToLower(); options.ReplaceAll(" ",""); Ssiz_t len=options.Length(); TString* sarray=0; Int_t count=0; if(len!=0) { TObjArray array; Char_t* mystring=(Char_t*)options.Data(); Char_t* buffer; TObjString* stemp; while(1) // find all token in option string and put them to a list { if(count==0) { buffer=strtok(mystring,separator.Data()); stemp=new TObjString(buffer); array.Add(stemp); } if(!(buffer=strtok(NULL,separator.Data())))break; stemp=new TObjString(buffer); array.Add(stemp); count++; } sarray= new TString [count+1]; for(Int_t i=0;iGetString(); } array.Delete(); } size=count+1; return sarray; } Bool_t HTool::findString(TString* classes,Int_t size,TString name) { // searches name in TString array classes with size size. // return kTRUE if the string is found. for(Int_t i=0;iAdd(new TH1F(name.Data(),name.Data(),nbins,xmin,xmax)); } } else { myhists->Add(new TH1F(name.Data(),name.Data(),nbins,xmin,xmax)); } }else { cout<<"readHistsDescription(): Wrong number of arguments!"<Draw() is used to fill the hists. evs is the number of entries to loop over. if evs=0 // the full size of the tree will be used. // INPUT: // histdescriptionfile is the file with description of the histograms to fill (ascii). // if histdescriptionfile=="" histograms will be filled generic and a histdescription file // histDescription.txt will be generated in the working directory. // listofClasses contains a list of Classes which should be taken into account, all other // classes will be ignored. if listofClasses=="" all classes in infile will be taken. // OUTPUT: // I. a root file treeHists.root will be generated which contains all histograms of the // selected variables // II. if histdescriptionfile has been empty an ascii file treeHistDescription.txt will // be generated in the working directory. // III. an ascii file treeHistResults.txt with the analysis results of the hists will be created, // which can be used for diff of different files. if (infile.CompareTo("")==0) { cout<<"No input file speciefied!"<cd(); TFile* in=new TFile(infile.Data(),"READ"); in->cd(); Int_t events=evs; TTree* T=(TTree*)in->Get("T"); if (evs==0) { events=(Int_t)T->GetEntries(); } TObjArray* classarray=new TObjArray(); TString keyname; TString keyclassname; //------------------Looping directories to find all categories in file-------------------------- // has to done only if no histdescription is used!! TList* keylist=0; Int_t sizekey =0; Int_t counter=0; if(!useDescription) { keylist=gDirectory->GetListOfKeys(); sizekey =keylist->LastIndex(); counter=0; for(Int_t i=0;iAt(i); keyname = key->GetName(); keyclassname= key->GetClassName(); if(keyclassname.CompareTo("TDirectory")==0) { cout<cd(keyname.Data()); TList* keylist2=gDirectory->GetListOfKeys(); //-------------------looping in dir for categories-------------------------------------- for(Int_t j=0;jLastIndex()+1;j++) { TKey* key2 =(TKey*)keylist2->At(j); keyname = key2->GetName(); keyclassname= key2->GetClassName(); if(keyclassname.Contains("Category")!=0) { if(useClassList) { if(HTool::findString(&myclassNames[0],numberclasses,keyname)) { counter++; classarray->Add(key2); } } else { counter++; classarray->Add(key2); } } } } } classarray->Expand(classarray->GetEntries()); cout<<"Number of Categries: "<GetEntries() <LastIndex()+1;j++) { cout<<"###########################################################"<At(j); classname=k->GetName(); cout<GetClass(k->GetName(),1); TList* list=HTool::getListOfAllDataMembers(cl); Int_t size =list->LastIndex(); for(Int_t i=0;iAt(i); varname = member->GetName(); if(varname.Contains("fgIsA")==0) { branch=classname+".fData."+varname; cout<cd(); T->Draw(branch,"","",events,0); TH1F *htemp = (TH1F*)gPad->GetPrimitive("htemp"); if(htemp) { branch=classname+"."+varname; htemp->SetName(branch); htemp->SetTitle(branch); HTool::printHistInfo(htemp); HTool::writeHistInfo(htemp,anaout); fprintf(histout,"%s , %i , %f , %f \n", htemp->GetName(), htemp->GetNbinsX(), htemp->GetXaxis()->GetXmin(), htemp->GetXaxis()->GetXmax()); out->cd(); htemp->Write(); in->cd(); } } } } fclose(histout); fclose(anaout); } else { cout<<"Do not use description"<Expand(classarray->GetEntries()); cout<<"Number of Hists: "<GetEntries() <LastIndex()+1;j++) { cout<<"###########################################################"<At(j); classname=h->GetName(); classname.Resize(classname.First(".")); varname =h->GetName(); Int_t st=varname.Last('.'); for(Int_t ii=0;ii>"+h->GetName(); cout<cd(); T->Draw(branch,"","",events,0); if(h) { HTool::printHistInfo(h); HTool::writeHistInfo(h,anaout); out->cd(); h->Write(); in->cd(); } } fclose(anaout); } delete result; classarray->Delete(); delete classarray; out->Save(); out->Close(); return kTRUE; } Bool_t HTool::drawHistComp(TString nameinp1,TString nameinp2,TString name,TCanvas* comp,Int_t padn) { // Gets histogram with name name from 2 input files and plots them into // into a canvas. If the canvas pointer is 0 a canvas will be created. // if padn=1 they will be overlayed in on pad, if pad=2 and comp=0, a // new canvas with n pads will be created and the 2 hists will be plotted single // if n=2 and comp not equal 0, the 2 pads of comp will used to draw. if(name.CompareTo("")==0) { cout<<"drawHistComp(): No Histogram name specified!"<GetListOfKeys(); Int_t sizekey =keylist->LastIndex(); TString keyname; TString keyclassname; for(Int_t i=0;iAt(i); keyname = key->GetName(); keyclassname= key->GetClassName(); if(keyclassname.Contains("TH1")!=0) { TString temp=keyname; temp.Resize(temp.First(".")); if(HTool::findString(&myclasses[0],nclasses,temp)) longlist=longlist+keyname+","; } } } Int_t size=0;TString* mynames=0; if(!getAll)mynames= HTool::parseString(name,size,",",kFALSE); else mynames= HTool::parseString(longlist,size,",",kFALSE); if(comp==0) { // only if no canvas from outside is used comp=new TCanvas("compare","compare",1000,800); if(padn==2&&size==1) { // if 2 pads are used and no multiple input comp ->Divide(2,1); } else if(padn==1&&size==1) { // if 1 pads are used and no multiple input } else { // if multiple input is used Int_t dim2=0; Int_t dim1=(Int_t)sqrt((Float_t)size); if(dim1*(dim1+1)Divide(dim1,dim2); } if(size==1) { // if no multiple input TH1* h1=HTool::getHist(inp1,name); TH1* h2=HTool::getHist(inp2,name); if(!h1 || !h2) return kFALSE; h1->SetDirectory(0); h2->SetDirectory(0); h1->SetLineColor(2); h2->SetLineColor(4); if(padn==1) { if(h1->GetMaximum()>=h2->GetMaximum()) { h1->DrawCopy(); h2->DrawCopy("same"); } else { h2->DrawCopy(); h1->DrawCopy("same"); } } else { comp->cd(1); h1->DrawCopy(); comp->cd(2); h2->DrawCopy(); } HTool::printCompHistInfo(h1,h2); } else if(padn==1&&size==1) { // if 1 pads are used and no multiple input } else { // if multiple input is used for(Int_t i=0;icd(i+1); TH1* h1=HTool::getHist(inp1,mynames[i]); TH1* h2=HTool::getHist(inp2,mynames[i]); if(!h1 || !h2) return kFALSE; h1->SetDirectory(0); h2->SetDirectory(0); h1->SetLineColor(2); h2->SetLineColor(4); if(h1->GetMaximum()>=h2->GetMaximum()) { h1->DrawCopy(); h2->DrawCopy("same"); } else { h2->DrawCopy(); h1->DrawCopy("same"); } HTool::printCompHistInfo(h1,h2); } } } else { // only if canvas from outside is used TH1* h1=HTool::getHist(inp1,name); TH1* h2=HTool::getHist(inp2,name); if(!h1 || !h2) return kFALSE; h1->SetDirectory(0); h2->SetDirectory(0); h1->SetLineColor(2); h2->SetLineColor(4); if(padn==1) { comp->cd(); if(h1->GetMaximum()>=h2->GetMaximum()) { h1->DrawCopy(); h2->DrawCopy("same"); } else { h2->DrawCopy(); h1->DrawCopy("same"); } HTool::printCompHistInfo(h1,h2); } else if(padn==2) { comp->cd(1); h1->DrawCopy(); comp->cd(2); h2->DrawCopy(); HTool::printCompHistInfo(h1,h2); } inp1->Close(); inp2->Close(); } return kTRUE; } Bool_t HTool::compHistFiles(TString nameinp1,TString nameinp2,TString name) { // Gets histogram with name name from 2 input files and compares them by // mean,rms,range,maximum,maxposition, entries.... if(nameinp1.CompareTo("")==0||nameinp2.CompareTo("")==0) { cout<<"compHistFiles(): File names not specified !"<GetListOfKeys(); Int_t sizekey =keylist->LastIndex(); TString keyname; TString keyclassname; for(Int_t i=0;iAt(i); keyname = key->GetName(); keyclassname= key->GetClassName(); if(keyclassname.Contains("TH1")!=0) { TString temp=keyname; temp.Resize(temp.First(".")); if(HTool::findString(&myclasses[0],nclasses,temp)) longlist=longlist+keyname+","; } } mynames= HTool::parseString(longlist,size,",",kFALSE); useList=kTRUE; } inp1->cd(); TList* keylist=gDirectory->GetListOfKeys(); Int_t sizekey =keylist->LastIndex(); TString keyname; TString keyclassname; for(Int_t i=0;iAt(i); keyname = key->GetName(); keyclassname= key->GetClassName(); if(keyclassname.Contains("TH1")!=0) { if(useList) { if(HTool::findString(&mynames[0],size,keyname)) { TH1* h1=HTool::getHist(inp1,keyname); TH1* h2=HTool::getHist(inp2,keyname); if(! h1 || !h2) continue; HTool::printCompHistInfo(h1,h2,1); } } else { TH1* h1=HTool::getHist(inp1,keyname); TH1* h2=HTool::getHist(inp2,keyname); if(! h1 || !h2) continue; HTool::printCompHistInfo(h1,h2,1); } } } return kTRUE; } Bool_t HTool::printHistInfo(TH1* h1) { // prints information over hist to screen . if(!h1) return kFALSE; cout<<"-----------------------------------------------------------"<GetName()<GetMean()<GetRMS()<GetEntries()<Integral()<GetMaximum()<GetBinCenter(h1->GetMaximumBin())<GetBinContent(0)<GetBinContent(h1->GetNbinsX()+1)<GetNbinsX()<GetXaxis()->GetXmin()<GetXaxis()->GetXmax()<GetName()<Clone(); TH1* h2tmp=(TH1*)h2->Clone(); Double_t prob=h1->KolmogorovTest(h2); cout<<" Kolmogorov Prob. : "<Add(h2tmp,-1); cout<<" Integral after Substr. : "<Integral()<GetMaximum()<GetBinCenter(h1tmp->GetMaximumBin())<GetMinimum()<GetBinCenter(h1tmp->GetMinimumBin())<GetMean() <<" : "<GetMean()<GetRMS() <<" : "<GetRMS()<GetEntries() <<" : "<GetEntries()<Integral() <<" : "<Integral()<GetMaximum() <<" : "<GetMaximum()<GetBinCenter(h1->GetMaximumBin())<<" : "<GetBinCenter(h2->GetMaximumBin())<GetBinContent(0) <<" : "<GetBinContent(0)<GetBinContent(h1->GetNbinsX()+1) <<" : "<GetBinContent(h2->GetNbinsX()+1)<GetNbinsX() <<" : "<GetNbinsX()<GetXaxis()->GetXmin() <<" : "<GetXaxis()->GetXmin()<GetXaxis()->GetXmax() <<" : "<GetXaxis()->GetXmax()<GetName()); fprintf(anaout," Mean : %f\n",h1->GetMean()); fprintf(anaout," RMS : %f\n",h1->GetRMS()); fprintf(anaout," Entries : %f\n",h1->GetEntries()); fprintf(anaout," Integral : %f\n",h1->Integral()); fprintf(anaout," Maximum : %f\n",h1->GetMaximum()); fprintf(anaout," Maximum X : %f\n",h1->GetBinCenter(h1->GetMaximumBin())); fprintf(anaout," UnderFlow : %f\n",h1->GetBinContent(0)); fprintf(anaout," OverFlow : %f\n",h1->GetBinContent(h1->GetNbinsX()+1)); fprintf(anaout," nBins : %i\n",h1->GetNbinsX()); fprintf(anaout," xMin : %f\n",h1->GetXaxis()->GetXmin()); fprintf(anaout," xMax : %f\n",h1->GetXaxis()->GetXmax()); fprintf(anaout,"-----------------------------------------------------------\n"); return kTRUE; }