00001
00002
00003
00005
00006 #include "emcGlobals.h"
00007
00008 #include <TROOT.h>
00009 #include <TSystem.h>
00010 #include <TGClient.h>
00011 #include <Rtypes.h>
00012 #include <TFile.h>
00013 #include <TKey.h>
00014 #include <TH2.h>
00015 #include <TLine.h>
00016
00017
00018 #include <fstream>
00019 #include <iomanip>
00020 #include <algorithm>
00021 #include <numeric>
00022 #include <iostream>
00023 #include <cmath>
00024 #include <ctime>
00025
00026 #include "Event.h"
00027 #include "PHTimeStamp.h"
00028
00029 using namespace std;
00030
00031
00032
00033
00034 Int_t GF::Save(const char * file,
00035 Option_t * option,
00036 const char * ftitle, Int_t compress)
00037 {
00038
00039 cout<<"SAVING into FILE "<<file<<endl;
00040
00041 if (!strlen(option)) option = "RECREATE";
00042
00043 TFile *hfile = new TFile(file,option,ftitle,compress);
00044 gROOT->cd();
00045 TList *l = gROOT->GetList();
00046 TIter next(l);
00047
00048 TObject *obj;
00049 while((obj = (TObject*) next())) {
00050 if (obj->IsA() == TDirectory::Class()) {
00051 TDirectory * mdir = (TDirectory*)obj;
00052
00053
00054 char * newdir = new char[strlen(file) + strlen(":/") +1];
00055 strcpy(newdir,file);
00056 strcat(newdir,":/");
00057
00058 gDirectory->cd(newdir);
00059 TDirectory * fdir = gDirectory->mkdir(mdir->GetName(),mdir->GetTitle());
00060 fdir->cd();
00061 if (fdir->IsWritable()) {
00062 mdir->GetList()->Write();
00063 fdir->Write();
00064 }
00065 } else {
00066 hfile->cd();
00067 obj->Write();
00068 }
00069 }
00070 hfile->Close();
00071 delete hfile;
00072
00073
00074
00075 return 0;
00076 }
00077
00078
00079
00080 PHTimeStamp * GF::getFileCreationDate(char * fName)
00081 {
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 long id,size,flags,modtime ;
00114 int failed = gSystem->GetPathInfo(fName,&id,&size,&flags,&modtime) ;
00115 if (!failed) {
00116 return new PHTimeStamp(modtime) ;
00117 }
00118 else {
00119 return 0 ;
00120 }
00121
00122
00123 }
00124
00125
00126
00127
00128 void GF::getEventTimeStamp(Event * ev, char * fName, PHTimeStamp * & StartTime, PHTimeStamp *& when){
00129
00130 if(!StartTime) {
00131 PHTimeStamp t0(2000,7,1,0,0,0);
00132 PHTimeStamp tf;
00133 tf.setToSystemTime();
00134
00135 PHTimeStamp * start = getGLV1TimeStamp(ev);
00136
00137 if(!start || (*start)<t0 || (*start)>tf){
00138 if(start) delete start;
00139
00140 if(fName) {
00141 start = GF::getFileCreationDate(fName);
00142 } else {
00143 start = new PHTimeStamp(0);
00144 start->setToSystemTime();
00145 }
00146 }
00147 StartTime = start;
00148 }
00149
00150
00151 when = getGLV1TimeStamp(ev);
00152 if(!when) {
00153 when = new PHTimeStamp(0);
00154 time_t tics = StartTime->getTics()+ev->getEvtSequence();
00155 when->setTics(tics);
00156 }
00157 }
00158
00159 PHTimeStamp * GF::getGLV1TimeStamp(Event * ev)
00160 {
00161
00162
00163
00164
00165
00166 Packet * p = ev->getPacket(14001);
00167 if (!p) return 0 ;
00168
00169 return new PHTimeStamp(p->iValue(0,"YEAR"), p->iValue(0,"MONTH"),
00170 p->iValue(0,"DATE"), p->iValue(0,"HOUR"),
00171 p->iValue(0,"MIN"), p->iValue(0,"SEC"));
00172 }
00173
00174
00175
00176
00177
00178
00179 int GF::reducedMean(int items, float * e, float & av, float & rms, float retain){
00180 if(!items) return 0;
00181 int * index = new int [items];
00182 TMath::Sort(items, e, index);
00183 int nz = items;
00184 for(int i = items-1;i>=0;i--){
00185 if(e[index[i]]!=0.) break;
00186 nz--;
00187 }
00188 if(nz<2) return 0;
00189 int start = 0;
00190 int end = nz-1;
00191 if(nz>=3) {
00192 av = std::accumulate(e, e+nz, 0.0F)/nz;
00193
00194
00195
00196
00197
00198 int minEnt = static_cast<int>((nz<=4? nz-- : nz*retain));
00199
00200 while(nz>minEnt){
00201 if(abs(e[index[start]]-av)>abs(e[index[end]]-av)) start++; else end--;
00202 nz--;
00203 }
00204 }
00205 float sum = 0.;
00206 float sum2 = 0.;
00207 float used = 0.;
00208 for (int i = start; i<end;i++){
00209 sum += e[index[i]];
00210 sum2+= e[index[i]]*e[index[i]];
00211 used +=1.;
00212 }
00213 if(used>0){
00214 av = sum/used;
00215 sum2 /=used;
00216 rms = sum2-av*av;
00217 rms = ((rms>0)? sqrt(rms) : 0.);
00218 } else {
00219 av = 0.;
00220 rms= 0.;
00221 }
00222 delete [] index;
00223 return (int)used;
00224 }
00225
00226
00227
00228
00229 char * GF::getNextFileName(ifstream & conffile, PHTimeStamp * & fTS)
00230 {
00231
00232
00233 char line[200];
00234 char * fName = new char [200];
00235 char c;
00236 while(conffile>>line){
00237 if(!strcmp(line, "newFile")){
00238
00239 conffile>>fName;
00240 } else if (line[0]!='/') {
00241
00242 while(conffile.get(c)&&c!='\n');
00243 continue;
00244 } else {
00245 strcpy(fName,line);
00246 }
00247
00248 while(conffile.get(c)&&c==' ');
00249 fTS = new PHTimeStamp(2000,7,1,0,0,0);
00250 if(c!='\n') {
00251 TString d;
00252 d += c;
00253 while(conffile.get(c)&&c!='\n') {d += c;}
00254 fTS->set(d.Data());
00255 } else {
00256
00257 delete fTS;
00258 fTS = GF::getFileCreationDate(fName);
00259 }
00260 return fName;
00261 }
00262 return 0;
00263 }
00264
00265
00266
00267 void GF::drawGrid(TH2 * h, TLine & l){
00268 if((h->GetNbinsX())%12||(h->GetNbinsY())%12) return;
00269
00270 TAxis * y = ((TH1*)h)->GetYaxis();
00271
00272
00273
00274 int yMin = (int)y->GetXmin();
00275 int yMax = (int)y->GetXmax();
00276 l.DrawLine(12,yMin,12,yMax);
00277 l.DrawLine(24,yMin,24,yMax);
00278 l.DrawLine(36,yMin,36,yMax);
00279 l.DrawLine(48,yMin,48,yMax);
00280 l.DrawLine(60,yMin,60,yMax);
00281 if(yMin<=36&&yMax>=36){
00282 l.DrawLine(0,12,72,12);
00283 l.DrawLine(0,24,72,24);
00284 }
00285 if(yMin<=72&&yMax>=72){
00286 l.DrawLine(0,48,72,48);
00287 l.DrawLine(0,60,72,60);
00288 }
00289 if(yMin<=96&&yMax>=96){
00290 l.DrawLine(0,84,72,84);
00291 l.DrawLine(0,96,72,96);
00292 }
00293 if(yMin<=132&&yMax>=132){
00294 l.DrawLine(0,120,72,120);
00295 l.DrawLine(0,132,72,132);
00296 }
00297 if(yMin<=168&&yMax>=168){
00298 l.DrawLine(0,156,72,156);
00299 l.DrawLine(0,168,72,168);
00300 }
00301 if(yMin<=216&&yMax>=216){
00302 l.DrawLine(0,192,72,192);
00303 l.DrawLine(0,204,72,204);
00304 }
00305 l.SetLineColor(2);
00306 if(yMin<=36&&yMax>=36) l.DrawLine(0,36,72,36);
00307 if(yMin<=72&&yMax>=72) l.DrawLine(0,72,72,72);
00308 if(yMin<=108&&yMax>=108)l.DrawLine(0,108,72,108);
00309 if(yMin<=144&&yMax>=144)l.DrawLine(0,144,72,144);
00310 if(yMin<=180&&yMax>=180)l.DrawLine(0,180,72,180);
00311 l.SetLineColor(1);
00312 }
00313