emcGlobals.C

Go to the documentation of this file.
00001 
00002 // E.Kistenev         8/2/99 
00003 // send comments to kistenev@bnl.gov 
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   //  l->ls();
00048   TObject *obj;
00049   while((obj = (TObject*) next())) {
00050     if (obj->IsA() == TDirectory::Class()) {
00051       TDirectory * mdir = (TDirectory*)obj;
00052       //printf("found a dir %s\n",mdir->GetName());
00053       //gDirectory->pwd();
00054       char * newdir = new char[strlen(file) + strlen(":/") +1];
00055       strcpy(newdir,file);
00056       strcat(newdir,":/");
00057       //printf("going to cd to %s\n",newdir);
00058       gDirectory->cd(newdir);  //  change into file directory
00059       TDirectory * fdir = gDirectory->mkdir(mdir->GetName(),mdir->GetTitle());
00060       fdir->cd();
00061       if (fdir->IsWritable()) {
00062         mdir->GetList()->Write();  //write it into file dir in memory?
00063         fdir->Write();             //write it to file on dir.
00064       }
00065     } else {
00066       hfile->cd();
00067       obj->Write();  //Top level objects.
00068     }
00069   }
00070   hfile->Close();
00071   delete hfile;
00072   //  now is the real problem - getting rid of what we just created (file directories in memory)
00073 
00074   
00075   return 0;  //dummy value for now - needs work. MarkP
00076 }
00077 
00078 // **********************************************************************
00079 
00080 PHTimeStamp * GF::getFileCreationDate(char * fName)
00081 {
00082   //  //  test for existence of the file
00083   //   PHTimeStamp * t;
00084   //   TString command;
00085   //   command += "test -e ";
00086   //   command += fName;
00087   //   if(!system(command.Data())){
00088   //     //    cout<<"File "<<fName<<" found"<<endl;
00089   //     command = "ls -l --full-time ";
00090   //     command += fName;
00091   //     command += " > _fileAttributes_";
00092   //     system(command.Data());
00093   //     ifstream fa;
00094   //     fa.open("_fileAttributes_",ios::in);
00095   //     TString d;
00096   //     char c[200];
00097   //     for(int i=0; i<5; i++) fa>>c;
00098   //     while(fa>>c) {d += c; d+=" ";}
00099   //     t = new PHTimeStamp(0);
00100   //     //    t->set(((char *)d.Data()));
00101   //     t->set(d.Data());
00102   //     //    cout<<*t<<endl;
00103   //     command = "rm -f ";
00104   //     command +="_fileAttributes_";
00105   //     system(command.Data()); 
00106   //     //    cout<<"Att. file removed"<<endl;
00107   //     return t;
00108   //   } else {
00109   //     //    cout<<"File "<<fName<<" not found"<<endl;
00110   //     return 0;
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   //  if Start Time is not known - get it either from this event or from file creation date. In general - it is responcibility of the user to reset the StartTime to NULL when new file is opened.
00130   if(!StartTime) {
00131     PHTimeStamp    t0(2000,7,1,0,0,0);
00132     PHTimeStamp    tf;
00133     tf.setToSystemTime();
00134     //    PHTimeStamp * start = ev->getTimeStamp();
00135     PHTimeStamp * start = getGLV1TimeStamp(ev);
00136     // IF EVENT TIME STAMP IS NONSENCE - BUILD ONE FROM FILE CREATION DATE
00137     if(!start || (*start)<t0 || (*start)>tf){
00138       if(start) delete start;
00139       //  if new StartTime is instantiated here - it is to user to delete it
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   //  Now real event time
00150   //  when = ev->getTimeStamp();
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   // Return a pointer to the timestamp of this event, taken
00162   // from glv1 packet.
00163   // The returned pointer is yours, thus it's up to you to delete it
00164   // at some point.
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     // FIXME: initialization to `int' from `double' !!!
00194     //    int minEnt = (nz<=4? nz-- : nz*0.7);
00195     // Just added the static_cast to pacify the compiler.
00196     // But should check the whole logic of this kind of assignement
00197     // L.A. June-16-2001
00198     int minEnt = static_cast<int>((nz<=4? nz-- : nz*retain));
00199     //    cout<<nz<<" "<<minEnt<<" "<<start<<" "<<index[start]<<" "<<end<<" "<<index[end]<<endl;
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 //  Version of the method of accessing files which includes recreation of the time stamp either using the data provided by user or the file creation date
00228 
00229 char * GF::getNextFileName(ifstream & conffile, PHTimeStamp * & fTS)
00230 {
00231   //  SCAN through config file until "newFile" identifier is found
00232 
00233   char   line[200];
00234   char * fName = new char [200];
00235   char   c;
00236   while(conffile>>line){
00237     if(!strcmp(line, "newFile")){
00238       //  read in the name of the file
00239       conffile>>fName;
00240     } else if (line[0]!='/') {
00241       // skip this line
00242       while(conffile.get(c)&&c!='\n');
00243       continue;
00244     } else {
00245       strcpy(fName,line);
00246     }
00247     //  read in creation date if known
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       //  File name without date
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