HYDRA_development_version
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hhistconverter.cc
Go to the documentation of this file.
1 #include "hhistconverter.h"
2 
3 
4 #include "TH1D.h"
5 #include "TH2D.h"
6 #include "TH3D.h"
7 
8 #include <iostream>
9 #include <iomanip>
10 using namespace std;
11 
12 //_HADES_CLASS_DESCRIPTION
13 ///////////////////////////////////////////////////////////
14 // HHistConverter
15 //
16 // Stores/reads Histograms in linear TArrayD. Supports
17 // TH1D, TH2D, TH3D and non constant binning.
18 // Helper class for using histograms in HParCond
19 // containers. Only array data will be stored to ORACLE.
20 // The Data can therefore be displayed in ORACLE or ASCII
21 // file format.
22 // To fill the Array linData one can fill first Histograms
23 // and convert them using the function
24 //
25 // static void HHistConverter::fillArray(const TH1* h,TArrayD& linData,TString name,Int_t nvals,Int_t width, Bool_t print)
26 //
27 // where name (lable), nvals (number of displayed values /row),
28 // width (formating digits for the fields in row and print
29 // are options to display the result of the convertion.
30 //
31 // From an existing array a Histogram cand be constructed with
32 //
33 // static TH1* HHistConverter::createHist(TString name,const TArrayD& linData)
34 //
35 // The created histogram with name "name" is dispatched from the
36 // directory (hist->SetDirectory(0)).
37 // The Array has the following structure:
38 //
39 // array[0] = dimension of Histogram (1-3)
40 // array[1] = n Bins in x
41 // array[2] = n Bins in y (0 if 1 dim)
42 // array[3] = n Bins in z (0 if 1 or 2 dim)
43 // array[4 - 4 + nbinsx + 1] = x axis (nBinx+1 values)
44 // array[....] = y axis (if dim > 1, nBiny+1 values)
45 // array[....] = z axis (if dim > 2, nBinz+1 values)
46 // array[....] = data of histcontent linearized form
47 //
48 // linearization (bin starting from 0):
49 // 1 dim ind = xbin
50 // 2 dim ind = xbin * nBiny + ybin
51 // 3 dim ind = xbin * nBiny * nBinz + ybin * nBinz + zbin
52 //
53 //
54 ///////////////////////////////////////////////////////////
55 
57 
58 
60 {
61 }
62 
64 {
65 
66 }
67 
68 void HHistConverter::printArray(const TArray& dat,Int_t nvals,Int_t width,Int_t start,Int_t end)
69 {
70  // prints content of array with n values /row, each data field
71  // with width digits from index start to index end (both included)
72 
73  Int_t size = dat.GetSize();
74  if(start < 0 || start+1 >= size || start >= end ) start = 0;
75  if(end < 0 || end+1 >= size || end <= start ) end = size;
76  cout<<" "<<flush;
77  Int_t ct = 1;
78  Int_t n = end - start;
79  for(Int_t i = start+1; i < end+1; i ++){
80  if (ct%nvals == 0 && ct > 1 && ct != n ) { // break to new line
81  cout<<setw(width)<<dat.GetAt(i-1)<<" \\ "<<endl;
82  cout<<" "<<flush;
83  }
84  else if (ct == n ) cout<<setw(width)<<dat.GetAt(i-1)<<flush; // end reached
85  else cout<<setw(width)<<dat.GetAt(i-1)<<" "<<flush; // inside line
86 
87  ct++;
88  }
89  cout<<endl;
90 }
91 
92 void HHistConverter::writeArray(ostream& out,TString name,const TArray& dat,Int_t nvals)
93 {
94  // write content of array with n values /row to stream out.
95  // A lable name and and type specifier is added. Format is
96  // compatible to HParCond ascii file format.
97 
98  Int_t maxbin = dat.GetSize();
99  out<<name.Data()<<": Double_t \\"<<endl;
100  out<<" "<<flush;
101  for(Int_t i = 1; i < maxbin+1; i ++){
102  if ( (i%(nvals) == 0) && i > 1 && i != maxbin ) { // break to new line
103  out<<dat.GetAt(i-1)<<" \\ "<<endl;
104  out<<" "<<flush;
105  }
106  else if (i == maxbin ) {out<<dat.GetAt(i-1)<<flush; } // end reached
107  else {out<<dat.GetAt(i-1)<<" "<<flush; } // inside line
108 
109  }
110  out<<endl;
111 }
112 
113 void HHistConverter::printArrayInfo(const TArrayD& linData,TString name,Int_t nvals,Int_t width)
114 {
115  // print the information of a TArrayD containing
116  // linearized histogram data. Axis and data are
117  // displayed with n values nvalues per/row, each
118  // data field with width digits.
119 
120  Int_t dim = (Int_t)linData[0]; // dimension of hist
121  Int_t xbin = (Int_t)linData[1]; // n bins x
122  Int_t ybin = (Int_t)linData[2]; // n bins y
123  Int_t zbin = (Int_t)linData[3]; // n bins z
124 
125  Int_t offsetData = 0;
126  Int_t offsetx = 4;
127  Int_t offsety = offsetx + (xbin + 1);
128  Int_t offsetz = offsety + (ybin + 1);
129 
130  Int_t maxbin = 0;
131  if(dim == 1) { maxbin = xbin; offsetData = offsetx + (xbin + 1);}
132  if(dim == 2) { maxbin = xbin * ybin; offsetData = offsety + (ybin + 1);}
133  if(dim == 3) { maxbin = xbin * ybin * zbin; offsetData = offsetz + (zbin + 1);}
134 
135  cout<<"---------------------------------------------------------------"<<endl;
136  if(dim == 1) cout<<"info : "<<name.Data()<<", nBin = "<<xbin <<endl;
137  if(dim == 2) cout<<"info : "<<name.Data()
138  <<", nBin = "<<maxbin
139  <<", nx = " <<xbin
140  <<", ny = " <<ybin<<", stored linear : ind = x * ybin + y"<<endl;
141  if(dim == 3) cout<<"info : "<<name.Data()
142  <<", nBin = "<<maxbin
143  <<", nx = " <<xbin
144  <<", ny = " <<ybin
145  <<", nz = " <<zbin<<", stored linear : ind = x * (ybin*zbin) + y*zbin + z"<<endl;
146 
147  if(dim > 0){
148  cout<<"x axis : "<<endl;
149  printArray(linData,nvals,width,offsetx,offsetx + xbin + 1);
150  }
151  if(dim > 1) {
152  cout<<"y axis : "<<endl;
153  printArray(linData,nvals,width,offsety,offsety + ybin + 1);
154  }
155  if(dim == 3) {
156  cout<<"z axis : "<<endl;
157  printArray(linData,nvals,width,offsetz,offsetz + zbin + 1);
158  }
159  cout<<"data : "<<endl;
160  printArray(linData,nvals,width,offsetData,offsetData + maxbin);
161 }
162 
163 
164 TH1* HHistConverter::createHist(TString name,const TArrayD& linData)
165 {
166  // creates a histogram withname name from an TArrayD.
167  // supports 1,2 and 3 dim Histograms of type Double_t.
168 
169  TH1* h = 0;
170  TArrayD& linDat = (TArrayD&)linData;
171  Int_t dim = (Int_t)linData[0]; // dimension of hist
172  Int_t xbin = (Int_t)linData[1]; // n bins x
173  Int_t ybin = (Int_t)linData[2]; // n bins y
174  Int_t zbin = (Int_t)linData[3]; // n bins z
175 
176  Int_t offsetData = 0;
177  Int_t offsetx = 4;
178  Int_t offsety = offsetx + (xbin + 1);
179  Int_t offsetz = offsety + (ybin + 1);
180 
181  if(dim == 1) { offsetData = offsetx + (xbin + 1);}
182  if(dim == 2) { offsetData = offsety + (ybin + 1);}
183  if(dim == 3) { offsetData = offsetz + (zbin + 1);}
184 
185  if (dim == 1){
186 
187  h = (TH1*) new TH1D(name.Data(),name.Data(),xbin,&linDat[offsetx]);
188 
189  for(Int_t i = 0; i < xbin; i ++){
190  h->SetBinContent(i+1,linData.At(offsetData + i));
191  }
192  } else if(dim == 2) {
193  h = (TH1*) new TH2D(name.Data(),name.Data()
194  ,xbin,&linDat[offsetx]
195  ,ybin,&linDat[offsety]
196  );
197  Int_t ind;
198  for(Int_t i = 0; i < xbin; i ++){
199  for(Int_t j = 0; j < ybin; j ++){
200  ind = i * ybin + j;
201  h->SetBinContent(i+1,j+1,linData.At(offsetData + ind) );
202  }
203  }
204  } else if(dim == 3) {
205  Int_t ind;
206  h = (TH1*) new TH3D(name.Data(),name.Data()
207  ,xbin,&linDat[offsetx]
208  ,ybin,&linDat[offsety]
209  ,zbin,&linDat[offsetz]
210  );
211 
212  for(Int_t i = 0; i < xbin; i ++){
213  for(Int_t j = 0; j < ybin; j ++){
214  for(Int_t k = 0; k < zbin; k ++){
215  ind = i * ybin*zbin + j*ybin + j;
216  h->SetBinContent(i+1,j+1,k+1,linData.At(offsetData + ind) );
217  }
218  }
219  }
220  }
221  h->SetDirectory(0);
222 
223  return h;
224 }
225 void HHistConverter::fillArray(const TH1* h,TArrayD& linData,TString name,Int_t nvals,Int_t width, Bool_t print)
226 {
227  // To fill the Array linData from histogram h
228  // where name (lable), nvals (number of displayed values /row),
229  // width (formating digits for the fields in row and print
230  // are options to display the result of the convertion.
231 
232  Int_t dim = h->GetDimension();
233  Int_t xbin = h->GetNbinsX();
234  Int_t ybin = h->GetNbinsY();
235  Int_t zbin = h->GetNbinsZ();
236 
237  Int_t offsetData = 0;
238  Int_t offsetx = 4;
239  Int_t offsety = offsetx + (xbin + 1);
240  Int_t offsetz = offsety + (ybin + 1);
241 
242  Int_t maxbin = 0;
243  if(dim == 1) { maxbin = xbin; offsetData = offsetx + (xbin + 1); ybin = zbin = 0;}
244  if(dim == 2) { maxbin = xbin * ybin; offsetData = offsety + (ybin + 1); zbin = 0;}
245  if(dim == 3) { maxbin = xbin * ybin * zbin; offsetData = offsetz + (zbin + 1);}
246 
247  linData.Set(offsetData + maxbin);
248 
249  linData[0] = dim;
250  linData[1] = xbin;
251  linData[2] = ybin;
252  linData[3] = zbin;
253 
254  //-----------------------------------------------------------------------------
255  // Filling axis
256  for(Int_t i = 0; i < xbin; i ++){
257  linData.SetAt(h->GetXaxis()->GetBinLowEdge(i + 1),offsetx + i);
258  }
259  linData.SetAt(h->GetXaxis()->GetBinUpEdge(xbin),offsetx + xbin);
260 
261 
262  if(dim >= 2){
263  for(Int_t i = 0; i < ybin; i ++){
264  linData.SetAt(h->GetYaxis()->GetBinLowEdge(i + 1),offsety + i);
265  }
266  linData.SetAt(h->GetYaxis()->GetBinUpEdge(ybin),offsety + ybin);
267  }
268  if(dim == 3){
269  for(Int_t i = 0; i < zbin; i ++){
270  linData.SetAt(h->GetZaxis()->GetBinLowEdge(i + 1),offsetz + i);
271  }
272  linData.SetAt(h->GetZaxis()->GetBinUpEdge(zbin),offsetz + zbin);
273  }
274 
275  //-----------------------------------------------------------------------------
276  // Filling data
277  if(dim == 1){
278  for(Int_t i = 0; i < xbin; i ++){
279  linData.SetAt(h->GetBinContent(i + 1),offsetData + i);
280  }
281  } else if (dim == 2){
282  Int_t ind;
283 
284  for(Int_t i = 0; i < xbin; i ++){
285  for(Int_t j = 0; j < ybin; j ++){
286  ind = i * ybin + j;
287  linData.SetAt(h->GetBinContent(i + 1,j + 1),offsetData + ind);
288  }
289  }
290  } else if (dim == 3){
291  Int_t ind;
292 
293  for(Int_t i = 0; i < xbin; i ++){
294  for(Int_t j = 0; j < ybin; j ++){
295  for(Int_t k = 0; k < zbin; k ++){
296  ind = i * ybin * zbin + j * ybin + k;
297  linData.SetAt(h->GetBinContent(i + 1,j + 1,k + 1),offsetData + ind);
298  }
299  }
300  }
301  }
302  if(print){
303  printArrayInfo(linData,name,nvals,width);
304  }
305 
306 }
static void printArrayInfo(const TArrayD &linData, TString name, Int_t nvals, Int_t width)
static void printArray(const TArray &dat, Int_t nvals=10, Int_t width=0, Int_t start=-1, Int_t end=-1)
Int_t n
static void writeArray(std::ostream &out, TString name, const TArray &dat, Int_t nvals=10)
static TH1 * createHist(TString name, const TArrayD &linData)
ClassImp(HHistConverter) HHistConverter
static void fillArray(const TH1 *h, TArrayD &linData, TString name="", Int_t nvals=10, Int_t width=0, Bool_t print=kTRUE)