convertNormalization.C

Go to the documentation of this file.
00001 #include "emcManageable.h"
00002 #include "emcDataManager.h"
00003 #include "EmcIndexer.h"
00004 #include "PHTimeStamp.h"
00005 #include <iostream>
00006 #include <vector>
00007 #include <cstdlib>
00008 #include "emcCalibrationData.h"
00009 #include "emcGainFEM.h"
00010 #include "emcQAFEM.h"
00011 #include <fstream>
00012 #include <sstream>
00013 #include "convertNormalization.h"
00014 #include "emcQAs.h"
00015 
00016 using namespace std;
00017 
00018 // Reads IniCal and Gains to produce "normalization" files, to
00019 // be used e.g. by LV2. 
00020 //
00021 // Usage: convertNormalization(dataSource, [timestamp ptr])
00022 
00023 void convertSector(const emcCalibrationData& sector, 
00024                    const PHTimeStamp& when, 
00025                    std::ostream& out)
00026 {
00027   emcDataManager* dm = emcDataManager::GetInstance();
00028 
00029   vector<emcGainFEM*> gains(18);
00030   vector<emcQAFEM*> qa(18);
00031 
00032   int iS = sector.GetNumber();
00033 
00034   for ( size_t ifem = 0; ifem < gains.size(); ++ifem )
00035     {
00036       // SM144 ranges from 0 to 171 (i.e. absolute fem number for PbSc)
00037       int SM144 = EmcIndexer::iSiSM144_PXSM144(iS,ifem);
00038       gains[ifem] = new emcGainFEM(SM144);
00039       gains[ifem]->SetSource(sector.GetSource());
00040       bool ok = dm->Read(*(gains[ifem]),when);
00041       if (!ok)
00042         {
00043           cerr << "<E> Cannot read gains for fem " << SM144 << endl;
00044           continue;
00045         }
00046       qa[ifem] = new emcQAFEM(SM144);
00047       qa[ifem]->SetSource(sector.GetSource());
00048       ok = dm->Read(*(qa[ifem]),when);
00049        if (!ok)
00050         {
00051           cerr << "<E> Cannot read qa for fem " << SM144 << endl;
00052           continue;
00053         }
00054     }
00055 
00056   ostream::fmtflags oldflags = out.flags() ;
00057 
00058   out << "TowerId FEM FEM-Ch SoftwKey Arm Sector Yrow Zrow   Calib      Bad?" << endl;
00059 
00060   for ( size_t ist = 0; ist < sector.GetSize(); ++ist )
00061     {
00062       int iSM, iSMT;
00063       EmcIndexer::iSiSTiSMiSMT(iS, ist, iSM ,  iSMT);
00064       int towerid = EmcIndexer::iSiSMiSMTiPX(iS, iSM, iSMT);
00065       int ifem, ifemChannel;
00066       EmcIndexer::PXPXSM144CH(towerid,ifem,ifemChannel);
00067 
00068       int arm,sector_in_arm,yrow,zrow;
00069 
00070       EmcIndexer::TowerLocation(towerid,arm,sector_in_arm,yrow,zrow);
00071                         
00072       bool bad=false;
00073 
00074       float calib=0;
00075 
00076       float g = gains[iSM]->getValue(ifemChannel,0);
00077 
00078       if ( g > 0 ) 
00079         {
00080           calib = sector.GetValue(ist,0)*sector.GetValue(ist,1)/g;
00081         }
00082 
00083       if ( static_cast<INT32>(qa[iSM]->getValue(ifemChannel,0)) != 0 ) 
00084         {
00085           bad=true;
00086         }
00087 
00088       out << setw(7) << towerid << " "
00089           << setw(3) << ifem << " "
00090           << setw(6) << ifemChannel << " "
00091           << setw(8) << EmcIndexer::SoftwareKey(towerid) << " "
00092           << setw(3) << arm << " "
00093           << setw(6) << sector_in_arm << " "
00094           << setw(4) << yrow << " "
00095           << setw(4) << zrow << " ";
00096       out.setf(ostream::scientific);
00097       out << setw(13)
00098           << calib << " "
00099           << bad << endl;
00100 
00101 
00102     }
00103 
00104   // Clean up
00105    for ( size_t ifem = 0; ifem < gains.size(); ++ifem )
00106     {
00107       delete gains[ifem];
00108       gains[ifem]=0;
00109     }
00110 
00111    out.setf(oldflags);
00112 }
00113 
00114 //_____________________________________________________________________________
00115 void convertNormalization(emcManageable::EStorage fromWhere, PHTimeStamp* ts)
00116 { 
00117   emcDataManager* dm = emcDataManager::GetInstance();
00118 
00119   for ( size_t isector = 0; isector < 6; ++isector )
00120     {
00121       emcCalibrationData sector(emcCalibrationData::kIniCal,isector);
00122       sector.SetSource(fromWhere);
00123       PHTimeStamp when;
00124       if ( ts ) 
00125         {
00126           when=*ts;
00127         }
00128 
00129       bool ok = dm->Read(sector,when);
00130         
00131       if ( !ok ) 
00132         {
00133           cerr << "<E> Cannot read sector " << isector << endl;
00134           continue;
00135         }
00136       else
00137         {
00138           std::ostringstream name;
00139 
00140           name << EmcIndexer::EmcSectorId(isector) << ".INICAL.LVL2";
00141 
00142           std::ofstream out(name.str().c_str());
00143 
00144           convertSector(sector,when,out);
00145 
00146           out.close();
00147         }
00148     }
00149 }