StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
THack.cxx
1 /***************************************************************************
2  *
3  * $Id: THack.cxx,v 1.6 2009/05/22 23:49:16 fine Exp $
4  *
5  ***************************************************************************
6  *
7  * Description:
8  *
9  ***************************************************************************
10  **************************************************************************/
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <assert.h>
15 
16 #include "THack.h"
17 #include "TClonesArray.h"
18 #include "TDirectory.h"
19 #include "TPad.h"
20 #include "TList.h"
21 #include "TSystem.h"
22 #include "TH1.h"
23 #include "TTree.h"
24 #include "TError.h"
25 
26 class myClonesArray :public TClonesArray
27 {
28 public:
29  myClonesArray(){;}
30 TObject **GetCont(TClonesArray *klon);
31 TObject **GetKeep(TClonesArray *klon);
32 };
33 //______________________________________________________________________________
34 TObject **myClonesArray::GetCont(TClonesArray *klon)
35 {
36  int offsetC = (char*)&fCont - (char*)this;
37 return *((TObject***)((char*)klon+offsetC));
38 }
39 
40 //______________________________________________________________________________
41 TObject **myClonesArray::GetKeep(TClonesArray *klon)
42 {
43  int offsetK = (char*)&fKeep - (char*)this;
44  TObjArray *k = *((TObjArray**)((char*)klon+offsetK));
45  int offsetC = (char*)&fCont - (char*)this;
46  return *(TObject***)((char*)k+offsetC);
47 }
48 
49 //______________________________________________________________________________
50 //______________________________________________________________________________
51 void THack::DeleteClonesArray(TClonesArray *clone)
52 {
53  myClonesArray mycl;
54 
55  TObject **keep = mycl.GetKeep(clone);
56  TObject **cont = mycl.GetCont(clone);
57  int sz = clone->Capacity();
58  int i=0;
59  if (keep) {
60  for (i=0;i<sz;i++) {
61  if(!keep[i]) break;
62  cont[i]=keep[i];
63  }
64  }
65  delete clone;
66 }
67 
68 //______________________________________________________________________________
69 void THack::ClearClonesArray(TClonesArray *clone)
70 {
71  myClonesArray mycl;
72 
73  TObject **keep = mycl.GetKeep(clone);
74 // TObject **cont = mycl.GetCont(clone);
75  int sz = clone->Capacity();
76  int i=0;
77  clone->Delete();
78  if (keep) {
79  TObject *to;
80  for (i=0;i<sz;i++) {
81  to = keep[i];
82  if(!to) break;
83  if (to->TestBit(TObject::kNotDeleted)) continue;//alive
84  clone->New(i);
85  }
86  }
87  clone->Clear();
88 }
89 
90 //______________________________________________________________________________
91 void THack::PadRefresh(TPad *pad,int flag)
92 {
93 // Refresh all TPads in TCanvas recursively
94 
95  if (!pad) return;
96  pad->Modified();
97  pad->Update();
98  TList *tl = pad->GetListOfPrimitives();
99  if (!tl) return;
100  TListIter next(tl);
101  TObject *to;
102  while ((to=next())) {
103  if (to->InheritsFrom(TPad::Class())) PadRefresh((TPad*)to,1);}
104  if (flag) return;
105  gSystem->ProcessEvents();
106 }
107 
108 //______________________________________________________________________________
109 void THack::HistRelease(TDirectory *dir)
110 {
111 // Release all histograms from TFile(TDirectory)
112  if(!dir) return;
113  TListIter nextHist(dir->GetList());
114 // release histograms from file
115  TH1 *h1;
116  while ((h1=(TH1*)nextHist())) {
117  if (!h1->InheritsFrom(TH1::Class())) continue;
118  h1->SetDirectory(0);
119  }
120 }
121 
122 //______________________________________________________________________________
123 int THack::LineToD(const char *line, const char **lend,
124  int nItems, double *Items,TString *Names)
125 {
126  int nIt=0;
127  const char *l=line; char *ll,*le=0;
128  while ((l = strstr(l,"="))) {
129  l++;
130  if(nIt>nItems) break;
131  if(Names)Names[nIt]="";
132  double d = strtod(l,&ll);
133  if (l==ll) continue;
134  le=ll;
135  Items[nIt]=d;
136  nIt++;
137  if (!Names) continue;
138  int n=0;
139  for (int jj=-2;ll+jj>=line;jj--) {
140  int space = isspace(l[jj]);
141  if (space && !n) continue;
142  if (!strchr("_()[]",l[jj]) && !isalnum(l[jj])) break;
143  Names[nIt-1].Insert(0,l+jj,1);n++;
144  }
145  }
146  if (lend) *lend=le;
147  return nIt;
148 }
149 
150 bool THack::IsTreeWritable(const TTree *tree, bool fatal)
151 {
152  // Test whether the TFile assocoated wit TTree is writable
153  bool out = false;
154  TDirectory *d = 0;
155  if (tree && (d = tree->GetDirectory()) && d->IsWritable() ) {
156  out = true;
157  } else if (tree && fatal) {
158  Fatal("IsTreeWritable", "TTree %p %s can not be written", tree,tree->GetName());
159  }
160  return out;
161 }
162 
163