mcr/SCC/PlotSilCurrents.C

00001 // @(#)fROOT/mcr/SCC:$Name:  $:$Id: PlotSilCurrents.C,v 1.4 2007/09/24 07:32:41 Diego_Faso Exp $
00002 // Author: Diego Faso <mailto:faso@to.infn.it>, 2007/09/18
00003 
00004 #ifndef FROOT_TFndSccSilPlot
00005 #define FROOT_TFndSccSilPlot
00006 
00007 #include <Riostream.h>
00008 #include "TFile.h"
00009 #include "TFndGraphs.h"
00010 #include "TCanvas.h"
00011 #include "TButton.h"
00012 #include "TDatime.h"
00013 
00014 const Int_t ntotmodules = 18;
00015 const Int_t nisimodules =  8;
00016 
00017 // interpreter/class shared variables
00018 TFndGraph *fGraphs[ntotmodules];
00019 TFndMultiGraph *fCurMultiGraphs;
00020 
00021 TCanvas *fShowCanvas;
00022 TPad *fButPad;
00023 TPad *fDisplayPad;
00024 
00025 TButton *fModBut[ntotmodules];
00026 TButton *fAllBut[2]; // for displaying all modules (ISIM or OSIM)
00027 
00028 
00029 class TFndSccSilPlot : public TNamed
00030 {
00031 private:
00032   TDatime fCurTime;
00033   TDatime fStartTime;
00034   TDatime fStopTime;
00035   FILE *fRawFile;
00036   TString fCurFileName; // raw-filename
00037   
00038   Int_t fNskip;
00039 
00040   Int_t fReadTime;
00041   Float_t fReadCur[ntotmodules];
00042   Int_t fReadInfo[2];
00043   Float_t fLastInfo;
00044 
00045   Int_t fGRCOL[ntotmodules];
00046 
00047 public:
00048   TFndSccSilPlot(Int_t nskip=0);
00049   ~TFndSccSilPlot();
00050 
00051   TDatime GetCurTime() { return fCurTime; }
00052   TDatime GetStartTime() { return fStartTime; }
00053   TDatime GetStopTime()  { return fStopTime; }
00054   void SetStartTime(Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec)
00055   { fStartTime.Set(year,month,day,hour,min,sec); }
00056   void SetStopTime(Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec)
00057   { fStopTime.Set(year,month,day,hour,min,sec) ; }
00058 
00059   void BuildRawFileName();
00060 
00061   void Loop();
00062   void ScanFile();
00063   void FillGraphs();
00064   void PrintBuffers();
00065 
00066   void InitGraphs();
00067 
00068   // --- GUI
00069   void DisplayModule(const Int_t &mod);
00070   void DisplayAllModules(const Int_t &lay);
00071   void DrawShowCanvas();
00072   void UpdateGUI();
00073 
00074   void ResetDisplay(){
00075     if(fDisplayPad) fDisplayPad->Clear();
00076     if(fCurMultiGraphs) fCurMultiGraphs->Reset();
00077     else fCurMultiGraphs = new TFndMultiGraph("cur_multigraph","");
00078     //
00079     fDisplayPad->cd();
00080   }  
00081 
00082 ClassDef(TFndSccSilPlot,1)
00083 };
00084 TFndSccSilPlot *gFndSilPlot;
00085 
00086 #endif // FROOT_TFndSccSilPlot
00087 
00089 ClassImp(TFndSccSilPlot)
00090 
00091 //_______________________________
00092 TFndSccSilPlot::TFndSccSilPlot(Int_t nskip):
00093   fCurTime(),fStartTime(),fStopTime(),
00094   fRawFile(),fCurFileName(),
00095   fNskip(nskip),
00096   fReadTime(),fReadCur(),fReadInfo(),fLastInfo(),
00097   fGRCOL()
00098 {
00099   
00100   gFndSilPlot = this;
00101   //  SetName("FinudaSilPlot");
00102   cout << "ctor called" << endl;
00103   InitGraphs();
00104 }
00105 
00106 //_______________________________
00107 TFndSccSilPlot::~TFndSccSilPlot(){
00108 
00109   cout << "dtor called" << endl;
00110 }
00111 
00112 //_______________________________
00113 void TFndSccSilPlot::InitGraphs(){
00114 
00115   fGRCOL[ 0] = kBlack;
00116   fGRCOL[ 1] = kBlue - 7;
00117   fGRCOL[ 2] = kBlue;
00118   fGRCOL[ 3] = kGreen ;
00119   fGRCOL[ 4] = kGreen -7 ;
00120   fGRCOL[ 5] = kOrange ;
00121   fGRCOL[ 6] = kOrange + 10 ;
00122   fGRCOL[ 7] = kRed -2;
00123   fGRCOL[ 8] = kRed;
00124   fGRCOL[ 9] = kTeal + 10;
00125   //
00126   fGRCOL[10] = kBlack;
00127   fGRCOL[11] = kBlue;
00128   fGRCOL[12] = kGreen;
00129   fGRCOL[13] = kGreen -7;
00130   fGRCOL[14] = kOrange;
00131   fGRCOL[15] = kOrange + 10;
00132   fGRCOL[16] = kRed -2;
00133   fGRCOL[17] = kRed;
00134 
00135   TString gnam = "";
00136   TString gtit = "";
00137   
00138   for(Int_t i=0;i<ntotmodules;i++){
00139     if(i<nisimodules){
00140       gnam.Form("ISIM_%d",i+1);
00141       gtit.Form("ISIM %d",i+1);
00142     }
00143     else{
00144       gnam.Form("OSIM_%d",i-7); 
00145       gtit.Form("OSIM %d",i-7);
00146     }
00147     fGraphs[i] = new TFndGraph(gnam,gtit);
00148     fGraphs[i]->SetMarkerColor(fGRCOL[i]);
00149     fGraphs[i]->GetXaxis()->SetNdivisions(3);
00150     fGraphs[i]->GetXaxis()->SetLabelSize(0.02);
00151   }
00152   
00153   fCurMultiGraphs = 0;
00154   
00155 }
00156 
00157 //_______________________________
00158 void TFndSccSilPlot::BuildRawFileName(){
00159 
00160   TString dnam=""; // date used for filename
00161 
00162   dnam=fCurTime.AsSQLString();
00163   dnam.Resize(10);
00164   dnam.Remove(4,1);
00165   dnam.Remove(6,1);
00166   dnam.Remove(8,1);
00167   
00168   //     cout << "fCurTime: " << fCurTime.AsSQLString();
00169   //     cout << " ( " << dnam << " )." << endl;
00170   
00171   fCurFileName.Form("%s/%scur.raw",
00172             gSystem->ExpandPathName("$FND_SCC_SILCUR"),
00173             dnam.Data()
00174             );
00175   
00176 }
00177 
00178 //_______________________________
00179 void TFndSccSilPlot::Loop(){
00180   
00181   cout << "Starting loop..." << endl;
00182   
00183   fCurTime = fStartTime;
00184   while (fCurTime.Convert() < fStopTime.Convert()){
00185     BuildRawFileName();
00186     cout << "Looking for file: " << fCurFileName.Data() << endl;
00187     
00188     if(gSystem->AccessPathName(fCurFileName.Data()) ==0 ){
00189       cout << "reading file..." << endl;
00190       fRawFile = fopen(fCurFileName.Data(),"r");
00191       ScanFile();
00192       // ---
00193       fclose(fRawFile);
00194     }
00195     else cout << "file not found" << endl;
00196     // ---
00197     fCurTime.Set(fCurTime.Convert() + 86400);
00198   }
00199   
00200   
00201 }
00202 
00203 //_______________________________
00204 void TFndSccSilPlot::ScanFile(){
00205 
00206   cout << "Scanning file." << endl;
00207   
00208 
00209   Int_t res = 1;
00210   Int_t ncols=0;
00211   while (1){
00212     fReadTime = 0;
00213     bzero(fReadCur,sizeof(fReadCur));
00214     bzero(fReadInfo,sizeof(fReadInfo));
00215     fLastInfo = 0;  
00216     //
00217     res = fscanf(fRawFile,
00218                  "%d %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %d %d %f",
00219                  &fReadTime,
00220                  &fReadCur[0],&fReadCur[1],&fReadCur[2],&fReadCur[3],&fReadCur[4],&fReadCur[5],
00221                  &fReadCur[6],&fReadCur[7],&fReadCur[8],&fReadCur[9],&fReadCur[10],&fReadCur[11],
00222                  &fReadCur[12],&fReadCur[13],&fReadCur[14],&fReadCur[15],&fReadCur[16],&fReadCur[17],
00223                  &fReadInfo[0],&fReadInfo[1],&fLastInfo);
00224     //
00225     ncols++;
00226     
00227     //    cout << "RES: " << res << endl;
00228     if(ncols > 500000){
00229       cout << "MORE THAN 500000 lines: skipping" << endl;
00230       break;
00231     }
00232     if(res < 0) break;
00233     // --- debug
00234     //    cout << "ncols: " << ncols << endl;
00235     if(fNskip != 0 && ncols!=0 && ncols%(fNskip+1) != 0 ) continue;
00236     FillGraphs();
00237     //    PrintBuffers();   cout << endl;
00238     // ---
00239 
00240   }
00241   
00242 }
00243 
00244 //_______________________________
00245 void TFndSccSilPlot::FillGraphs(){
00246 
00247   //  cout << fReadTime << " ";
00248   for(Int_t i=0;i<ntotmodules;i++){
00249     // cout << fReadCur[i] << " " ;
00250     fGraphs[i]->AddPoint(fReadTime,fReadCur[i]);
00251   }
00252   //  for(Int_t i=0;i<2;i++) cout << fReadInfo[i] << " ";
00253 }
00254 
00255 //_______________________________
00256 void TFndSccSilPlot::PrintBuffers(){
00257   cout << fReadTime << " ";
00258   for(Int_t i=0;i<ntotmodules;i++) cout << fReadCur[i] << " " ;
00259   for(Int_t i=0;i<2;i++) cout << fReadInfo[i] << " ";
00260   cout << fLastInfo << endl;
00261 }
00262 
00263 //_______________________________
00264 void TFndSccSilPlot::DrawShowCanvas(){
00265 
00266   fShowCanvas = new TCanvas("ShowCanvas","Show Canvas",10,10,1400,770);
00267 
00268   fButPad = new TPad("ButPad","Button Pad",0,0,.203,1,20,1,1);
00269   fDisplayPad = new TPad("DisplayPad","Display Pad",.207,0,1,1,42,1,1);
00270 
00271   fButPad->Draw();
00272   fDisplayPad->Draw();
00273     
00274   // Buttons
00275   fButPad->cd();
00276 
00277   TString bnam = "";
00278   TString cmdstr = "";
00279   Double_t start = 0.01;
00280   Double_t height = 0.04;
00281   Double_t gap = 0.003;
00282   for(Int_t i=0;i<ntotmodules;i++){
00283     if(i<nisimodules) bnam.Form("ISIM_%d",i+1);
00284     else{
00285       if(i==nisimodules) start+=0.04;
00286       bnam.Form("OSIM_%d",i-7);
00287     }
00288     cmdstr.Form("gFndSilPlot->DisplayModule(%d)",i);
00289     fModBut[i] = new TButton(bnam,cmdstr,0.1,start,0.9,start+height);
00290     fModBut[i]->Draw();
00291     start+=(height+gap);
00292   }
00293   //
00294   start+=0.04;
00295   for(Int_t i=0;i<2;i++){
00296     bnam = (i==0)? "ALL_ISIM" : "ALL_OSIM";
00297     cmdstr.Form("gFndSilPlot->DisplayAllModules(%d)",i);
00298     fAllBut[i] = new TButton(bnam,cmdstr,0.1,start,0.9,start+height);
00299     fAllBut[i]->Draw();
00300     start+=(height+gap);
00301   }  
00302   // ---
00303   UpdateGUI();
00304 }
00305 
00306 //_______________________________
00307 void TFndSccSilPlot::DisplayModule(const Int_t &mod){
00308 
00309   if(! fGraphs[mod] ){
00310     cerr << "ERROR: required graph is a NULL pointer!" << endl;
00311     return;
00312   }
00313   if( fGraphs[mod]->GetN() < 2){
00314     cerr << "WARNING: required graph is empty (less than 2 points)." << endl;
00315     return;
00316   }
00317   
00318 
00319   ResetDisplay();
00320   
00321   //   cout << "Displaying module (ID): " << mod << endl;
00322   //   cout << "============> " <<  fGraphs[mod] << endl;
00323   //   cout << "============> " <<  fDisplayPad << endl;
00324 
00325 
00326   // drawing a multigraph is faster
00327   // than drawing a graph: enable it if needed
00328   fCurMultiGraphs->AddFndGraph(fGraphs[mod],"P");
00329   fCurMultiGraphs->DrawDefaults(kTRUE);
00330   
00331   UpdateGUI();
00332 }
00333 
00334 //_______________________________
00335 void TFndSccSilPlot::DisplayAllModules(const Int_t &lay){
00336 
00337   cout << "Displaying all modules for layer: " << lay << endl;
00338 
00339   ResetDisplay();
00340   
00341   Int_t mod_start = (lay==0) ?           0 : nisimodules;
00342   Int_t mod_stop  = (lay==0) ? nisimodules : ntotmodules;
00343     
00344   for(Int_t mod=mod_start;mod<mod_stop;mod++) fCurMultiGraphs->AddFndGraph(fGraphs[mod],"P");
00345   fCurMultiGraphs->DrawDefaults(kTRUE);
00346 
00347   UpdateGUI();
00348 }
00349 
00350 //_______________________________
00351 void TFndSccSilPlot::UpdateGUI(){
00352 
00353   cout << "Updating GUI..." << endl;
00354 
00355   if(fCurMultiGraphs && fCurMultiGraphs->GetHistogram()){
00356     fCurMultiGraphs->GetHistogram()->GetXaxis()->SetNdivisions(5);
00357     fCurMultiGraphs->GetHistogram()->GetXaxis()->SetLabelSize(0.02);
00358     fCurMultiGraphs->GetHistogram()->GetYaxis()->SetLabelSize(0.03);
00359   }
00360 
00361   fDisplayPad->cd();
00362   gPad->Modified();
00363   gPad->Update();
00364 
00365 }
00366 
00367 //_______________________________
00368 //_______________________________
00369 //_______________________________
00370 void PlotSilCurrents(const Int_t &y0,const Int_t &m0,const Int_t &d0,const Int_t &y1,const Int_t &m1,const Int_t &d1,Int_t nskip=10){
00371   // nskip: number of data to be skipped
00372   //        (fill plot every "nskip" data)
00373   //
00374   // examples:
00375   //        nskip =   0 ===>  6 sec
00376   //        nskip =  10 ===>  1 min
00377   //        nskip = 100 ===> 10 min
00378   //        nskip = 600 ===>  1 hour
00379 
00380   TFndSccSilPlot *silplot = new TFndSccSilPlot(nskip);
00381   //
00382   silplot->SetStartTime(y0,m0,d0,0,0,0);
00383   silplot->SetStopTime(y1,m1,d1,0,0,0);
00384   
00385   cout << silplot->GetStartTime().AsString() << endl;
00386   cout << silplot->GetStopTime().AsString() << endl;
00387   
00388   silplot->Loop();
00389 
00390   // --- GUI
00391   silplot->DrawShowCanvas();
00392   // ---
00393   delete silplot;
00394 }

Generated on Tue Oct 16 15:40:47 2007 by  doxygen 1.5.2