Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:27:41

0001 // -*- C++ -*-
0002 //
0003 // Package:    SampicDigi/DiamondSampicDigiProducer
0004 // Class:      DiamondSampicDigiProducer
0005 //
0006 /**\class DiamondSampicDigiProducer DiamondSampicDigiProducer.cc SampicDigi/DiamondSampicDigiProducer/plugins/DiamondSampicDigiProducer.cc
0007 
0008  Description: This plugin takes testbeam data as input and converts them to TotemTimingDigi which could be then passed to reco
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Christopher Misan (krzysztof.misan@cern.ch)
0015 //         Created:  Sun, 07 Mar 2021 14:42:52 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 #include <string>
0022 
0023 // user include files
0024 #include "FWCore/Framework/interface/Frameworkfwd.h"
0025 #include "FWCore/Framework/interface/stream/EDProducer.h"
0026 
0027 #include "FWCore/Framework/interface/Event.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029 
0030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0031 #include "FWCore/Utilities/interface/StreamID.h"
0032 
0033 #include "DataFormats/CTPPSDigi/interface/TotemTimingDigi.h"
0034 #include "DataFormats/CTPPSDetId/interface/TotemTimingDetId.h"
0035 #include "DataFormats/Common/interface/DetSetVector.h"
0036 #include "DataFormats/CTPPSDetId/interface/CTPPSDiamondDetId.h"
0037 
0038 #include "TFile.h"
0039 #include "TTree.h"
0040 #include "TChain.h"
0041 
0042 //
0043 // class declaration
0044 //
0045 
0046 class DiamondSampicDigiProducer : public edm::stream::EDProducer<> {
0047 public:
0048   explicit DiamondSampicDigiProducer(const edm::ParameterSet&);
0049 
0050   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0051 
0052 private:
0053   void produce(edm::Event&, const edm::EventSetup&) override;
0054 
0055   // ----------member data ---------------------------
0056   std::vector<std::string> sampicFilesVec_;
0057   std::unordered_map<unsigned int, std::vector<unsigned int>> detid_vs_chid_;
0058   std::unique_ptr<TChain> inputTree_;
0059 
0060   //constants
0061   const int kMaxSampicChannels = 32;
0062   const int kSampicSamples = 64;
0063   const int kLhcClkPeriod = 25;
0064   const int kMaxBunchNumber = 3564;
0065   const double kLhcOrbitPeriodNs = 88924.45;
0066   const double kSampicOffset = 1.2;
0067 };
0068 
0069 //
0070 // constructors and destructor
0071 //
0072 
0073 using namespace edm;
0074 using namespace std;
0075 
0076 DiamondSampicDigiProducer::DiamondSampicDigiProducer(const edm::ParameterSet& iConfig)
0077     : sampicFilesVec_(iConfig.getParameter<std::vector<std::string>>("sampicFilesVec")) {
0078   for (const auto& id_map : iConfig.getParameter<std::vector<edm::ParameterSet>>("idsMapping"))
0079     detid_vs_chid_[id_map.getParameter<unsigned int>("treeChId")] = id_map.getParameter<vector<unsigned int>>("detId");
0080 
0081   inputTree_ = std::make_unique<TChain>("desy");
0082   for (const auto& fname : sampicFilesVec_)
0083     inputTree_->Add(fname.c_str());
0084 
0085   produces<DetSetVector<TotemTimingDigi>>("TotemTiming");
0086 }
0087 
0088 //
0089 // member functions
0090 //
0091 
0092 void DiamondSampicDigiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0093   int eventNum = iEvent.id().event();
0094   auto digi = std::make_unique<edm::DetSetVector<TotemTimingDigi>>();
0095 
0096   uint num_samples;
0097   double trigger_time;
0098   int sample_channel[kMaxSampicChannels];
0099   int sample_cellInfoForOrderedData[kMaxSampicChannels];
0100   unsigned int sample_timestampA[kMaxSampicChannels];
0101   unsigned int sample_timestampB[kMaxSampicChannels];
0102   unsigned long long sample_timestampFPGA[kMaxSampicChannels];
0103   double sample_amp[kMaxSampicChannels][kSampicSamples];
0104   double sample_triggerPosition[kMaxSampicChannels][kSampicSamples];
0105 
0106   inputTree_->SetBranchAddress("num_samples", &num_samples);
0107   inputTree_->SetBranchAddress("trigger_time", &trigger_time);
0108   inputTree_->SetBranchAddress("sample_channel", sample_channel);
0109   inputTree_->SetBranchAddress("sample_timestampFPGA", sample_timestampFPGA);
0110   inputTree_->SetBranchAddress("sample_timestampA", sample_timestampA);
0111   inputTree_->SetBranchAddress("sample_timestampB", sample_timestampB);
0112   inputTree_->SetBranchAddress("sample_cellInfoForOrderedData", sample_cellInfoForOrderedData);
0113   inputTree_->SetBranchAddress("sample_ampl", sample_amp);
0114   inputTree_->SetBranchAddress("sample_triggerPosition", sample_triggerPosition);
0115   inputTree_->GetEntry(eventNum);
0116 
0117   int bunchNumber = ((int)trigger_time / kLhcClkPeriod) % kMaxBunchNumber;
0118   int orbitNumber = (int)(trigger_time / kLhcOrbitPeriodNs);
0119 
0120   if (num_samples == 0) {
0121     iEvent.put(std::move(digi), "TotemTiming");
0122     return;
0123   }
0124 
0125   for (uint i = 0; i < num_samples; i++) {
0126     unsigned short ch_id = sample_channel[i];
0127 
0128     //for testbeam data channels<8 don't contain measurements
0129     if (ch_id < 8)
0130       continue;
0131 
0132     std::vector<uint8_t> samples;
0133     unsigned int offsetOfSamples = 0;
0134     bool search_for_white_cell = true;
0135 
0136     for (int y = 0; y < kSampicSamples; y++) {
0137       samples.push_back((int)(sample_amp[i][y] / kSampicOffset * 256));
0138       if (search_for_white_cell && sample_triggerPosition[i][y] == 1) {
0139         offsetOfSamples = y;
0140         search_for_white_cell = false;
0141       }
0142     }
0143 
0144     TotemTimingEventInfo eventInfoTmp(0,
0145                                       trigger_time * 1E9 / 10,  //l1ATimestamp
0146                                       bunchNumber,
0147                                       orbitNumber,
0148                                       eventNum,
0149                                       1,
0150                                       1220 / 10,  //l1ALatency
0151                                       64,
0152                                       offsetOfSamples,
0153                                       1);
0154 
0155     TotemTimingDigi digiTmp(2 * 32 + ch_id,
0156                             sample_timestampFPGA[i],
0157                             sample_timestampA[i],
0158                             sample_timestampB[i],
0159                             sample_cellInfoForOrderedData[i],
0160                             samples,
0161                             eventInfoTmp);
0162 
0163     auto vec = detid_vs_chid_.at(ch_id);
0164     for (const auto& id : vec) {
0165       CTPPSDiamondDetId detId(id);
0166       edm::DetSet<TotemTimingDigi>& digis_for_detid = digi->find_or_insert(detId);
0167       digis_for_detid.push_back(digiTmp);
0168     }
0169   }
0170   iEvent.put(std::move(digi), "TotemTiming");
0171 }
0172 
0173 void DiamondSampicDigiProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0174   edm::ParameterSetDescription desc;
0175   desc.add<vector<std::string>>("sampicFilesVec")->setComment("path to sampic root data");
0176 
0177   edm::ParameterSetDescription idmap_valid;
0178   idmap_valid.add<unsigned int>("treeChId", 0)->setComment("HW id as retrieved from tree");
0179   idmap_valid.add<vector<unsigned int>>("detId")->setComment("mapped CTPPSDiamondDetId's for this channel");
0180 
0181   desc.addVPSet("idsMapping", idmap_valid);
0182 
0183   descriptions.add("DiamondSampicDigiProducer", desc);
0184 }
0185 
0186 DEFINE_FWK_MODULE(DiamondSampicDigiProducer);