StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LeakFinder.C
1 enum {kCtr =1,kClear = 2,kMake = 3,
2  startClear=2,contClear=12,endClear=22,
3  startMake =3,contMake =13,endMake =23,
4  kMaxMake=100,kMaxMakeSQ = kMaxMake*kMaxMake,
5  kMaxData =kMaxMake+3
6  };
7 
8 class ReadLog {
9 public:
10 int fNMakers;
11 FILE *inp;
12 THashList hash;
13 TObjArray mArray;
14 char line[500];
15 // methods
16 ReadLog(const char *file) {inp = fopen(file,"r");fNMakers=0;}
17 const char *Name(int idx) {
18  if (!idx) return "__ALL__";
19  TObject *tn = mArray.At(idx); return (tn)? tn->GetName():0;}
20 
21 const char *Next(int &kind,int &idx,double &exe,double &heap,double &free,double &inc) {
22  if(!inp) return 0;
23  kind=0;idx=0;exe=0;heap=0;free=0,inc=0;
24  TString ts;
25  char *endMaker,*clear,*ctr,*doPs,*fr,*to,*and;
26  while(fgets(line,500,inp)) {
27  doPs = strstr(line,"doPs for");
28  if (!doPs) continue;
29  endMaker = strstr(line,"EndMaker");
30  clear = strstr(line,"Clear");
31  ctr = strstr(line,"constructor");
32  kind = 0;
33  if (ctr ) kind = 1;
34  if (clear ) kind = 2;
35  if (endMaker) kind = 3;
36  if (!kind) continue;
37  fr = doPs+8 +strspn(doPs+8," \t");
38  to = strstr(fr,":");
39  ts = ""; ts.Append(fr,to-fr);
40  if (!ts.Length()) continue;
41  fr = strstr(fr,"total"); if (!fr) continue;
42  fr = strstr(fr,"=" ); if (!fr) continue;
43  exe = atof(fr+1);
44  fr = strstr(fr,"heap" ); if (!fr) continue;
45  fr = strstr(fr,"=" ); if (!fr) continue;
46  heap = atof(fr+1);
47  and = strstr(fr,"and");
48  if (and) free = atof(and+3);
49  fr = strstr(fr,"(" ); if (!fr) continue;
50  inc = atof(fr+1);
51  if (kind==1) continue;
52  TNamed *tn = (TNamed*)hash.FindObject(ts.Data());
53  if (!tn) {
54  tn = new TNamed(ts.Data(),"");
55  hash.Add(tn);
56  fNMakers++;
57  tn->SetUniqueID((UInt_t)fNMakers);
58  mArray.AddAtAndExpand(tn,fNMakers);
59  }
60  idx = tn->GetUniqueID();
61  return tn->GetName();
62  }
63  fclose(inp); inp = 0;
64  return 0;
65 }};
66 //______________________________________________________________________________
67 class Data4Fit
68 {
69 public:
70 
71 Data4Fit(ReadLog &rr);
72 int fNEvts;
73 int fNMk;
74 TArrayD fDat;
75 
76 double *GetDat(int evt) {return fDat.GetArray()+evt*(kMaxData);}
77 double *GetEvt(int evt) {return GetDat(evt)+2;}
78 double &GetEvtRes(int evt) {return GetEvt(evt)[-1];}
79 double &GetEvtSiz(int evt) {return GetEvt(evt)[-2];}
80 };
81 //______________________________________________________________________________
82 Data4Fit::Data4Fit(ReadLog &rr)
83 {
84  double QQ[kMaxData];
85  memset(QQ,0,sizeof(double)*kMaxData);
86  double *Q = QQ+2;
87  const char *name;
88  int kind,idx; double exe,heap,free,inc;
89  fNEvts=0,fNMk=0;
90  int nkind[4] = {0,0,0,0};
91  int kase = 0,oldKind=0,num = 0,nmk=0,narr;
92 
93  while ((name = rr.Next(kind,idx,exe,heap,free,inc))) {
94  nkind[kind]++;
95  if (kind<kClear) continue;
96  num++;
97 // printf("%4d - %20s \t%d %d \t %g %g %+g\n",num,name,kind,idx,exe,heap,inc);
98 
99  if (!oldKind) { kase = kind; }
100  else if (kind==oldKind) { kase = kind +10;}
101  else { kase = oldKind+20;}
102  fNMk = rr.fNMakers;
103 SWIT: switch (kase) {
104 
105  case startClear:
106  kase = contClear;
107 
108  case contClear:
109  if (!nmk) break;
110  Q[-1] +=inc;
111  break;
112 
113  case endClear:
114  kase = startMake;
115  if(!nmk) goto SWIT;
116  narr = fDat.GetSize();
117  if (narr< (fNEvts+1)*kMaxData) fDat.Set((fNEvts+10)*kMaxData*2);
118 // printf("Q="); for (int j=0;j<=fNMk;j++) {printf("%12.6g ",Q[j]);}; printf("\n");
119 
120  memcpy(GetDat(fNEvts),QQ,kMaxData*sizeof(double));
121 // printf("Q="); for (int j=0;j<=fNMk;j++) {printf("%12.6g ",GetEvt(fNEvts)[j]);}; printf("\n");
122  fNEvts++;
123 
124  case startMake:
125  memset(QQ,0,sizeof(double)*kMaxData);
126  Q[0]=1.;
127  kase = contMake;
128 
129  case contMake:
130  nmk++;
131  Q[idx] = inc;
132  Q[-1 ] += inc;
133  Q[-2 ] += inc;
134  break;
135 
136  case endMake:
137  kase = startClear;
138  goto SWIT;
139 
140  }//EndSwitch
141  oldKind = kind;
142  }//end LOOP
143  fNMk = rr.fNMakers;
144 }
145 //______________________________________________________________________________
146 void LeakGraph(char *file);
147 void LeakFit (char *file);
148 //______________________________________________________________________________
149 void LeakFinder(char *file,int kase=1,const char *par1=0)
150 {
151  switch (kase) {
152  case 0: LeakGraph(file); break;
153  case 1: if (!TClassTable::GetDict("TCL")) {gSystem->Load("libTable");}
154  LeakFit (file); break;
155  case 2: LeakCount(file,par1); break;
156  }
157 }
158 
159 //______________________________________________________________________________
160 void LeakGraph(char *file)
161 {
162  TArrayF X[4],XX[2];
163 
164  ReadLog rr(file);
165  const char *name;
166  int kind,idx; double exe,heap,free,inc;
167  int num = 0,nXX=0;
168  float yMax =0, yMin = 1000000.,xMax;
169  while ((name = rr.Next(kind,idx,exe,heap,free,inc))) {
170 // if (kind!=3) continue;
171  if (kind==kCtr) continue;
172 
173  int n = X[0].GetSize();
174  if (num>=n) { for (int i=0;i<4;i++){X[i].Set(n*2+10);}}
175  X[0][num] = num;
176  if (yMax < exe ) yMax = exe;
177  if (yMax < heap) yMax = heap;
178  if (yMax < free) yMax = free;
179  if (yMin > exe ) yMin = exe;
180  if (yMin > heap) yMin = heap;
181  if (yMin > free) yMin = free;
182  X[1][num] = exe;
183  X[2][num] = heap;
184  X[3][num] = free;
185 // printf("YYYYYYYYYYYYYY %g %g %g\n",X[num],Y[0][num],Y[1][num]);
186  num++;
187 // printf("%4d - %20s \t%d %d \t %g %g %g %+g\n",num,name,kind,idx,exe,heap,free,inc);
188  if (kind!=kClear) continue;
189  if (strcmp("bfc",name)) continue;
190  n = XX[0].GetSize();
191  if (nXX>=n) { for (int i=0;i<2;i++){XX[i].Set(n*2+10);}}
192  XX[0][nXX]=num;
193  XX[1][nXX]=heap;
194  nXX++;
195  }
196  int nPoints = num;
197  yMax *=1.1;
198  yMin -=0.1*yMax;
199  xMax = nPoints;
200  float fact = float(nXX)/nPoints;
201  xMax = nPoints*fact;
202  for (int i=0;i<num;i++) {X [0][i] = X [0][i]*fact;}
203  for (int i=0;i<nXX;i++) {XX[0][i] = XX[0][i]*fact;}
204 
205  TCanvas *C1 = new TCanvas("LEAK","Memory grow",1);
206  TGraph *pl[4];
207  for (int i=0;i<3;i++){pl[i] = new TGraph(nPoints, X[0].GetArray(), X[i+1].GetArray());}
208  pl[3] = new TGraph(nXX ,XX[0].GetArray(),XX[1 ].GetArray());
209  pl[0]->SetLineColor(kBlack);
210  pl[1]->SetLineColor(kBlue );
211  pl[2]->SetLineColor(kGreen);
212  pl[3]->SetLineColor(kRed );
213 
214  pl[0]->SetName("Exe");
215  pl[1]->SetName("Heap");
216  pl[2]->SetName("Free");
217  pl[3]->SetName("Clear");
218 
219  C1->DrawFrame(0.,yMin,xMax,yMax);
220  pl[3]->Draw("LP");
221  //pl[1]=0;
222  for (int i=0;i<3;i++) {if (pl[i]) pl[i]->Draw("LP");}
223  C1->Modified();
224  C1->Update();
225 
226 }
227 
228 //______________________________________________________________________________
229 void LeakFit(char *file)
230 {
231 
232  int ignore[kMaxMake];
233  memset(ignore,0,kMaxMake*sizeof(int));
234  double A[kMaxMake][kMaxMake],B[kMaxMake],Q[kMaxMake],V[kMaxMake];
235  TCL::vzero(A[0],(kMaxMake)*(kMaxMake));
236  TCL::vzero(B ,kMaxMake);
237  TCL::vzero(V ,kMaxMake);
238 
239  double Corr[5][kMaxMake];
240  TCL::vzero(Corr[0],5*(kMaxMake));
241  int nCorr=0;
242 
243  TCanvas *C1 = new TCanvas("LeakPlot","Leak vs Size",1);
244  C1->Divide(1,2);
245 
246  TH2F *H1 = new TH2F("LeakPlot", "Leak vs Size", 100, 0, 50., 100, -0.1, 3.);
247  TH2F *H2 = new TH2F("LeagPlot", "Leag vs Size", 100, 0, 50., 100, -0.1, 3.);
248 // H->SetBit(TH1::kCanRebin);
249  C1->cd(1); H1->Draw();
250  C1->cd(2); H2->Draw();
251 
252  ReadLog RR(file);
253  Data4Fit FF(RR);
254  const char *name;
255  int num = 0,nEvents=0,nMk=0,nFill=0,iWeight=0;
256 
257  double emptySize = 10;
258  int skipEvents = 10;
259  iWeight=0;
260  nMk = FF.fNMk;
261  nEvents = FF.fNEvts;
262  for (int ievt=0;ievt<nEvents;ievt++) {
263  double diff = FF.GetEvtRes(ievt);
264  double eSize = FF.GetEvtSiz(ievt);
265  if (ievt<=skipEvents) continue;
266  H1->Fill(eSize,diff);
267  if (eSize < emptySize+0.1) continue;
268  nFill++;
269  TCL::ucopy(FF.GetEvt(ievt),Q,nMk+1);
270 //printf("Q="); for (int j=0;j<=nMk;j++) {printf("%d=%12.6g ",j,Q[j]);}; printf("\n");
271  TCL::vadd(V,Q,V,nMk+1);
272  double Qmax = 0;
273  for (int i=1;i<=nMk;i++){
274  if (fabs(Q[i])>Qmax) Qmax=fabs(Q[i]);
275  if (Q[i]< 0.1*diff) Q[i]=0.;
276  };
277 
278  double wt = 1;
279  if (iWeight==1) wt =1./pow(diff,2);
280  if (iWeight==2) wt =1./pow(eSize-emptySize,2);
281  if (iWeight==3) wt =1./pow(Qmax,2);
282 
283  for (int i1=0;i1<=nMk;i1++){
284  B[i1]+= diff*Q[i1]*wt;
285 // printf("B[%d] = %12.6g*%12.6g*%12.6g =%12.6g \n",i1,diff,Q[i1],wt,B[i1]);
286  for (int i2=0;i2<=nMk;i2++){ A[i1][i2] += Q[i1]*Q[i2]*wt;}
287  }
288 // Second Method
289  nCorr++;
290  for (int i=1;i<=nMk;i++) {
291  Corr[0][i] += Q[i];
292  Corr[1][i] += diff;
293  Corr[2][i] += Q[i]*Q[i];
294  Corr[3][i] += diff*diff;
295  Corr[4][i] += diff*Q[i];
296  }
297 #
298 
299  }//end LOOP
300  int nX = nMk+1;
301  double scale = 1./nFill;
302  TCL::vscale(B,scale,B,nX);
303  TCL::vscale(A[0],scale,A[0],(kMaxMake)*(kMaxMake));
304 
305 // Regularisation
306  int nneg=1;
307 
308  while (nneg) {
309  printf("%d makers %d events filled %d times\n\n",nMk,nEvents,nFill);
310 
311  for (int ign=0;ign<nX;ign++) {
312  if(!ignore[ign]) continue;
313  if(ignore[ign]>0) {//Too big
314  for (int i=0;i<nX;i++) B[i]-=A[i][ign];
315  for (int i=0;i<nX;i++) {A[i][ign]=0;A[ign][i]=0;}
316  A[ign][ign]=1; B[ign]=1;
317  } else {
318  for (int i=0;i<nX;i++) {A[i][ign]=0;A[ign][i]=0;}
319  A[ign][ign]=1; B[ign]=0;
320  }
321  }
322  double reg = 0;
323  for (int i=0;i<nX;i++) {
324  if (ignore[i]) continue;
325  if (A[i][i]>reg) reg = A[i][i];
326  }
327  reg /=10000.;
328 
329  TArrayD AA(nX*nX),BB(nX);
330  for (int i1=0;i1<nX;i1++) {
331  // printf("B[%d]== %g\n",i1,B[i1]);
332  BB[i1] = B[i1];
333  for (int i2=0;i2<nX;i2++) {
334  // printf("A[%d][%d]== %g\n",i1,i2,A[i1][i2]);
335  AA[i1+nX*i2] = A[i1][i2];
336  if(i1==i2) AA[i1+nX*i2]+=reg;
337  // printf("AA[%d][%d] = %12.6g\n",i1,i2,AA[i1+nX*i2]);
338  }}
339  TCL::trsequ(AA.GetArray(),nX,BB.GetArray(), 1);
340  nneg=0;
341  for (int i=1;i<nX;i++) {
342  if (ignore[i]) continue;
343  double bj = BB[i];
344  if (bj < 0.01) {nneg++; ignore[i]=-1;}
345  if (bj > 1.00) {nneg++; ignore[i]= 1;}
346  }
347  }//End while
348 
349  for (int i=1;i<nX;i++) {//print results
350  if (ignore[i]<0) continue;
351  double bj = BB[i];
352  double est = bj*V[i];
353  if (fabs(bj ) <= 0.0) continue;
354  if (fabs(est) <= 0.0) continue;
355 // if ((bj ) < 0.001) continue;
356 // if ((est) < 0.010) continue;
357  printf ("Maker(%d) %s \tCorr =%g \tLeak =%g\n",i,RR.Name(i),bj,est);
358  }//end for print results
359 
360 // Test result
361 
362  for (int ievt=0;ievt<nEvents;ievt++) {
363  double diff = FF.GetEvtRes(ievt);
364  double eSize = FF.GetEvtSiz(ievt);
365  if (ievt<=skipEvents) continue;
366  double dot = TCL::vdot(FF.GetEvt(ievt),BB.GetArray(),nX);
367  H2->Fill(eSize,diff-dot);
368 
369  }//end LOOP
370 // for (int i=1;i<nX;i++) {printf ("Maker(%d) %s \tV=%g\n",i,RR.Name(i),V[i]);}
371 
372 // Second Method
373 
374  for (int i=1;i<=nMk;i++) {
375  for (int j=0;j<5;j++) {Corr[j][i]/=nCorr;}
376  double x = Corr[0][i];
377  double r = Corr[1][i];
378  double xx = Corr[2][i];
379  double rr = Corr[3][i];
380  double xr = Corr[4][i];
381  Corr[2][i]=1.e+10;
382 //printf("%4d - x,r,xx,rr,xr = %g %g %g %g %g \n",i,x,r,xx,rr,xr);
383  double Dxx = xx - x*x;
384  if (Dxx< 1.e-10) continue;
385  double Drr = rr - r*r;
386  double Dxr = xr - x*r;
387  double chisq = 1.e+10;
388  double b = Dxr/Dxx;
389  double a = r-b*x;
390  double chisq = a*a+b*b*xx+rr+2.*a*b*x-2*a*r-2*b*xr;
391  Corr[0][i]=a;
392  Corr[1][i]=b;
393  if (b<0. || b> 1.2) chisq+=1.e+6;
394  Corr[2][i]=chisq;
395  }
396  int *Idx= (int*)Corr[5];
397  double *bb = Corr[1];
398  double *sq = Corr[2];
399  TMath::Sort(nMk,sq+1,Idx+1,0);
400  int j;double leak;
401  for (int i=1;i<=nMk;i++) {
402  j = Idx[i]+1;
403  assert(j>0 && j<=nMk);
404  if (sq[j]>=1.e+10) break;
405  leak = V[j]*bb[j]/nFill;
406  printf("%4d - %12s(%d) Chisq =%g Corr = %g Leak=%g\n",i,RR.Name(j),j,sq[j],bb[j],leak);
407  }
408  printf("===================End Of LeakFit========================");
409 
410 }//end LeakFit
411 //______________________________________________________________________________
412 void LeakCount(char *file, const char *sele)
413 {
414 
415  ReadLog rr(file);
416  const char *name;
417  int kind,idx; double exe,heap,free,inc;
418  int num = 0;
419  double summ = 0;
420 
421 
422  while ((name = rr.Next(kind,idx,exe,heap,free,inc))) {
423  if (kind<2) continue;
424  if (sele && *sele && strcmp(name,sele)) continue;
425  num++;
426  summ +=inc;
427  printf("%4d - %20s \t%d %d \t %12.6f %12.6f %+12.6f\t%12.6f\n",num,name,kind,idx,exe,heap,inc,summ);
428  }
429 
430  printf("%s = %d %g\n",sele,num,summ);
431 }
static float * trsequ(float *smx, int m=3, float *b=0, int n=1)
Definition: TCernLib.cxx:2111