CMS 3D CMS Logo

HcalTriggerPrimitiveAlgo.cc

Go to the documentation of this file.
00001 #include "SimCalorimetry/HcalTrigPrimAlgos/interface/HcalTriggerPrimitiveAlgo.h"
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 
00004 #include <iostream>
00005 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00006 #include "Geometry/HcalTowerAlgo/interface/HcalTrigTowerGeometry.h"
00007 #include "DataFormats/HcalDetId/interface/HcalTrigTowerDetId.h"
00008 using namespace std;
00009 
00010 HcalTriggerPrimitiveAlgo::HcalTriggerPrimitiveAlgo(bool pf, const std::vector<double>& w,
00011                                                    int latency, uint32_t FG_threshold)
00012   : incoder_(0), outcoder_(0), theThreshold(0),
00013     peakfind_(pf), weights_(w), latency_(latency), FG_threshold_(FG_threshold)
00014 {
00015 }
00016 
00017 
00018 HcalTriggerPrimitiveAlgo::~HcalTriggerPrimitiveAlgo()
00019 {
00020 }
00021 
00022 
00023 void HcalTriggerPrimitiveAlgo::run(const HcalTPGCoder * incoder,
00024                                    const HcalTPGCompressor * outcoder,
00025                                    const HBHEDigiCollection & hbheDigis,
00026                                    const HFDigiCollection & hfDigis,
00027                                    HcalTrigPrimDigiCollection & result)
00028 {
00029 
00030   incoder_=incoder;
00031   outcoder_=outcoder;
00032 
00033   theSumMap.clear();
00034   theFGSumMap.clear();
00035   theTowerMapFG.clear();
00036   // do the HB/HE digis
00037   for(HBHEDigiCollection::const_iterator hbheItr = hbheDigis.begin();
00038       hbheItr != hbheDigis.end(); ++hbheItr)
00039     {
00040       addSignal(*hbheItr);
00041     }
00042 
00043   // and the HF digis
00044   for(HFDigiCollection::const_iterator hfItr = hfDigis.begin(); 
00045       hfItr != hfDigis.end(); ++hfItr)
00046     {
00047       addSignal(*hfItr);
00048       
00049     }
00050 
00051 
00052   for(SumMap::iterator mapItr = theSumMap.begin(); mapItr != theSumMap.end(); ++mapItr)
00053     {
00054       result.push_back(HcalTriggerPrimitiveDigi(mapItr->first));
00055       HcalTrigTowerDetId detId(mapItr->second.id());
00056       if(detId.ietaAbs() >= theTrigTowerGeometry.firstHFTower())
00057         { analyzeHF(mapItr->second, result.back());}
00058       else{analyze(mapItr->second, result.back());}
00059     }
00060 
00061   theSumMap.clear();  
00062   return;
00063 }
00064 
00065 
00066 void HcalTriggerPrimitiveAlgo::addSignal(const HBHEDataFrame & frame) {
00067 
00068   std::vector<HcalTrigTowerDetId> ids = theTrigTowerGeometry.towerIds(frame.id());
00069   assert(ids.size() == 1 || ids.size() == 2);
00070   IntegerCaloSamples samples1(ids[0], int(frame.size()));
00071 
00072   samples1.setPresamples(frame.presamples());
00073   incoder_->adc2Linear(frame, samples1);
00074   
00075   if(ids.size() == 2) {
00076     // make a second trigprim for the other one, and split the energy
00077     IntegerCaloSamples samples2(ids[1], samples1.size());
00078     for(int i = 0; i < samples1.size(); ++i) 
00079       {
00080         samples1[i] = uint32_t(samples1[i]*0.5);
00081         samples2[i] = samples1[i];
00082       }
00083     samples2.setPresamples(frame.presamples());
00084     addSignal(samples2);
00085   }
00086   addSignal(samples1);
00087 }
00088 
00089 
00090 void HcalTriggerPrimitiveAlgo::addSignal(const HFDataFrame & frame) {
00091 
00092  
00093   if(frame.id().depth() == 1 || frame.id().depth() == 2) {
00094     std::vector<HcalTrigTowerDetId> ids = theTrigTowerGeometry.towerIds(frame.id());
00095     assert(ids.size() == 1);
00096     IntegerCaloSamples samples(ids[0], frame.size());
00097     samples.setPresamples(frame.presamples());
00098     // for(int i = 0; i < frame.size(); i++)
00099     // {cout<<frame.sample(i).adc()<<" ";}
00100     //cout<<endl;
00101     incoder_->adc2Linear(frame, samples);
00102     
00103     addSignal(samples);
00104         
00105     uint32_t fgid;
00106 
00107     // Mask off depths: fgid is the same for both depths
00108 
00109     fgid = (frame.id().rawId() | 0x1c000) ;
00110          
00111     SumMapFG::iterator itr = theFGSumMap.find(fgid);
00112    
00113     if(itr == theFGSumMap.end()) {
00114       theFGSumMap.insert(std::make_pair(fgid, samples));
00115     } 
00116     else {
00117       // wish CaloSamples had a +=
00118       for(int i = 0; i < samples.size(); ++i) {
00119         (itr->second)[i] += samples[i];        
00120       }      
00121     }
00122 
00123     // Depth =2 is the second entry in map (sum). Use its original Hcal Det Id to obtain trigger tower
00124     if (frame.id().depth()==2)
00125       {      
00126       for(unsigned int n = 0; n < ids.size(); n++)
00127           {
00128           theTowerMapFG.insert(TowerMapFG::value_type(ids[n],itr->second));
00129           }
00130       }
00131     
00132   }
00133 }
00134 
00135 
00136 void HcalTriggerPrimitiveAlgo::addSignal(const IntegerCaloSamples & samples) {
00137   HcalTrigTowerDetId id(samples.id());
00138   SumMap::iterator itr = theSumMap.find(id);
00139   if(itr == theSumMap.end()) {
00140     theSumMap.insert(std::make_pair(id, samples));
00141   } else {
00142     // wish CaloSamples had a +=
00143     for(int i = 0; i < samples.size(); ++i) {
00144       (itr->second)[i] += samples[i];   
00145     }
00146   }
00147 }
00148 
00149 
00150 void HcalTriggerPrimitiveAlgo::analyze(IntegerCaloSamples & samples, 
00151                                        HcalTriggerPrimitiveDigi & result)
00152 {
00153 
00154   //cout<<"TPG calc"
00155   // HcalTrigTowerDetId detId(samples.id());
00156   //find output samples length and new presamples
00157   int shrink = weights_.size()-1; //REAL LINE
00158   int outlength=samples.size() - shrink;
00159   int newprelength = ((samples.presamples()+1)-weights_.size())+latency_;
00160   std::vector<bool> finegrain(outlength,false);
00161   IntegerCaloSamples sum(samples.id(), outlength);
00162   
00163   bool SOI_pegged =  false;
00164   //Test is SOI input is pegged before summing
00165   if(samples[samples.presamples()]> 0x3FF) SOI_pegged = true;
00166   bool highvalue = false;
00167   //slide algo window
00168   for(int ibin = 0; ibin < int(samples.size())- shrink; ++ibin)
00169     {
00170       if(samples[ibin+1]>10){highvalue = true;}
00171       int algosumvalue = 0;
00172       for(unsigned int i = 0; i < weights_.size(); i++) {
00173         algosumvalue += int(samples[ibin+i] * weights_[i]);
00174       }//add up value * scale factor
00175       if (algosumvalue<0) sum[ibin]=0; // low-side
00176       else if (algosumvalue>0x3FF) sum[ibin]=0x3FF;  //high-side
00177       else sum[ibin] = algosumvalue;//assign value to sum[]
00178     }
00179 
00180   if(highvalue)
00181     {
00182       // cout<<"ICS ";
00183       for(int ibin = 0; ibin < int(samples.size())- shrink; ++ibin)
00184         {
00185           //  cout<<samples[ibin+1]<<" ";
00186         }
00187       // cout<<endl;
00188     }
00189   //Do peak finding if requested
00190 
00191 
00192   if(peakfind_)
00193     {
00194       IntegerCaloSamples output(samples.id(),outlength-2);
00195       output.setPresamples(newprelength-1);
00196       for(int ibin2 = 1; ibin2 < (sum.size())-1; ++ibin2) 
00197         {
00198           //use if peak finding true
00199           //Old equalities
00200           // if ( sum[ibin2] > sum[ibin2-1] && 
00201           //    sum[ibin2] >= sum[ibin2+1] && 
00202           //    sum[ibin2] > theThreshold)
00203           if ( sum[ibin2] >= sum[ibin2-1] && 
00204                       sum[ibin2] > sum[ibin2+1] && 
00205                sum[ibin2] > theThreshold)
00206             {
00207               output[ibin2-1]=sum[ibin2];//if peak found
00208             }
00209           else{output[ibin2-1]=0;}//if no peak
00210         }
00211       if(SOI_pegged == true)
00212         {
00213           output[output.presamples()] = 0x3FF;
00214         }
00215       outcoder_->compress(output, finegrain, result);//send to transcoder
00216       //      outcoder_->loadhcalUncompress();
00217     }
00218   
00219   else//No peak finding
00220     {
00221       IntegerCaloSamples output(samples.id(),outlength);
00222       output.setPresamples(newprelength);
00223       for(int ibin2 = 0; ibin2<sum.size(); ++ibin2) 
00224         {
00225           output[ibin2]=sum[ibin2];//just pass value
00226         }
00227       outcoder_->compress(output, finegrain, result);
00228     }   
00229 }
00230 
00231 
00232 void HcalTriggerPrimitiveAlgo::analyzeHF(IntegerCaloSamples & samples, 
00233                                          HcalTriggerPrimitiveDigi & result)
00234 {
00235   std::vector<bool> finegrain(samples.size(),false);
00236   // IntegerCaloSamples sum(samples.id(), samples.size());
00237   HcalTrigTowerDetId detId_(samples.id()); 
00238    
00239   // get information from Tower map
00240   for(TowerMapFG::iterator mapItr = theTowerMapFG.begin(); mapItr != theTowerMapFG.end(); ++mapItr)
00241     {
00242       bool set_fg = false;
00243       HcalTrigTowerDetId detId(mapItr->first);
00244       if (detId == detId_) {
00245         for (int i=0; i < samples.size(); ++i) {
00246           mapItr->second[i] >= FG_threshold_ ? set_fg = true : false;
00247           finegrain[i] = (finegrain[i] || set_fg);
00248         }
00249       }
00250     }  
00251   IntegerCaloSamples output(samples.id(),samples.size());
00252   output.setPresamples(samples.presamples());
00253   //cout<<"Presamples = "<<samples.presamples()<<endl;
00254   for(int ibin2 = 0; ibin2 < samples.size(); ++ibin2) 
00255     {//output[ibin2]=sum[ibin2];
00256       samples[ibin2] /= 8;
00257       //cout << "samples: " << samples[i] << endl;
00258       if (samples[ibin2] > 0x3FF) samples[ibin2] = 0x3FF;  //Compression is 1 to 1 with saturation at 8 bits
00259       output[ibin2]=samples[ibin2];
00260     }
00261   //cout<<endl;
00262   outcoder_->compress(output, finegrain, result);
00263 }

Generated on Tue Jun 9 17:46:26 2009 for CMSSW by  doxygen 1.5.4