CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/L1Trigger/RegionalCaloTrigger/plugins/MaskedRctInputDigiProducer.cc

Go to the documentation of this file.
00001 #include "L1Trigger/RegionalCaloTrigger/interface/MaskedRctInputDigiProducer.h"
00002 
00003 #include <vector>
00004 #include <string>
00005 #include <fstream>
00006 #include <iostream>
00007 using std::ifstream;
00008 using std::vector;
00009 using std::endl;
00010 
00011 //
00012 // constructors and destructor
00013 //
00014 
00015 MaskedRctInputDigiProducer::MaskedRctInputDigiProducer(const edm::ParameterSet& iConfig):
00016   useEcal_(iConfig.getParameter<bool>("useEcal")),
00017   useHcal_(iConfig.getParameter<bool>("useHcal")),
00018   ecalDigisLabel_(iConfig.getParameter<edm::InputTag>("ecalDigisLabel")),
00019   hcalDigisLabel_(iConfig.getParameter<edm::InputTag>("hcalDigisLabel")),
00020   maskFile_(iConfig.getParameter<edm::FileInPath>("maskFile"))
00021 {
00022    //register your products
00023 /* Examples
00024    produces<ExampleData2>();
00025 
00026    //if do put with a label
00027    produces<ExampleData2>("label");
00028 */
00029 
00030   produces<EcalTrigPrimDigiCollection>();
00031   produces<HcalTrigPrimDigiCollection>();
00032 
00033    //now do what ever other initialization is needed
00034   
00035 }
00036 
00037 
00038 MaskedRctInputDigiProducer::~MaskedRctInputDigiProducer()
00039 {
00040  
00041    // do anything here that needs to be done at desctruction time
00042    // (e.g. close files, deallocate resources etc.)
00043 
00044 }
00045 
00046 
00047 //
00048 // member functions
00049 //
00050 
00051 // ------------ method called to produce the data  ------------
00052 void
00053 MaskedRctInputDigiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00054 {
00055    using namespace edm;
00056 /* This is an event example
00057    //Read 'ExampleData' from the Event
00058    Handle<ExampleData> pIn;
00059    iEvent.getByLabel("example",pIn);
00060 
00061    //Use the ExampleData to create an ExampleData2 which 
00062    // is put into the Event
00063    std::auto_ptr<ExampleData2> pOut(new ExampleData2(*pIn));
00064    iEvent.put(pOut);
00065 */
00066 
00067    edm::Handle<EcalTrigPrimDigiCollection> ecal;
00068    edm::Handle<HcalTrigPrimDigiCollection> hcal;
00069 
00070    if (useEcal_) { iEvent.getByLabel(ecalDigisLabel_, ecal); }
00071    if (useHcal_) { iEvent.getByLabel(hcalDigisLabel_, hcal); }
00072 
00073    EcalTrigPrimDigiCollection ecalColl;
00074    HcalTrigPrimDigiCollection hcalColl;
00075    if (ecal.isValid()) { ecalColl = *ecal; }
00076    if (hcal.isValid()) { hcalColl = *hcal; }
00077 
00078 /* this is an EventSetup example
00079    //Read SetupData from the SetupRecord in the EventSetup
00080    ESHandle<SetupData> pSetup;
00081    iSetup.get<SetupRecord>().get(pSetup);
00082 */
00083  
00084    ifstream maskFileStream(maskFile_.fullPath().c_str());
00085    if (!maskFileStream.is_open())
00086      {
00087        throw cms::Exception("FileInPathError")
00088          << "RCT mask file not opened" << std::endl;;
00089      }
00090 
00091    // read and process file (transform mask to new position in phi to match digis)
00092    //char junk[256];
00093    std::string junk;
00094    //char junk[32];
00095    do
00096      {
00097        //maskFileStream.getline(junk, 256);
00098        maskFileStream >> junk;
00099      }
00100    while (junk.compare("ECAL:") != 0);
00101 
00102    //   std::vector<std::vector<std::vector<unsigned short> > > temp(2,std::vector<std::vector<unsigned short> >(72,std::vector<unsigned short>(28,1)));
00103    std::vector<std::vector<std::vector<unsigned short> > > ecalMask(2,std::vector<std::vector<unsigned short> >(72,std::vector<unsigned short>(28,1)));
00104    std::vector<std::vector<std::vector<unsigned short> > > hcalMask(2,std::vector<std::vector<unsigned short> >(72,std::vector<unsigned short>(28,1)));
00105    std::vector<std::vector<std::vector<unsigned short> > > hfMask(2,std::vector<std::vector<unsigned short> >(18,std::vector<unsigned short>(8,1)));
00106 
00107    // read ECAL mask first
00108    // loop through rct iphi
00109    for (int i = 1; i <= 72; i++)
00110      {
00111        int phi_index = (72 + 18 - i) % 72;  // transfrom from RCT coords
00112        if (phi_index == 0) {phi_index = 72;}
00113        //std::cout << "ecal phi index is " << phi_index << std::endl;
00114        for (int j = 28; j >= 1; j--)
00115          {
00116            maskFileStream >> junk;
00117            if (junk.compare("-") == 0) 
00118              {}
00119            else if ((junk.compare("X") == 0) || (junk.compare("x") == 0))
00120              {
00121                ecalMask.at(0).at(phi_index-1).at(j-1) = 0;
00122              }
00123            else
00124              {
00125                std::cerr << "RCT mask producer: error -- unrecognized character" << std::endl;
00126              }
00127          }
00128        for (int j = 1; j <= 28; j++)
00129          {
00130            maskFileStream >> junk;
00131            if(junk.compare("-") == 0)
00132              {}
00133            else if((junk.compare("X") == 0) || (junk.compare("x") == 0))
00134              {
00135                ecalMask.at(1).at(phi_index-1).at(j-1) = 0;
00136              }
00137            else
00138              {
00139                std::cerr << "RCT mask producer: error -- unrecognized character" << std::endl;
00140              }     
00141          }
00142      }
00143    //std::cout << "done reading ECAL" << std::endl;
00144 
00145    maskFileStream >> junk;
00146    if (junk.compare("HCAL:") != 0)
00147      {
00148        throw cms::Exception("FileInPathError")
00149          << "RCT mask producer: error reading ECAL mask" << std::endl;;
00150      }
00151 
00152    //std::cout << "Starting HCAL read" << std::endl;
00153 
00154    // now read HCAL mask
00155    // loop through rct iphi
00156    for (int i = 1; i <= 72; i++)
00157      {
00158        int phi_index = (72 + 18 - i) % 72;  // transfrom from RCT coords
00159        if (phi_index == 0) {phi_index = 72;}
00160        //std::cout << "hcal phi index is " << phi_index << std::endl;
00161        for (int j = 28; j >= 1; j--)
00162          {
00163            maskFileStream >> junk;
00164            if (junk.compare("-") == 0) 
00165              {}
00166            else if ((junk.compare("X") == 0) || (junk.compare("x") == 0))
00167              {
00168                hcalMask.at(0).at(phi_index-1).at(j-1) = 0;
00169              }
00170            else
00171              {
00172                std::cerr << "RCT mask producer: error -- unrecognized character" << std::endl;
00173              }
00174          }
00175        for (int j = 1; j <= 28; j++)
00176          {
00177            maskFileStream >> junk;
00178            if(junk.compare("-") == 0)
00179              {}
00180            else if((junk.compare("X") == 0) || (junk.compare("x") == 0))
00181              {
00182                hcalMask.at(1).at(phi_index-1).at(j-1) = 0;
00183              }
00184            else
00185              {
00186                std::cerr << "RCT mask producer: error -- unrecognized character" << std::endl;
00187              }     
00188          }
00189      }
00190 
00191    maskFileStream >> junk;
00192    if (junk.compare("HF:") != 0)
00193      {
00194        throw cms::Exception("FileInPathError")
00195          << "RCT mask producer: error reading HCAL mask" << std::endl;;
00196      }       
00197 
00198    // loop through the hf phi columns in file
00199    for(int i = 0; i < 18; i++)
00200      {
00201        //std::cout << "iphi for hf file read is " << i << std::endl;
00202        for (int j = 4; j >= 1; j--)
00203          {
00204            if (maskFileStream >> junk) {}
00205            else { std::cerr << "RCT mask producer: error reading HF mask" << std::endl; }
00206            if (junk.compare("-") == 0) 
00207              {}
00208            else if ((junk.compare("X") == 0) || (junk.compare("x") == 0))
00209              {
00210                hfMask.at(0).at(i).at(j-1) = 0;  // just save iphi as 0-17, transform later
00211              }
00212            else
00213              {
00214                std::cerr << "RCT mask producer: error -- unrecognized character" << std::endl;
00215              }
00216          }
00217        for (int j = 1; j <= 4; j++)
00218          {
00219            if (maskFileStream >> junk) {}
00220            else { std::cerr << "RCT mask producer: error reading HF mask" << std::endl; }
00221            if (junk.compare("-") == 0) 
00222              {}
00223            else if ((junk.compare("X") == 0) || (junk.compare("x") == 0))
00224              {
00225                hfMask.at(1).at(i).at(j-1) = 0;
00226              }
00227            else
00228              {
00229                std::cerr << "RCT mask producer: error -- unrecognized character" << std::endl;
00230              }
00231          }         
00232      }
00233 
00234    //std::cout << "HF read done" << std::endl;
00235 
00236    maskFileStream.close();
00237 
00238    //std::cout << "file closed" << std::endl;
00239 
00240    // apply mask
00241 
00242    std::auto_ptr<EcalTrigPrimDigiCollection>
00243      maskedEcalTPs(new EcalTrigPrimDigiCollection());
00244    std::auto_ptr<HcalTrigPrimDigiCollection>
00245      maskedHcalTPs(new HcalTrigPrimDigiCollection());
00246    maskedEcalTPs->reserve(56*72);
00247    maskedHcalTPs->reserve(56*72+18*8);
00248    int nEcalSamples = 0;
00249    int nHcalSamples = 0;
00250 
00251    for (unsigned int i = 0; i < ecalColl.size(); i++)
00252      {
00253        nEcalSamples = ecalColl[i].size();
00254        short ieta = (short) ecalColl[i].id().ieta();
00255        unsigned short absIeta = (unsigned short) abs(ieta);
00256        int sign = ieta / absIeta;
00257        short iphi = (unsigned short) ecalColl[i].id().iphi();
00258        //if (i < 20) {std::cout << "ieta is " << ieta << ", absIeta is " << absIeta
00259        //                     << ", iphi is " << iphi << std::endl;}
00260 
00261        EcalTriggerPrimitiveDigi
00262          ecalDigi(EcalTrigTowerDetId(sign, EcalTriggerTower, absIeta, iphi));
00263        ecalDigi.setSize(nEcalSamples);
00264 
00265        for (int nSample = 0; nSample < nEcalSamples; nSample++)
00266          {
00267            
00268            int energy = 0;
00269            bool fineGrain = false;
00270            
00271            if (sign < 0)
00272              {
00273                //std::cout << "eta-: mask is " << ecalMask.at(0).at(iphi-1).at(absIeta-1) << std::endl;
00274                energy = ecalMask.at(0).at(iphi-1).at(absIeta-1) * ecalColl[i].sample(nSample).compressedEt();
00275                fineGrain = ecalMask.at(0).at(iphi-1).at(absIeta-1) * ecalColl[i].sample(nSample).fineGrain();
00276              }
00277            else if (sign > 0)
00278              {
00279                //std::cout << "eta+: mask is " << ecalMask.at(1).at(iphi-1).at(absIeta-1) << std::endl;    
00280                energy = ecalMask.at(1).at(iphi-1).at(absIeta-1) * ecalColl[i].sample(nSample).compressedEt();
00281                fineGrain = ecalMask.at(1).at(iphi-1).at(absIeta-1) * ecalColl[i].sample(nSample).fineGrain();
00282              }
00283            
00284            ecalDigi.setSample(nSample, EcalTriggerPrimitiveSample(energy,
00285                                                         fineGrain,
00286                                                         0));
00287          }
00288        maskedEcalTPs->push_back(ecalDigi);
00289      }
00290    //std::cout << "End of ecal digi masking" << std::endl;
00291 
00292    //std::cout << "nHcalDigis is " << hcalColl.size() << std::endl;
00293    for (unsigned int i = 0; i < hcalColl.size(); i++)
00294      {
00295        nHcalSamples = hcalColl[i].size();
00296        //if ((i % 100) == 0 ) {std::cout << "Loop " << i << std::endl;}
00297        short ieta = (short) hcalColl[i].id().ieta();
00298        unsigned short absIeta = (unsigned short) abs(ieta);
00299        int sign = ieta / absIeta;
00300        short iphi = (unsigned short) hcalColl[i].id().iphi();
00301        //if (i < 20) {std::cout << "ieta is " << ieta << ", absIeta is " << absIeta
00302        //      << ", iphi is " << iphi << std::endl;}
00303        /*
00304        if (hcalColl[i].SOI_compressedEt() != 0)
00305          {
00306            std::cout << "original et " << hcalColl[i].SOI_compressedEt() 
00307                      << "  fg " << hcalColl[i].SOI_fineGrain() << "  iphi " 
00308                      << iphi << "  ieta " << ieta << std::endl;
00309          }
00310        */
00311        HcalTriggerPrimitiveDigi
00312          hcalDigi(HcalTrigTowerDetId(ieta,iphi));
00313        hcalDigi.setSize(nHcalSamples);
00314        hcalDigi.setPresamples(hcalColl[i].presamples());
00315 
00316        for (int nSample = 0; nSample < nHcalSamples; nSample++)
00317          {
00318            
00319            int energy = 0;
00320            bool fineGrain = false;
00321            
00322            if (absIeta < 29)
00323              {
00324                if (sign < 0)
00325                  {
00326                    energy = hcalMask.at(0).at(iphi-1).at(absIeta-1) * hcalColl[i].sample(nSample).compressedEt();
00327                    fineGrain = hcalMask.at(0).at(iphi-1).at(absIeta-1) * hcalColl[i].sample(nSample).fineGrain();
00328                  }
00329                else if (sign > 0)
00330                  {
00331                    energy = hcalMask.at(1).at(iphi-1).at(absIeta-1) * hcalColl[i].sample(nSample).compressedEt();
00332                    fineGrain = hcalMask.at(1).at(iphi-1).at(absIeta-1) * hcalColl[i].sample(nSample).fineGrain();
00333                  }
00334              }
00335            else if ((absIeta >= 29) && (absIeta <= 32))
00336              {
00337                //std::cout << "hf iphi: " << iphi << std::endl;
00338                short hf_phi_index = iphi/4;
00339                //iphi = iphi/4;  // change from 1,5,9, etc to access vector positions
00340                //std::cout << "hf phi index: " << hf_phi_index << std::endl;
00341                if (sign < 0)
00342                  {
00343                    //std::cout << "ieta is " << ieta << ", absIeta is " << absIeta << ", iphi is " << iphi << std::endl;
00344                    //std::cout << "eta-: mask is " << hfMask.at(0).at(hf_phi_index).at(absIeta-29) << std::endl; // hf ieta 0-3
00345                    energy = hfMask.at(0).at(hf_phi_index).at(absIeta-29) * hcalColl[i].sample(nSample).compressedEt();  // for hf, iphi starts at 0
00346                    fineGrain = hfMask.at(0).at(hf_phi_index).at(absIeta-29) * hcalColl[i].sample(nSample).fineGrain();
00347                  }
00348                else if (sign > 0)
00349                  {
00350                    //std::cout << "ieta is " << ieta << ", absIeta is " << absIeta << ", iphi is " << iphi << std::endl;
00351                    //std::cout << "eta+: mask is " << hfMask.at(1).at(hf_phi_index).at(absIeta-29) << std::endl;
00352                    energy = hfMask.at(1).at(hf_phi_index).at(absIeta-29) * hcalColl[i].sample(nSample).compressedEt();
00353                    fineGrain = hfMask.at(1).at(hf_phi_index).at(absIeta-29) * hcalColl[i].sample(nSample).fineGrain();
00354                  }
00355                //iphi = iphi*4 + 1; // change back to original
00356                //std::cout << "New hf iphi = " << iphi << std::endl;
00357              }
00358            
00359            hcalDigi.setSample(nSample, HcalTriggerPrimitiveSample(energy,
00360                                                                   fineGrain,
00361                                                                   0, 0));
00362            
00363            //if (hcalDigi.SOI_compressedEt() != 0)
00364            //{
00365            //  std::cout << "et " << hcalDigi.SOI_compressedEt()
00366            //     << "fg " << hcalDigi.SOI_fineGrain() << std::endl;
00367            //}
00368          }
00369        maskedHcalTPs->push_back(hcalDigi);
00370      }
00371    //std::cout << "End of hcal digi masking" << std::endl;
00372 
00373    // put new data into event
00374 
00375    iEvent.put(maskedEcalTPs);
00376    iEvent.put(maskedHcalTPs);
00377 
00378 }
00379 
00380 // ------------ method called once each job just before starting event loop  ------------
00381 
00382 
00383 // ------------ method called once each job just after ending the event loop  ------------
00384 void 
00385 MaskedRctInputDigiProducer::endJob() {
00386 }
00387 
00388 //define this as a plug-in
00389 DEFINE_FWK_MODULE(MaskedRctInputDigiProducer);