ROOT logo
#include "hhistconverter.h"


#include "TH1D.h"
#include "TH2D.h"
#include "TH3D.h"

#include <iostream>
#include <iomanip>
using namespace std;

//_HADES_CLASS_DESCRIPTION
///////////////////////////////////////////////////////////
// HHistConverter
//
// Stores/reads Histograms in linear TArrayD. Supports
// TH1D, TH2D, TH3D and non constant binning.
// Helper class for using histograms in HParCond
// containers. Only array data will be stored to ORACLE.
// The Data can therefore be displayed in ORACLE or ASCII
// file format.
// To fill the Array linData one can fill first Histograms
// and convert them using the function
//
// static void  HHistConverter::fillArray(const TH1* h,TArrayD& linData,TString name,Int_t nvals,Int_t width, Bool_t print)
//
// where name (lable), nvals (number of displayed values /row),
// width (formating digits for the fields in row and print
// are options to display the result of the convertion.
//
// From an existing array a Histogram cand be constructed with
//
// static TH1* HHistConverter::createHist(TString name,const TArrayD& linData)
//
// The created histogram with name "name" is dispatched from the
// directory (hist->SetDirectory(0)).
// The Array has the following structure:
//
// array[0]                    = dimension of Histogram (1-3)
// array[1]                    = n Bins in x
// array[2]                    = n Bins in y (0 if 1 dim)
// array[3]                    = n Bins in z (0 if 1 or 2 dim)
// array[4 - 4 + nbinsx + 1]   = x axis  (nBinx+1 values)
// array[....]                 = y axis  (if dim > 1, nBiny+1 values)
// array[....]                 = z axis  (if dim > 2, nBinz+1 values)
// array[....]                 = data of histcontent linearized form
//
// linearization (bin starting from 0):
//                  1 dim     ind = xbin
//                  2 dim     ind = xbin * nBiny + ybin
//                  3 dim     ind = xbin * nBiny * nBinz + ybin * nBinz + zbin
//
//
///////////////////////////////////////////////////////////

ClassImp(HHistConverter)


HHistConverter::HHistConverter()
{
}

HHistConverter::~HHistConverter()
{

}

void HHistConverter::printArray(const TArray& dat,Int_t nvals,Int_t width,Int_t start,Int_t end)
{
    // prints content of array with n values /row, each data field
    // with width digits from index start to index end (both included)

    Int_t size = dat.GetSize();
    if(start < 0 || start+1 >= size || start >= end   ) start = 0;
    if(end   < 0 || end+1   >= size || end   <= start ) end   = size;
    cout<<"        "<<flush;
    Int_t ct = 1;
    Int_t n  = end - start;
    for(Int_t i = start+1; i < end+1; i ++){
	if (ct%nvals == 0 && ct > 1 && ct != n )  { // break to new line
	    cout<<setw(width)<<dat.GetAt(i-1)<<" \\ "<<endl;
	    cout<<"        "<<flush;
	}
	else if  (ct == n )  cout<<setw(width)<<dat.GetAt(i-1)<<flush;         // end reached
	else                 cout<<setw(width)<<dat.GetAt(i-1)<<" "<<flush;   // inside line

	ct++;
    }
    cout<<endl;
}

void HHistConverter::writeArray(ostream& out,TString name,const TArray& dat,Int_t nvals)
{
    // write content of array with n values /row to stream out.
    // A lable name and and type specifier is added. Format is
    // compatible to HParCond ascii file format.

    Int_t maxbin = dat.GetSize();
    out<<name.Data()<<": Double_t \\"<<endl;
    out<<"        "<<flush;
    for(Int_t i = 1; i < maxbin+1; i ++){
	if ( (i%(nvals) == 0) && i > 1 && i != maxbin )  {   // break to new line
	    out<<dat.GetAt(i-1)<<" \\ "<<endl;
	    out<<"        "<<flush;
	}
	else if                          (i == maxbin )  {out<<dat.GetAt(i-1)<<flush; }        // end reached
	else                                             {out<<dat.GetAt(i-1)<<" "<<flush; }  // inside line

    }
    out<<endl;
}

