mEmcMIPCorr3Module.C

Go to the documentation of this file.
00001 //-------------------------------------------------------------------------
00002 // 
00003 // Package: offline/packages/emc
00004 // 
00005 // Copyright (C) PHENIX collaboration, 2000 
00006 //
00007 // Implementation of class : mEmcMIPCorr3Module.C for Run2
00008 //
00009 // Hisayuki Torii
00010 //-------------------------------------------------------------------------
00011 #include "mEmcMIPCorr3Module.h"
00012 #include "dEmcCalibTowerWrapper.h"
00013 #include "dEmcCalibTower.h"
00014 
00015 #include "PHNode.h"
00016 #include "PHCompositeNode.h"
00017 #include "PHIODataNode.h"
00018 #include "PHNodeIterator.h"
00019 #include "PHDataNodeIterator.h"
00020 
00021 #include "RunHeader.h"
00022 #include <iostream>
00023 #include <fstream>
00024 
00025 using namespace std;
00026 
00027 typedef PHIODataNode <RunHeader> RunHeaderNode_t;
00028 
00029 mEmcMIPCorr3Module* mEmcMIPCorr3Module::_instance = NULL;
00030 //-------------------------------------------------------------------------
00031 mEmcMIPCorr3Module::mEmcMIPCorr3Module()
00032 { 
00033   // default ctor
00034   fVerbose = 0 ;
00035   int iarm,isect,iz,iy;
00036   iarm = 2;
00037   while( iarm-- ){
00038     isect = 4;
00039     while( isect-- ){
00040       _last_run[iarm][isect] = 0;
00041       _last_run_mip[iarm][isect] = 1.0;
00042       iz = 96;
00043       while( iz-- ){
00044         iy = 48;
00045         while( iy-- ){
00046           _corr_twr_mip[iarm][isect][iz][iy] = 1;
00047           _corr_twr_err[iarm][isect][iz][iy] = 0;
00048           _corr_twr_stat[iarm][isect][iz][iy] = false;
00049         }
00050       }
00051     }
00052   }
00053 
00054   return;
00055 }
00056 //-------------------------------------------------------------------------
00057 mEmcMIPCorr3Module* mEmcMIPCorr3Module::instance()
00058 { 
00059   if(! _instance )
00060     _instance = new mEmcMIPCorr3Module();
00061   return _instance;
00062 }
00063 //-------------------------------------------------------------------------
00064 void mEmcMIPCorr3Module::readfile_twr(char* filename)
00065 { 
00066   int iarm,isect,iz,iy,status;
00067   int read_num;
00068   int swkey;
00069   float corr,corr_err;
00070   char tmpline[128];
00071   ifstream fin(filename);
00072   if(!fin) {
00073     cerr<<" mEmcMIPCorr3Module:: Can't open file: "<<filename<<endl;
00074     return;
00075   }
00076   cout<<" mEmcMIPCorr3Module:: Open file : "<<filename<<endl;
00077   while( fin.getline(tmpline,128) ){
00078     if( tmpline[0] != '#' ){
00079       read_num = sscanf(tmpline,"%d %f %f %d",&swkey,&corr,&corr_err,&status);
00080       if( read_num == 4 ){
00081         iarm = (int)( swkey / 100000 );
00082         isect = (int)( ( swkey - iarm * 100000 )/10000 );
00083         iy = (int)( ( swkey - iarm*100000 - isect*10000 )/100 );
00084         iz = (int)( swkey - iarm*100000 - isect*10000 - iy*100 );
00085         if( iarm >= 0 && iarm < 2 &&
00086             isect >= 0 && isect < 4 &&
00087             iy >= 0 && iy < 48 &&
00088             iz >= 0 && iz < 96 ){
00089           _corr_twr_mip[iarm][isect][iz][iy] = corr;
00090           _corr_twr_err[iarm][isect][iz][iy] = corr_err;
00091           if( status > 0 )
00092             _corr_twr_stat[iarm][isect][iz][iy] = true;
00093           else
00094             _corr_twr_stat[iarm][isect][iz][iy] = false;
00095         } else {
00096           cout<<" Error in mEmcMIPCorr3Module::readfile() : "<<endl;
00097           cout<<"             -- Tower id is out of range : "<<filename<<endl;
00098         }
00099       } else {
00100       }
00101     }
00102   }
00103   fin.close();
00104 
00105   return;
00106 }
00107 //-------------------------------------------------------------------------
00108 void mEmcMIPCorr3Module::readfile_run(char* filename)
00109 { 
00110   int irun,iarm,isect,status;
00111   int read_num;
00112   float corr,corr_err;
00113   char tmpline[128];
00114   ifstream fin(filename);
00115   if(!fin) {
00116     cerr<<" mEmcCIPCorr3Module:: Can't open file: "<<filename<<endl;
00117     return;
00118   }
00119   cout<<" mEmcMIPCorr3Module:: Open file : "<<filename<<endl;
00120 
00121   while( fin.getline(tmpline,128) ){
00122     if( tmpline[0] != '#' ){
00123       read_num = sscanf(tmpline,"%d %d %d %f %f %d",&irun,&iarm,&isect,&corr,&corr_err,&status);
00124       if( read_num == 6 ){
00125         if( status > 0 ){
00126           _corr_run[iarm][isect].push_back(irun);
00127           _corr_run_mip[iarm][isect].push_back(corr);
00128           _corr_run_err[iarm][isect].push_back(corr_err);
00129         }
00130       } else {
00131       }
00132     }
00133   }
00134   fin.close();
00135 
00136   return;
00137 }
00138 //-------------------------------------------------------------------------
00139 float mEmcMIPCorr3Module::get_corr_run(int iarm,int isect, int run){
00140   float corr_run;
00141   if( run == _last_run[iarm][isect] ){
00142     corr_run = _last_run_mip[iarm][isect];
00143   } else {
00144     vector<int>& vec_run = _corr_run[iarm][isect];
00145     vector<float>& vec_run_mip = _corr_run_mip[iarm][isect];
00146     //
00147     int n = vec_run.size();
00148     int min_n = 0;
00149     int rundiff;
00150     int min_rundiff = 500;
00151     while( n-- ){
00152       rundiff = abs( vec_run[n] - run );
00153       if( rundiff < min_rundiff ){
00154         min_rundiff = rundiff;
00155         min_n = n;
00156       }
00157     }
00158     if( min_rundiff < 500 ){
00159       _last_run[iarm][isect] = vec_run[min_n];
00160       _last_run_mip[iarm][isect] = vec_run_mip[min_n];
00161       corr_run = _last_run_mip[iarm][isect];
00162     } else
00163       corr_run = 1.0000000;
00164   }
00165   return corr_run;
00166   //
00167 }
00168 //-------------------------------------------------------------------------
00169 void mEmcMIPCorr3Module::print()
00170 { 
00171   int iarm,isect,iz,iy,n;
00172   iarm = 2;
00173   while( iarm-- ){
00174     isect = 4;
00175     while( isect-- ){
00176       iz = ((iarm==1&&isect<2) ? 96 : 72 );
00177       while( iz-- ){
00178         iy = ((iarm==1&&isect<2)? 48 : 36 );
00179         while( iy-- ){
00180           cout<<" isect, iz, iy = "<<isect<<","<<iz<<","<<iy<<"   : "<<_corr_twr_mip[iarm][isect][iz][iy]<<endl;
00181         }
00182       }
00183     }
00184   }
00185   iarm = 2;
00186   while( iarm-- ){
00187     isect = 4;
00188     while( isect-- ){
00189       n = _corr_run[iarm][isect].size();
00190       while( n-- ){
00191         cout<<" irun = "<<_corr_run[iarm][isect][n]<<" : "<<_corr_run_mip[iarm][isect][n]<<" +- "
00192             <<_corr_run_err[iarm][isect][n]<<endl;
00193       }
00194     }
00195   }
00196 
00197   return;
00198 }
00199 //-------------------------------------------------------------------------
00200 PHBoolean mEmcMIPCorr3Module::event(PHCompositeNode *root) 
00201 {  
00202   int iarm,isect,iy,iz;
00203   float energy;
00204   int irow = 0 ;
00205   
00206   if( fVerbose > 0 )
00207     cout << "mEmcMIPCorr3Module >>>  Starting..." << endl;
00208   
00209   // Set up dEmcCalibTower
00210   PHNodeIterator i(root);
00211   PHIODataNode<PHTable>* dEmcCalibTowerNode = (PHIODataNode<PHTable>*)i.findFirst("PHIODataNode","dEmcCalibTower");
00212   dEmcCalibTowerWrapper * dEmcCalibTower = static_cast<dEmcCalibTowerWrapper*>(dEmcCalibTowerNode->getData());
00213 
00214   int irun = -9999;
00215   PHTypedNodeIterator<RunHeader> runiter(root);
00216   RunHeaderNode_t *RunHeaderNode = runiter.find("RunHeader");
00217   if (RunHeaderNode)
00218     {
00219       irun = RunHeaderNode->getData()->get_RunNumber();
00220     }
00221   else
00222     {
00223       cout << PHWHERE << "RunHeader Node missing" << endl;
00224     }
00225   irow = dEmcCalibTower->RowCount();
00226   while( irow-- ){
00227     iarm = dEmcCalibTower->get_arm(irow);
00228     isect = dEmcCalibTower->get_sector(irow);
00229     iz = dEmcCalibTower->get_ind(0,irow);
00230     iy = dEmcCalibTower->get_ind(1,irow);
00231     if( _corr_twr_stat[iarm][isect][iz][iy] ){
00232       energy = dEmcCalibTower->get_ecal(irow) * _corr_twr_mip[iarm][isect][iz][iy] * get_corr_run(iarm,isect,irun);
00233       dEmcCalibTower->set_ecal( irow, energy );
00234       
00235     }
00236   }
00237 #ifdef SKIP_CLUSTER
00238   PHIODataNode<PHTable>* dEmcClusterLocalExtNode = (PHIODataNode<PHTable>*)i.findFirst("PHIODataNode","dEmcClusterLocalExt");
00239   dEmcClusterLocalExtWrapper * dEmcClusterLocalExt = static_cast<dEmcClusterLocalExtWrapper*>(dEmcClusterLocalExtNode->getData());
00240 
00241   irow = dEmcClusterLocalExt->RowCount();
00242   while( irow -- ){
00243     iarm = dEmcClusterLocalExt->get_arm(irow);
00244     isect = dEmcClusterLocalExt->get_sector(irow);
00245     iz = dEmcClusterLocalExt->get_ind(0,irow);
00246     iy = dEmcClusterLocalExt->get_ind(1,irow);
00247     if( _corr_twr_stat[isect][iz][iy] ){
00248       dEmcClusterLocalExt->set_e( irow, dEmcClusterLocalExt->get_e(irow) * _corr_twr_mip[iarm][isect][iz][iy] );
00249       dEmcClusterLocalExt->set_ecore( irow, dEmcClusterLocalExt->get_ecore(irow) * _corr_twr_mip[iarm][isect][iz][iy] );
00250       dEmcClusterLocalExt->set_ecorr( irow, dEmcClusterLocalExt->get_ecorr(irow) * _corr_twr_mip[iarm][isect][iz][iy] );
00251       dEmcClusterLocalExt->set_ecent( irow, dEmcClusterLocalExt->get_ecent(irow) * _corr_twr_mip[iarm][isect][iz][iy] );
00252     }
00253   }
00254 #endif
00255   return true;
00256   
00257 }
00258 //-------------------------------------------------------------------------