00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <assert.h>
00015
00016 #include "THack.h"
00017 #include "TClonesArray.h"
00018 #include "TDirectory.h"
00019 #include "TPad.h"
00020 #include "TList.h"
00021 #include "TSystem.h"
00022 #include "TH1.h"
00023 #include "TTree.h"
00024 #include "TError.h"
00025
00026 class myClonesArray :public TClonesArray
00027 {
00028 public:
00029 myClonesArray(){;}
00030 TObject **GetCont(TClonesArray *klon);
00031 TObject **GetKeep(TClonesArray *klon);
00032 };
00033
00034 TObject **myClonesArray::GetCont(TClonesArray *klon)
00035 {
00036 int offsetC = (char*)&fCont - (char*)this;
00037 return *((TObject***)((char*)klon+offsetC));
00038 }
00039
00040
00041 TObject **myClonesArray::GetKeep(TClonesArray *klon)
00042 {
00043 int offsetK = (char*)&fKeep - (char*)this;
00044 TObjArray *k = *((TObjArray**)((char*)klon+offsetK));
00045 int offsetC = (char*)&fCont - (char*)this;
00046 return *(TObject***)((char*)k+offsetC);
00047 }
00048
00049
00050
00051 void THack::DeleteClonesArray(TClonesArray *clone)
00052 {
00053 myClonesArray mycl;
00054
00055 TObject **keep = mycl.GetKeep(clone);
00056 TObject **cont = mycl.GetCont(clone);
00057 int sz = clone->Capacity();
00058 int i=0;
00059 if (keep) {
00060 for (i=0;i<sz;i++) {
00061 if(!keep[i]) break;
00062 cont[i]=keep[i];
00063 }
00064 }
00065 delete clone;
00066 }
00067
00068
00069 void THack::ClearClonesArray(TClonesArray *clone)
00070 {
00071 myClonesArray mycl;
00072
00073 TObject **keep = mycl.GetKeep(clone);
00074
00075 int sz = clone->Capacity();
00076 int i=0;
00077 clone->Delete();
00078 if (keep) {
00079 TObject *to;
00080 for (i=0;i<sz;i++) {
00081 to = keep[i];
00082 if(!to) break;
00083 if (to->TestBit(TObject::kNotDeleted)) continue;
00084 clone->New(i);
00085 }
00086 }
00087 clone->Clear();
00088 }
00089
00090
00091 void THack::PadRefresh(TPad *pad,int flag)
00092 {
00093
00094
00095 if (!pad) return;
00096 pad->Modified();
00097 pad->Update();
00098 TList *tl = pad->GetListOfPrimitives();
00099 if (!tl) return;
00100 TListIter next(tl);
00101 TObject *to;
00102 while ((to=next())) {
00103 if (to->InheritsFrom(TPad::Class())) PadRefresh((TPad*)to,1);}
00104 if (flag) return;
00105 gSystem->ProcessEvents();
00106 }
00107
00108
00109 void THack::HistRelease(TDirectory *dir)
00110 {
00111
00112 if(!dir) return;
00113 TListIter nextHist(dir->GetList());
00114
00115 TH1 *h1;
00116 while ((h1=(TH1*)nextHist())) {
00117 if (!h1->InheritsFrom(TH1::Class())) continue;
00118 h1->SetDirectory(0);
00119 }
00120 }
00121
00122
00123 int THack::LineToD(const char *line, const char **lend,
00124 int nItems, double *Items,TString *Names)
00125 {
00126 int nIt=0;
00127 const char *l=line; char *ll,*le=0;
00128 while ((l = strstr(l,"="))) {
00129 l++;
00130 if(nIt>nItems) break;
00131 if(Names)Names[nIt]="";
00132 double d = strtod(l,&ll);
00133 if (l==ll) continue;
00134 le=ll;
00135 Items[nIt]=d;
00136 nIt++;
00137 if (!Names) continue;
00138 int n=0;
00139 for (int jj=-2;ll+jj>=line;jj--) {
00140 int space = isspace(l[jj]);
00141 if (space && !n) continue;
00142 if (!strchr("_()[]",l[jj]) && !isalnum(l[jj])) break;
00143 Names[nIt-1].Insert(0,l+jj,1);n++;
00144 }
00145 }
00146 if (lend) *lend=le;
00147 return nIt;
00148 }
00149
00150 bool THack::IsTreeWritable(const TTree *tree, bool fatal)
00151 {
00152
00153 bool out = false;
00154 TDirectory *d = 0;
00155 if (tree && (d = tree->GetDirectory()) && d->IsWritable() ) {
00156 out = true;
00157 } else if (tree && fatal) {
00158 Fatal("IsTreeWritable", "TTree %p %s can not be written", tree,tree->GetName());
00159 }
00160 return out;
00161 }
00162
00163