void HHistConverter::printArrayInfo(const TArrayD& linData,TString name,Int_t nvals,Int_t width)
{
    // print the information of a TArrayD containing
    // linearized histogram data. Axis and data are
    // displayed with n values nvalues per/row, each
    // data field with width digits.

    Int_t dim  = (Int_t)linData[0];    // dimension of hist
    Int_t xbin = (Int_t)linData[1];    // n bins x
    Int_t ybin = (Int_t)linData[2];    // n bins y
    Int_t zbin = (Int_t)linData[3];    // n bins z

    Int_t offsetData = 0;
    Int_t offsetx    = 4;
    Int_t offsety    = offsetx + (xbin + 1);
    Int_t offsetz    = offsety + (ybin + 1);

    Int_t maxbin = 0;
    if(dim == 1) { maxbin = xbin;               offsetData = offsetx + (xbin + 1);}
    if(dim == 2) { maxbin = xbin * ybin;        offsetData = offsety + (ybin + 1);}
    if(dim == 3) { maxbin = xbin * ybin * zbin; offsetData = offsetz + (zbin + 1);}

    cout<<"---------------------------------------------------------------"<<endl;
    if(dim == 1) cout<<"info : "<<name.Data()<<", nBin = "<<xbin  <<endl;
    if(dim == 2) cout<<"info : "<<name.Data()
	<<", nBin = "<<maxbin
	    <<", nx = "  <<xbin
	    <<", ny = "  <<ybin<<", stored linear : ind = x * ybin + y"<<endl;
    if(dim == 3) cout<<"info : "<<name.Data()
	<<", nBin = "<<maxbin
	    <<", nx = "  <<xbin
	    <<", ny = "  <<ybin
	    <<", nz = "  <<zbin<<", stored linear : ind = x * (ybin*zbin) + y*zbin + z"<<endl;

    if(dim > 0){
	cout<<"x axis : "<<endl;
	printArray(linData,nvals,width,offsetx,offsetx + xbin + 1);
    }
    if(dim > 1) {
	cout<<"y axis : "<<endl;
	printArray(linData,nvals,width,offsety,offsety + ybin + 1);
    }
    if(dim == 3) {
	cout<<"z axis : "<<endl;
	printArray(linData,nvals,width,offsetz,offsetz + zbin + 1);
    }
    cout<<"data : "<<endl;
    printArray(linData,nvals,width,offsetData,offsetData + maxbin);
}


