GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4MbsHist.cxx
Go to the documentation of this file.
1 // $Id: TGo4MbsHist.cxx 999 2013-07-25 11:58:59Z linev $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4MbsHist.h"
15 
16 #include <string.h>
17 
18 #include "TGraph.h"
19 #include "TH1.h"
20 #include "TH2.h"
21 #include "TList.h"
22 #include "TFolder.h"
23 #include "TTimeStamp.h"
24 #include "TRegexp.h"
25 #include "snprintf.h"
26 
27 #include "TGo4ThreadManager.h"
29 
30 const Int_t TGo4MbsHist::fgiLISTLEN=512;
31 
33  TObject(),
34  fiBufLen(128),
35  fiBuffer(0),
36  fxCursor(0),
37  fiHisNum(0)
38 {
39  fiBuffer=new Int_t[fiBufLen];
40 }
41 
43  TObject(),
44  fiBufLen(1),
45  fiBuffer(0),
46  fxCursor(0),
47  fiHisNum(1)
48 {
49 if(histo)
50  {
51  PrepareHeader(histo,0,&fxHistoHead);
52  if(histo->GetDimension()==2)
53  {
54  // two dim histo
55  Int_t i=0;
56  Stat_t value=0;
57 
58  Int_t maxu=histo->GetNbinsX();
59  Int_t maxv=histo->GetNbinsY();
60  fiBufLen=(maxu)*(maxv); // note: we ignore over + underflow bins
61  fiBuffer=new Int_t[fiBufLen];
62  // test: we swap x and y loops
63  for (Int_t v = 0;v<maxv; v++)
64  {
65  for (Int_t u = 0; u<maxu; u++)
66  {
67  value=histo->GetBinContent(u,v);
68  SetValue((char*) &fiBuffer[i++], value);
69  }
70  } // for
71  }
72  else
73  {
74  // one dim histo and all others
75  Int_t maxu=histo->GetNbinsX();
76  fiBufLen=maxu; // note: we ignore over + underflow bins
77  fiBuffer=new Int_t[fiBufLen];
78  Stat_t value=0;
79  for (Int_t u = 0; u<maxu;u++)
80  {
81  value=histo->GetBinContent(u);
82  SetValue((char*) &fiBuffer[u], value);
83  }
84  } // if(histo->GetDimension()))
85  }
86 else
87  {
88  // case of dummy object!
89  fiBuffer=new Int_t[fiBufLen];
90  } // if(histo)
91 
92 }
93 
94 TGo4MbsHist::TGo4MbsHist(TFolder* folder, const char* filter) :
95  TObject(),
96  fiBufLen(fgiLISTLEN),
97  fiBuffer(0),
98  fxCursor(0),
99  fiHisNum(0)
100 {
101  // allocate buffer of total size
102  //std::cout <<"Init buflen with "<< fiBufLen << std::endl;
103  fiBuffer=new Int_t[fiBufLen];
105  ScanGo4Folder(folder,0,filter);
106 }
107 
108 
110 {
111  delete [] fiBuffer;
112 }
113 
114 void TGo4MbsHist::PrepareHeader(TH1* source, const char* path, s_his_head* target)
115 {
116  if(source==0 || target==0) return;
117  //std::cout <<"MMMMMMMM Preparing header for histogram "<< source->GetName() << std::endl;
118  s_his_head* dest=target; // use local pointer to avoid changes by snprintf
119  dest->l_bins_1=source->GetNbinsX();
120  dest->l_bins_2=source->GetNbinsY();
121  //std::cout <<" \tMMMMMMMM Prepare header - bins1="<< dest->l_bins_1 << std::endl;
122  //std::cout <<"\tMMMMMMMM Prepare header - bins2="<< dest->l_bins_2 << std::endl;
123 // display of path enabled again to test Origin problems JA
124  if(path)
125  snprintf(dest->c_name,64,"%s%s",path,source->GetName());
126  else
127  snprintf(dest->c_name,64,"%s",source->GetName());
128  snprintf(dest->c_lettering_1,64,"%s",source->GetXaxis()->GetTitle());
129  snprintf(dest->c_lettering_2,64,"%s",source->GetYaxis()->GetTitle());
130  snprintf(dest->c_lettering_res,64,"%s",source->GetYaxis()->GetTitle());
131  dest->r_limits_low=source->GetXaxis()->GetXmin();
132  dest->r_limits_up=source->GetXaxis()->GetXmax();
133  dest->r_limits_low_2=source->GetYaxis()->GetXmin();
134  dest->r_limits_up_2=source->GetYaxis()->GetXmax();
135  //std::cout <<" \tMMMMMMMM Prepare header - 1low="<< dest->r_limits_low << std::endl;
136  //std::cout <<"\tMMMMMMMM Prepare header - 1up ="<< dest->r_limits_up << std::endl;
137  //std::cout <<" \tMMMMMMMM Prepare header - 2low="<< dest->r_limits_low_2 << std::endl;
138  //std::cout <<"\tMMMMMMMM Prepare header - 2up ="<< dest->r_limits_up_2 << std::endl;
139 
140  if(source->InheritsFrom(TH1D::Class()) ||
141  source->InheritsFrom(TH1F::Class()) ||
142  source->InheritsFrom(TH2D::Class()) ||
143  source->InheritsFrom(TH2F::Class())) {
144  //std::cout <<" \tMMMMMMMM Prepare header has float histo" << std::endl;
145  strcpy(dest->c_dtype,"r");
146  }
147  else
148  {
149  //std::cout <<" \tMMMMMMMM Prepare header has int histo" << std::endl;
150  strcpy(dest->c_dtype,"i");
151  }
152  TTimeStamp now;
153  snprintf(dest->c_data_time_cre,28,"%s",now.AsString("l")); // creation time
154  snprintf(dest->c_clear_date,28,"%s",now.AsString("l")); // clear time, dummy here
155 
156 }
157 
158 
159 void TGo4MbsHist::SetValue(char* address, Stat_t value)
160 {
161 if(!strcmp(fxHistoHead.c_dtype,"r"))
162  {
163  Float_t* faddress=(Float_t*) address;
164  *faddress= (Float_t) value;
165  }
166 else
167  {
168  Int_t* iaddress=(Int_t*) address;
169  *iaddress= (Int_t) value;
170  }
171 }
172 
173 
174 void TGo4MbsHist::ScanGo4Folder(TFolder* folder, const char* superfolders, const char* filter)
175 {
176  // scan folder for histogram status objects
177 // if(filter)
178 // std::cout <<"ScanGo4Folder with filter "<<filter << std::endl;
179 // else
180 // std::cout <<"ScanGo4Folder with no filter "<< std::endl;
181 //
182 
183  if(folder==0) return;
184 
185  TIter iter(folder->GetListOfFolders());
186  TObject* entry = 0;
187  while((entry = iter()) !=0) {
188  char pathbuffer[TGo4ThreadManager::fguTEXTLENGTH];
189  Int_t num=0;
190  if(superfolders)
191  num=snprintf(pathbuffer,TGo4ThreadManager::fguTEXTLENGTH,"%s/",superfolders);
192  else
193 // num=snprintf(pathbuffer,TGo4ThreadManager::fguTEXTLENGTH,"");
194  strcpy(pathbuffer,"");
195  char* cursor=pathbuffer + num;
196  Int_t edge=TGo4ThreadManager::fguTEXTLENGTH-num;
197  if(entry->InheritsFrom(TFolder::Class()))
198  {
199  // found subfolder, process it recursively
200  const char* foldname=entry->GetName();
201  // filter out folders without histograms or graphs:
202  if(!strcmp(foldname,TGo4AnalysisObjectManager::fgcDYNFOLDER)){;}
203  else if(!strcmp(foldname,TGo4AnalysisObjectManager::fgcCONDFOLDER)){;}
204  else if(!strcmp(foldname,TGo4AnalysisObjectManager::fgcPARAFOLDER)){;}
205  else if(!strcmp(foldname,TGo4AnalysisObjectManager::fgcTREEFOLDER)){;}
206  else if(!strcmp(foldname,TGo4AnalysisObjectManager::fgcPICTFOLDER)){;}
207  else if(!strcmp(foldname,TGo4AnalysisObjectManager::fgcCANVFOLDER)){;}
208  else if(!strcmp(foldname,TGo4AnalysisObjectManager::fgcANALYSISFOLDER)){;}
209  else
210  {
211  //std::cout <<"##### parsing folder "<< foldname << std::endl;
212  snprintf(cursor,edge,"%s",foldname);
213  TFolder* subobj= dynamic_cast<TFolder*> (entry);
214  ScanGo4Folder(subobj,pathbuffer, filter);
215  }
216  }
217  else if (entry->InheritsFrom(TH1::Class()) || entry->InheritsFrom(TGraph::Class()))
218  {
219  // first check if filter is ok:
220  Bool_t ismatching=kFALSE;
221  const char* entryname=entry->GetName();
222  TString entrystring=entryname;
223  TRegexp reg(filter,kTRUE);
224  if(filter==0)
225  ismatching=kTRUE; // no filter set, take all
226  else if(!strcmp(filter,"*"))
227  ismatching=kTRUE; // take all in this folder
228 // else if (strstr(filter,entryname))
229 // ismatching=kTRUE; // expression just contained in name
230  else if (entrystring.Index(reg,0)!=kNPOS)
231  ismatching=kTRUE; // root regular expression class
232  else
233  ismatching=kFALSE;
234 
235  if(ismatching)
236  {
237  //std::cout <<"found matching:" << entryname << std::endl;
238  TH1* hist= dynamic_cast<TH1*> (entry);
239  if(hist==0)
240  {
241  TGraph* graf=dynamic_cast<TGraph*> (entry);
242  if(graf && graf->GetN()>0)
243  {
244  hist=graf->GetHistogram();
245  }
246  if(hist==0) continue;
247  } // if(hist==0)
248  PrepareHeader(hist,pathbuffer,fxCursor);
249  fxCursor++;
250  fiHisNum++;
251  //std::cout <<"MMMMMMMM found histogram "<< entry->GetName()<<" cr="<<(Int_t) fxCursor<< std::endl;
252  if( (char*) (fxCursor) > (char*)(fiBuffer)+fiBufLen*sizeof(Int_t)- sizeof(s_his_head))
253  {
254  //std::cout <<"MMMMMMM Realloc buffer cursor "<< (Int_t) fxCursor << std::endl;
255  Int_t oldbuflen=fiBufLen;
257  Int_t* otherbuf= new Int_t[fiBufLen];
258  memcpy(otherbuf, fiBuffer,oldbuflen*sizeof(Int_t) );
259  delete[] fiBuffer;
260  fiBuffer=otherbuf;
262  for(Int_t n=0; n< fiHisNum; ++n) fxCursor++; // set cursor in new buffer
263 // std::cout <<"MMMMMMM Copied buffer, cursor set "<< std::endl;
264 // std::cout <<" hisnum="<< (Int_t) fiHisNum<< std::endl;
265 // std::cout <<" newcr="<< (Int_t) fxCursor<< std::endl;
266 // std::cout <<" buffer="<< (Int_t) fiBuffer<< std::endl;
267  } else {}
268  }//if(ismatching)
269  }
270  } // while
271 }
Int_t fiBufLen
Definition: TGo4MbsHist.h:71
static const Int_t fgiLISTLEN
Definition: TGo4MbsHist.h:55
REAL4 r_limits_up
Definition: s_his_head.h:28
void SetValue(char *address, Stat_t value)
Int_t fiHisNum
Definition: TGo4MbsHist.h:78
CHARS c_clear_date[28]
Definition: s_his_head.h:44
void PrepareHeader(TH1 *source, const char *path, s_his_head *target)
CHARS c_lettering_res[64]
Definition: s_his_head.h:45
INTS4 l_bins_2
Definition: s_his_head.h:23
virtual ~TGo4MbsHist()
REAL4 r_limits_low
Definition: s_his_head.h:27
REAL4 r_limits_up_2
Definition: s_his_head.h:36
s_his_head * fxCursor
Definition: TGo4MbsHist.h:75
CHARS c_data_time_cre[28]
Definition: s_his_head.h:43
CHARS c_name[64]
Definition: s_his_head.h:41
REAL4 r_limits_low_2
Definition: s_his_head.h:35
CHARS c_lettering_1[64]
Definition: s_his_head.h:46
void ScanGo4Folder(TFolder *fold, const char *superfolders=0, const char *filter=0)
INTS4 l_bins_1
Definition: s_his_head.h:22
CHARS c_lettering_2[64]
Definition: s_his_head.h:47
CHARS c_dtype[4]
Definition: s_his_head.h:42
s_his_head fxHistoHead
Definition: TGo4MbsHist.h:70
Int_t * fiBuffer
Definition: TGo4MbsHist.h:72