CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/Calibration/HcalCalibAlgos/src/HcalConstantsASCIIWriter.cc

Go to the documentation of this file.
00001 // system include files
00002 #include <memory>
00003 #include <string>
00004 #include <iostream>
00005 
00006 // user include files
00007 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00008 #include "Calibration/HcalCalibAlgos/interface/HcalConstantsASCIIWriter.h"
00009 #include "DataFormats/Provenance/interface/Provenance.h"
00010 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00011 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00012 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00013 #include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
00014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00015 #include "FWCore/ParameterSet/interface/FileInPath.h"
00016 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00017 #include "DataFormats/DetId/interface/DetId.h"
00018 #include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
00019 
00020 #include "CondFormats/HcalObjects/interface/HcalRespCorrs.h"
00021 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00022 #include "HepMC/GenParticle.h"
00023 #include "HepMC/GenVertex.h"
00024 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbASCIIIO.h"
00025 #include "CondFormats/DataRecord/interface/HcalRespCorrsRcd.h"
00026 #include <fstream>
00027 #include <sstream>
00028 #include <map>
00029 #include <vector>
00030 
00031 using namespace reco;
00032 //
00033 // constructors and destructor
00034 //
00035 namespace cms{
00036 HcalConstantsASCIIWriter::HcalConstantsASCIIWriter(const edm::ParameterSet& iConfig)
00037 {
00038   // get name of output file with histogramms
00039   file_input="Calibration/HcalCalibAlgos/data/"+iConfig.getParameter <std::string> ("fileInput")+".txt";
00040   file_output="Calibration/HcalCalibAlgos/data/"+iConfig.getParameter <std::string> ("fileOutput")+".txt"; 
00041 }
00042 
00043 HcalConstantsASCIIWriter::~HcalConstantsASCIIWriter()
00044 {
00045  
00046    // do anything here that needs to be done at desctruction time
00047    // (e.g. close files, deallocate resources etc.)
00048 
00049 }
00050 
00051 void HcalConstantsASCIIWriter::beginJob()
00052 {
00053     edm::FileInPath f1(file_output);
00054     std::string fDataFile = f1.fullPath();
00055 
00056   myout_hcal = new ofstream(fDataFile.c_str());
00057   if(!myout_hcal) std::cout << " Output file not open!!! "<<std::endl;
00058     
00059 }
00060 
00061 void HcalConstantsASCIIWriter::endJob()
00062 {
00063    delete myout_hcal; 
00064 }
00065 
00066 
00067 //
00068 // member functions
00069 //
00070 
00071 // ------------ method called to produce the data  ------------
00072 void
00073 HcalConstantsASCIIWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00074 {
00075 
00076   std::cout<<" Start HcalConstantsASCIIWriter::analyze "<<std::endl;
00077   
00078     edm::ESHandle<HcalRespCorrs> r;
00079     iSetup.get<HcalRespCorrsRcd>().get(r);
00080     HcalRespCorrs* oldRespCorrs = new HcalRespCorrs(*r.product());
00081 //    std::vector<DetId> dd = oldRespCorrs->getAllChannels();
00082   
00083    edm::ESHandle<CaloGeometry> pG;
00084    iSetup.get<CaloGeometryRecord>().get(pG);
00085    const CaloGeometry* geo = pG.product();
00086 //   iSetup.get<HcalDbRecord>().get(conditions);
00087    
00088    std::vector<DetId> did =  geo->getValidDetIds();
00089 
00090  
00091     std::map<HcalDetId,float> corrold;
00092     //map<HcalDetId,float> corrnew; 
00093     
00094     int mysubd,depth,ieta,iphi;
00095     float coradd,corerr;
00096      
00097     std::vector<HcalDetId> theVector; 
00098     for(std::vector<DetId>::iterator i = did.begin(); i != did.end(); i++)
00099     {
00100       if( (*i).det() == DetId::Hcal ) { 
00101       HcalDetId hid = HcalDetId(*i);
00102       theVector.push_back(hid);
00103       corrold[hid] = (oldRespCorrs->getValues(*i))->getValue();
00104       std::cout<<" Old calibration "<<hid.depth()<<" "<<hid.ieta()<<" "<<hid.iphi()<<std::endl;
00105       } 
00106     }
00107 
00108     std::cout<<" Get old calibration "<<std::endl;
00109 // Read new corrections from file
00110 
00111     edm::FileInPath f1(file_input);
00112     std::string fDataFile = f1.fullPath();
00113      
00114     std::ifstream in( fDataFile.c_str() );
00115     std::string line;
00116     
00117    double corrnew_p[5][5][45][75]; 
00118    double corrnew_m[5][5][45][75];
00119    std::cout<<" Start to read txt file "<<fDataFile.c_str()<<std::endl; 
00120     while( std::getline( in, line)){
00121 
00122 //    std::cout<<" Line size "<<line.size()<< " "<<line<< std::endl;
00123 
00124       if(!line.size() || line[0]=='#') continue;
00125       std::istringstream linestream(line);
00126 
00127       linestream>>mysubd>>depth>>ieta>>iphi>>coradd>>corerr;
00128 //      DetId mydid(DetId::Hcal,HcalSubdetector(mysubd));
00129 //      HcalDetId  hid(HcalSubdetector(mysubd),ieta,iphi,depth);
00130 //        HcalDetId hid(mydid);
00131 //      std::cout<<" Check mysubd "<<hid.subdet()<<" depth "<<hid.depth()<<" ieta "<<hid.ieta()<<" iphi "<<hid.iphi()<<" "<<hid.rawId()<< std::endl;
00132       int ietak = ieta; 
00133       if(ieta<0) ietak = -1*ieta;
00134       if(ieta>0) corrnew_p[mysubd][depth][ietak][iphi] =  coradd; 
00135       if(ieta<0) corrnew_m[mysubd][depth][ietak][iphi] =  coradd;
00136       std::cout<<" Try to initialize mysubd "<<mysubd<<" depth "<<depth<<" ieta "<<ieta<<" "<<ietak<<" iphi "<<iphi<<" "<<coradd<<
00137       std::endl;
00138     } 
00139     
00140     HcalRespCorrs* mycorrections = new HcalRespCorrs(); 
00141     
00142    for(std::vector<HcalDetId>::iterator it = theVector.begin(); it != theVector.end(); it++)
00143    {
00144      float cc1 = (*corrold.find(*it)).second;
00145  //    float cc2 = (*corrnew.find(*it)).second;
00146      float cc2 = 0.;
00147      int ietak = (*it).ieta();
00148 
00149      if((*it).ieta()<0) ietak=-1*(*it).ieta();
00150 
00151      if((*it).ieta()>0) cc2 = corrnew_p[(*it).subdet()][(*it).depth()][ietak][(*it).iphi()];
00152      if((*it).ieta()<0) cc2 = corrnew_m[(*it).subdet()][(*it).depth()][ietak][(*it).iphi()];
00153 
00154      float cc = cc1*cc2;
00155       std::cout<<" Multiply "<<(*it).subdet()<<" "<<(*it).depth()<<" "<<(*it).ieta()<<" "<<ietak<<" "<<(*it).iphi()<<" "<<(*it).rawId()<<" "<<cc1<<" "<<cc2<<std::endl;
00156 
00157   // now make the basic object for one cell with HcalDetId myDetId containing the value myValue
00158       HcalRespCorr item ((*it).rawId(), cc);
00159       mycorrections->addValues(item);
00160    }   
00161 
00162     HcalRespCorrs mycc = *mycorrections;
00163     HcalDbASCIIIO::dumpObject (*myout_hcal, mycc);
00164 
00165 }
00166 }
00167 //define this as a plug-in
00168 //DEFINE_FWK_MODULE(HcalConstantsASCIIWriter)
00169