TH1* HHistConverter::createHist(TString name,const TArrayD& linData)
{
    // creates a histogram withname name from an TArrayD.
    // supports 1,2 and 3 dim Histograms of type Double_t.

    TH1* h = 0;
    TArrayD& linDat = (TArrayD&)linData;
    Int_t dim  = (Int_t)linData[0];    // dimension of hist
    Int_t xbin = (Int_t)linData[1];    // n bins x
    Int_t ybin = (Int_t)linData[2];    // n bins y
    Int_t zbin = (Int_t)linData[3];    // n bins z

    Int_t offsetData = 0;
    Int_t offsetx    = 4;
    Int_t offsety    = offsetx + (xbin + 1);
    Int_t offsetz    = offsety + (ybin + 1);

    if(dim == 1) { offsetData = offsetx + (xbin + 1);}
    if(dim == 2) { offsetData = offsety + (ybin + 1);}
    if(dim == 3) { offsetData = offsetz + (zbin + 1);}

    if     (dim == 1){

	h = (TH1*) new TH1D(name.Data(),name.Data(),xbin,&linDat[offsetx]);

	for(Int_t i = 0; i < xbin; i ++){
	    h->SetBinContent(i+1,linData.At(offsetData + i));
	}
    } else if(dim == 2) {
	h = (TH1*) new TH2D(name.Data(),name.Data()
			    ,xbin,&linDat[offsetx]
			    ,ybin,&linDat[offsety]
			   );
	Int_t ind;
	for(Int_t i = 0; i < xbin; i ++){
	    for(Int_t j = 0; j < ybin; j ++){
		ind = i * ybin + j;
		h->SetBinContent(i+1,j+1,linData.At(offsetData + ind) );
	    }
	}
    } else if(dim == 3) {
	Int_t ind;
	h = (TH1*) new TH3D(name.Data(),name.Data()
			    ,xbin,&linDat[offsetx]
			    ,ybin,&linDat[offsety]
			    ,zbin,&linDat[offsetz]
			   );

	for(Int_t i = 0; i < xbin; i ++){
	    for(Int_t j = 0; j < ybin; j ++){
		for(Int_t k = 0; k < zbin; k ++){
		    ind = i * ybin*zbin + j*ybin + j;
		    h->SetBinContent(i+1,j+1,k+1,linData.At(offsetData + ind) );
		}
	    }
	}
    }
    h->SetDirectory(0);

    return h;
}
void    HHistConverter::fillArray(const TH1* h,TArrayD& linData,TString name,Int_t nvals,Int_t width, Bool_t print)
{
    // To fill the Array linData from histogram h
    // where name (lable), nvals (number of displayed values /row),
    // width (formating digits for the fields in row and print
    // are options to display the result of the convertion.

    Int_t dim  = h->GetDimension();
    Int_t xbin = h->GetNbinsX();
    Int_t ybin = h->GetNbinsY();
    Int_t zbin = h->GetNbinsZ();

    Int_t offsetData = 0;
    Int_t offsetx    = 4;
    Int_t offsety    = offsetx + (xbin + 1);
    Int_t offsetz    = offsety + (ybin + 1);

    Int_t maxbin = 0;
    if(dim == 1) { maxbin = xbin;               offsetData = offsetx + (xbin + 1); ybin = zbin = 0;}
    if(dim == 2) { maxbin = xbin * ybin;        offsetData = offsety + (ybin + 1); zbin = 0;}
    if(dim == 3) { maxbin = xbin * ybin * zbin; offsetData = offsetz + (zbin + 1);}

    linData.Set(offsetData + maxbin);

    linData[0] = dim;
    linData[1] = xbin;
    linData[2] = ybin;
    linData[3] = zbin;

    //-----------------------------------------------------------------------------
    // Filling axis
    for(Int_t i = 0; i < xbin; i ++){
	linData.SetAt(h->GetXaxis()->GetBinLowEdge(i + 1),offsetx + i);
    }
    linData.SetAt(h->GetXaxis()->GetBinUpEdge(xbin),offsetx + xbin);


    if(dim >= 2){
	for(Int_t i = 0; i < ybin; i ++){
	    linData.SetAt(h->GetYaxis()->GetBinLowEdge(i + 1),offsety + i);
	}
	linData.SetAt(h->GetYaxis()->GetBinUpEdge(ybin),offsety + ybin);
    }
    if(dim == 3){
	for(Int_t i = 0; i < zbin; i ++){
	    linData.SetAt(h->GetZaxis()->GetBinLowEdge(i + 1),offsetz + i);
	}
	linData.SetAt(h->GetZaxis()->GetBinUpEdge(zbin),offsetz + zbin);
    }

    //-----------------------------------------------------------------------------
    // Filling data
    if(dim == 1){
	for(Int_t i = 0; i < xbin; i ++){
	    linData.SetAt(h->GetBinContent(i + 1),offsetData + i);
	}
    } else if (dim == 2){
	Int_t ind;

	for(Int_t i = 0; i < xbin; i ++){
	    for(Int_t j = 0; j < ybin; j ++){
		ind = i * ybin + j;
		linData.SetAt(h->GetBinContent(i + 1,j + 1),offsetData + ind);
	    }
	}
    } else if (dim == 3){
	Int_t ind;

	for(Int_t i = 0; i < xbin; i ++){
	    for(Int_t j = 0; j < ybin; j ++){
		for(Int_t k = 0; k < zbin; k ++){
		    ind = i * ybin * zbin + j * ybin + k;
		    linData.SetAt(h->GetBinContent(i + 1,j + 1,k + 1),offsetData + ind);
		}
	    }
	}
    }
    if(print){
	printArrayInfo(linData,name,nvals,width);
    }

}
 hhistconverter.cc:1
 hhistconverter.cc:2
 hhistconverter.cc:3
 hhistconverter.cc:4
 hhistconverter.cc:5
 hhistconverter.cc:6
 hhistconverter.cc:7
 hhistconverter.cc:8
 hhistconverter.cc:9
 hhistconverter.cc:10
 hhistconverter.cc:11
 hhistconverter.cc:12
 hhistconverter.cc:13
 hhistconverter.cc:14
 hhistconverter.cc:15
 hhistconverter.cc:16
 hhistconverter.cc:17
 hhistconverter.cc:18
 hhistconverter.cc:19
 hhistconverter.cc:20
 hhistconverter.cc:21
 hhistconverter.cc:22
 hhistconverter.cc:23
 hhistconverter.cc:24
 hhistconverter.cc:25
 hhistconverter.cc:26
 hhistconverter.cc:27
 hhistconverter.cc:28
 hhistconverter.cc:29
 hhistconverter.cc:30
 hhistconverter.cc:31
 hhistconverter.cc:32
 hhistconverter.cc:33
 hhistconverter.cc:34
 hhistconverter.cc:35
 hhistconverter.cc:36
 hhistconverter.cc:37
 hhistconverter.cc:38
 hhistconverter.cc:39
 hhistconverter.cc:40
 hhistconverter.cc:41
 hhistconverter.cc:42
 hhistconverter.cc:43
 hhistconverter.cc:44
 hhistconverter.cc:45
 hhistconverter.cc:46
 hhistconverter.cc:47
 hhistconverter.cc:48
 hhistconverter.cc:49
 hhistconverter.cc:50
 hhistconverter.cc:51
 hhistconverter.cc:52
 hhistconverter.cc:53
 hhistconverter.cc:54
 hhistconverter.cc:55
 hhistconverter.cc:56
 hhistconverter.cc:57
 hhistconverter.cc:58
 hhistconverter.cc:59
 hhistconverter.cc:60
 hhistconverter.cc:61
 hhistconverter.cc:62
 hhistconverter.cc:63
 hhistconverter.cc:64
 hhistconverter.cc:65
 hhistconverter.cc:66
 hhistconverter.cc:67
 hhistconverter.cc:68
 hhistconverter.cc:69
 hhistconverter.cc:70
 hhistconverter.cc:71
 hhistconverter.cc:72
 hhistconverter.cc:73
 hhistconverter.cc:74
 hhistconverter.cc:75
 hhistconverter.cc:76
 hhistconverter.cc:77
 hhistconverter.cc:78
 hhistconverter.cc:79
 hhistconverter.cc:80
 hhistconverter.cc:81
 hhistconverter.cc:82
 hhistconverter.cc:83
 hhistconverter.cc:84
 hhistconverter.cc:85
 hhistconverter.cc:86
 hhistconverter.cc:87
 hhistconverter.cc:88
 hhistconverter.cc:89
 hhistconverter.cc:90
 hhistconverter.cc:91
 hhistconverter.cc:92
 hhistconverter.cc:93
 hhistconverter.cc:94
 hhistconverter.cc:95
 hhistconverter.cc:96
 hhistconverter.cc:97
 hhistconverter.cc:98
 hhistconverter.cc:99
 hhistconverter.cc:100
 hhistconverter.cc:101
 hhistconverter.cc:102
 hhistconverter.cc:103
 hhistconverter.cc:104
 hhistconverter.cc:105
 hhistconverter.cc:106
 hhistconverter.cc:107
 hhistconverter.cc:108
 hhistconverter.cc:109
 hhistconverter.cc:110
 hhistconverter.cc:111
 hhistconverter.cc:112
 hhistconverter.cc:113
 hhistconverter.cc:114
 hhistconverter.cc:115
 hhistconverter.cc:116
 hhistconverter.cc:117
 hhistconverter.cc:118
 hhistconverter.cc:119
 hhistconverter.cc:120
 hhistconverter.cc:121
 hhistconverter.cc:122
 hhistconverter.cc:123
 hhistconverter.cc:124
 hhistconverter.cc:125
 hhistconverter.cc:126
 hhistconverter.cc:127
 hhistconverter.cc:128
 hhistconverter.cc:129
 hhistconverter.cc:130
 hhistconverter.cc:131
 hhistconverter.cc:132
 hhistconverter.cc:133
 hhistconverter.cc:134
 hhistconverter.cc:135
 hhistconverter.cc:136
 hhistconverter.cc:137
 hhistconverter.cc:138
 hhistconverter.cc:139
 hhistconverter.cc:140
 hhistconverter.cc:141
 hhistconverter.cc:142
 hhistconverter.cc:143
 hhistconverter.cc:144
 hhistconverter.cc:145
 hhistconverter.cc:146
 hhistconverter.cc:147
 hhistconverter.cc:148
 hhistconverter.cc:149
 hhistconverter.cc:150
 hhistconverter.cc:151
 hhistconverter.cc:152
 hhistconverter.cc:153
 hhistconverter.cc:154
 hhistconverter.cc:155
 hhistconverter.cc:156
 hhistconverter.cc:157
 hhistconverter.cc:158
 hhistconverter.cc:159
 hhistconverter.cc:160
 hhistconverter.cc:161
 hhistconverter.cc:162
 hhistconverter.cc:163
 hhistconverter.cc:164
 hhistconverter.cc:165
 hhistconverter.cc:166
 hhistconverter.cc:167
 hhistconverter.cc:168
 hhistconverter.cc:169
 hhistconverter.cc:170
 hhistconverter.cc:171
 hhistconverter.cc:172
 hhistconverter.cc:173
 hhistconverter.cc:174
 hhistconverter.cc:175
 hhistconverter.cc:176
 hhistconverter.cc:177
 hhistconverter.cc:178
 hhistconverter.cc:179
 hhistconverter.cc:180
 hhistconverter.cc:181
 hhistconverter.cc:182
 hhistconverter.cc:183
 hhistconverter.cc:184
 hhistconverter.cc:185
 hhistconverter.cc:186
 hhistconverter.cc:187
 hhistconverter.cc:188
 hhistconverter.cc:189
 hhistconverter.cc:190
 hhistconverter.cc:191
 hhistconverter.cc:192
 hhistconverter.cc:193
 hhistconverter.cc:194
 hhistconverter.cc:195
 hhistconverter.cc:196
 hhistconverter.cc:197
 hhistconverter.cc:198
 hhistconverter.cc:199
 hhistconverter.cc:200
 hhistconverter.cc:201
 hhistconverter.cc:202
 hhistconverter.cc:203
 hhistconverter.cc:204
 hhistconverter.cc:205
 hhistconverter.cc:206
 hhistconverter.cc:207
 hhistconverter.cc:208
 hhistconverter.cc:209
 hhistconverter.cc:210
 hhistconverter.cc:211
 hhistconverter.cc:212
 hhistconverter.cc:213
 hhistconverter.cc:214
 hhistconverter.cc:215
 hhistconverter.cc:216
 hhistconverter.cc:217
 hhistconverter.cc:218
 hhistconverter.cc:219
 hhistconverter.cc:220
 hhistconverter.cc:221
 hhistconverter.cc:222
 hhistconverter.cc:223
 hhistconverter.cc:224
 hhistconverter.cc:225
 hhistconverter.cc:226
 hhistconverter.cc:227
 hhistconverter.cc:228
 hhistconverter.cc:229
 hhistconverter.cc:230
 hhistconverter.cc:231
 hhistconverter.cc:232
 hhistconverter.cc:233
 hhistconverter.cc:234
 hhistconverter.cc:235
 hhistconverter.cc:236
 hhistconverter.cc:237
 hhistconverter.cc:238
 hhistconverter.cc:239
 hhistconverter.cc:240
 hhistconverter.cc:241
 hhistconverter.cc:242
 hhistconverter.cc:243
 hhistconverter.cc:244
 hhistconverter.cc:245
 hhistconverter.cc:246
 hhistconverter.cc:247
 hhistconverter.cc:248
 hhistconverter.cc:249
 hhistconverter.cc:250
 hhistconverter.cc:251
 hhistconverter.cc:252
 hhistconverter.cc:253
 hhistconverter.cc:254
 hhistconverter.cc:255
 hhistconverter.cc:256
 hhistconverter.cc:257
 hhistconverter.cc:258
 hhistconverter.cc:259
 hhistconverter.cc:260
 hhistconverter.cc:261
 hhistconverter.cc:262
 hhistconverter.cc:263
 hhistconverter.cc:264
 hhistconverter.cc:265
 hhistconverter.cc:266
 hhistconverter.cc:267
 hhistconverter.cc:268
 hhistconverter.cc:269
 hhistconverter.cc:270
 hhistconverter.cc:271
 hhistconverter.cc:272
 hhistconverter.cc:273
 hhistconverter.cc:274
 hhistconverter.cc:275
 hhistconverter.cc:276
 hhistconverter.cc:277
 hhistconverter.cc:278
 hhistconverter.cc:279
 hhistconverter.cc:280
 hhistconverter.cc:281
 hhistconverter.cc:282
 hhistconverter.cc:283
 hhistconverter.cc:284
 hhistconverter.cc:285
 hhistconverter.cc:286
 hhistconverter.cc:287
 hhistconverter.cc:288
 hhistconverter.cc:289
 hhistconverter.cc:290
 hhistconverter.cc:291
 hhistconverter.cc:292
 hhistconverter.cc:293
 hhistconverter.cc:294
 hhistconverter.cc:295
 hhistconverter.cc:296
 hhistconverter.cc:297
 hhistconverter.cc:298
 hhistconverter.cc:299
 hhistconverter.cc:300
 hhistconverter.cc:301
 hhistconverter.cc:302
 hhistconverter.cc:303
 hhistconverter.cc:304
 hhistconverter.cc:305
 hhistconverter.cc:306