CMS 3D CMS Logo

TkAlCaSkimTreeMerger.cc
Go to the documentation of this file.
1 #include <string>
2 #include <fstream>
3 #include <map>
4 #include <iostream>
5 
16 
17 #include "TFile.h"
18 #include "TString.h"
19 #include "TChain.h"
20 #include "TStopwatch.h"
21 
23 public:
25  ~TkAlCaSkimTreeMerger() override;
26  void beginJob() override;
27  void endJob() override;
28  void analyze(const edm::Event &, const edm::EventSetup &) override;
30 
31 private:
32  TTree *out_; //TTree containing the merged result
33  TTree *firsttree_; //first tree of the list; this gives the structure to all the others
34  TChain *ch_; //chain containing all the tree you want to merge
35  std::string filelist_; //text file containing the list of input files
37  std::string treename_; //name of the tree you want to merge (contained in the file)
38  std::string outfilename_; //name of the file where you want to save the output
39 
40  //Hit Population
41  typedef std::map<uint32_t, uint32_t> DetHitMap;
44  int maxhits_; //above this number, the hit population is prescaled. Configurable for each subdet
47 
48  TStopwatch myclock;
49 };
50 
52  : filelist_(iConfig.getParameter<std::string>("FileList")),
53  treename_(iConfig.getParameter<std::string>("TreeName")),
54  outfilename_(iConfig.getParameter<std::string>("OutputFile")),
55  maxhits_(iConfig.getParameter<int32_t>("NhitsMaxLimit")),
56  maxhitsSet_(iConfig.getParameter<edm::ParameterSet>("NhitsMaxSet")) {
57  maxPXBhits_ = maxhitsSet_.getParameter<int32_t>("PXBmaxhits");
58  maxPXFhits_ = maxhitsSet_.getParameter<int32_t>("PXFmaxhits");
59  maxTIBhits_ = maxhitsSet_.getParameter<int32_t>("TIBmaxhits");
60  maxTIDhits_ = maxhitsSet_.getParameter<int32_t>("TIDmaxhits");
61  maxTOBhits_ = maxhitsSet_.getParameter<int32_t>("TOBmaxhits");
62  maxTEChits_ = maxhitsSet_.getParameter<int32_t>("TECmaxhits");
63  //anything you want to do for initializing
64  LogDebug("TkAlCaSkimTreeMerger") << "\n\n*** MAX N HITS = " << maxhits_ << std::endl << std::endl;
65  out_ = nullptr;
66  firsttree_ = nullptr;
67  ch_ = nullptr;
68 }
69 
71  delete ch_;
72  LogDebug("TkAlCaSkimTreeMerger") << "finished." << std::endl;
73 }
74 
75 // ------------ method called before analyzing the first event ------------
77  myclock.Start();
78 
79  //prepare the chain
80  ch_ = new TChain(treename_.c_str());
81  LogDebug("TkAlCaSkimTreeMerger") << "The chain contains " << ch_->GetNtrees() << " trees" << std::endl;
82 
83  //load the trees into the chain
84  std::ifstream flist(filelist_.c_str(), std::ios::in);
86  std::string firstfilename;
87  bool first = true;
88  while (!flist.eof()) {
89  filename = "";
90  flist >> filename;
91  if (filename.empty())
92  continue;
93  LogDebug("TkAlCaSkimTreeMerger") << "Adding " << filename << std::endl;
94  ch_->Add(filename.c_str());
95  if (first) {
97  first = false;
98  }
99  }
100  LogDebug("TkAlCaSkimTreeMerger") << "Now the chain contains " << ch_->GetNtrees() << " trees (" << ch_->GetEntries()
101  << " entries)" << std::endl;
102 
103  unsigned int id_ch = 0;
104  uint32_t nhits_ch = 0, noverlaps_ch = 0;
105  ch_->SetBranchAddress("DetId", &id_ch);
106  ch_->SetBranchAddress("Nhits", &nhits_ch);
107  ch_->SetBranchAddress("Noverlaps", &noverlaps_ch);
108 
109  ch_->SetBranchStatus("SubDet", false);
110  ch_->SetBranchStatus("Layer", false);
111  ch_->SetBranchStatus("is2D", false);
112  ch_->SetBranchStatus("isStereo", false);
113  ch_->SetBranchStatus("posX", false);
114  ch_->SetBranchStatus("posY", false);
115  ch_->SetBranchStatus("posZ", false);
116  ch_->SetBranchStatus("posR", false);
117  ch_->SetBranchStatus("posEta", false);
118  ch_->SetBranchStatus("posPhi", false); //now only id, nhits and noverlaps are on...
119 
120  int totnhits(0), totnoverlaps(0);
121 
122  //look if you find this detid in the map
123  DetHitMap::iterator mapiter;
124  DetHitMap::iterator overlapiter;
125 
126  for (int ent = 0; ent < ch_->GetEntries(); ++ent) {
127  // for(int ent=0;ent<100;++ent){
128  ch_->GetEntry(ent);
129  totnhits += nhits_ch;
130  totnoverlaps += noverlaps_ch;
131 
132  mapiter = hitmap_.find(id_ch);
133  if (mapiter != hitmap_.end()) { //present, increase its value
134  hitmap_[id_ch] = hitmap_[id_ch] + nhits_ch;
135  } else { //not present, let's add this key to the map with value=1
136  hitmap_.insert(std::pair<uint32_t, uint32_t>(id_ch, nhits_ch));
137  }
138  //do the same for overlaps
139  overlapiter = overlapmap_.find(id_ch);
140  if (overlapiter != overlapmap_.end()) {
141  overlapmap_[id_ch] = overlapmap_[id_ch] + noverlaps_ch;
142  } else {
143  overlapmap_.insert(std::pair<uint32_t, uint32_t>(id_ch, noverlaps_ch));
144  }
145 
146  } //end loop on ent - entries in the chain
147 
148  LogDebug("TkAlCaSkimTreeMerger") << "Nhits in the chain: " << totnhits << std::endl;
149  LogDebug("TkAlCaSkimTreeMerger") << "NOverlaps in the chain: " << totnoverlaps << std::endl;
150 
151  myclock.Stop();
152  LogDebug("TkAlCaSkimTreeMerger") << "Finished beginJob after " << myclock.RealTime() << " s (real time) / "
153  << myclock.CpuTime() << " s (cpu time)" << std::endl;
154  myclock.Continue();
155 } //end beginJob
156 
157 // ------------ method called to for each event ------------
159  LogDebug("TkAlCaSkimTreeMerger") << firsttree_->GetEntries() << std::endl;
160 } //end analyze
161 
162 // ------------ method called after having analyzed all the events ------------
164  //address variables in the first tree and in the chain
165  TFile *firstfile = new TFile(firstfilename_.c_str(), "READ");
166  firsttree_ = (TTree *)firstfile->Get(treename_.c_str());
167  LogDebug("TkAlCaSkimTreeMerger") << "the first tree has " << firsttree_->GetEntries() << " entries" << std::endl;
168  unsigned int id = 0;
169  uint32_t nhits = 0, noverlaps = 0;
170  float posX(-99999.0), posY(-77777.0), posZ(-88888.0);
171  float posEta(-6666.0), posPhi(-5555.0), posR(-4444.0);
172  int subdet = 0;
173  unsigned int layer = 0;
174  // bool is2D=false,isStereo=false;
175  firsttree_->SetBranchAddress("DetId", &id);
176  firsttree_->SetBranchAddress("Nhits", &nhits);
177  firsttree_->SetBranchAddress("Noverlaps", &noverlaps);
178  firsttree_->SetBranchAddress("SubDet", &subdet);
179  firsttree_->SetBranchAddress("Layer", &layer);
180  // firsttree_->SetBranchAddress("is2D" , &is2D);
181  // firsttree_->SetBranchAddress("isStereo", &isStereo);
182  firsttree_->SetBranchAddress("posX", &posX);
183  firsttree_->SetBranchAddress("posY", &posY);
184  firsttree_->SetBranchAddress("posZ", &posZ);
185  firsttree_->SetBranchAddress("posR", &posR);
186  firsttree_->SetBranchAddress("posEta", &posEta);
187  firsttree_->SetBranchAddress("posPhi", &posPhi);
188 
189  //create and book the output
190 
191  TFile *outfile = new TFile(outfilename_.c_str(), "RECREATE");
192  out_ = new TTree(treename_.c_str(), "AlignmentHitMapsTOTAL");
193  unsigned int id_out = 0;
194  uint32_t nhits_out = 0, noverlaps_out = 0;
195  float posX_out(-99999.0), posY_out(-77777.0), posZ_out(-88888.0);
196  float posEta_out(-6666.0), posPhi_out(-5555.0), posR_out(-4444.0);
197  int subdet_out = 0;
198  unsigned int layer_out = 0;
199  bool is2D_out = false, isStereo_out = false;
200  float prescfact_out = 1.0;
201  float prescfact_overlap_out = 1.0;
202 
203  out_->Branch("DetId", &id_out, "DetId/i");
204  out_->Branch("Nhits", &nhits_out, "Nhits/i");
205  out_->Branch("Noverlaps", &noverlaps_out, "Noverlaps/i");
206  out_->Branch("SubDet", &subdet_out, "SubDet/I");
207  out_->Branch("Layer", &layer_out, "Layer/i");
208  out_->Branch("is2D", &is2D_out, "is2D/B");
209  out_->Branch("isStereo", &isStereo_out, "isStereo/B");
210  out_->Branch("posX", &posX_out, "posX/F");
211  out_->Branch("posY", &posY_out, "posY/F");
212  out_->Branch("posZ", &posZ_out, "posZ/F");
213  out_->Branch("posR", &posR_out, "posR/F");
214  out_->Branch("posEta", &posEta_out, "posEta/F");
215  out_->Branch("posPhi", &posPhi_out, "posPhi/F");
216  out_->Branch("PrescaleFactor", &prescfact_out, "PrescaleFact/F");
217  out_->Branch("PrescaleFactorOverlap", &prescfact_overlap_out, "PrescaleFactOverlap/F");
218 
219  //look if you find this detid in the map
220  DetHitMap::iterator mapiter;
221 
222  for (int mod = 0; mod < firsttree_->GetEntries(); mod++) {
223  //for(int mod=0;mod<100;++mod){
224  // nhits_out=0;
225  // noverlaps_out=0;
226 
227  firsttree_->GetEntry(mod);
228  nhits_out = hitmap_[id];
229  noverlaps_out = overlapmap_[id];
230  // if(mod<25)LogDebug("TkAlCaSkimTreeMerger")<<"Nhits 1st tree: "<<nhits<<"\tTotal nhits chain: "<<nhits_out<<std::endl;
231  id_out = id;
232  subdet_out = subdet;
233  layer_out = layer;
234  posX_out = posX;
235  posY_out = posY;
236  posZ_out = posZ;
237  posR_out = posR;
238  posEta_out = posEta;
239  posPhi_out = posPhi;
240 
241  //calculate prescaling factor
242  int subdetmax = -1;
243  if (subdet_out == 1)
244  subdetmax = maxPXBhits_;
245  else if (subdet_out == 2)
246  subdetmax = maxPXFhits_;
247  else if (subdet_out == 3)
248  subdetmax = maxTIBhits_;
249  else if (subdet_out == 4)
250  subdetmax = maxTIDhits_;
251  else if (subdet_out == 5)
252  subdetmax = maxTOBhits_;
253  else if (subdet_out == 6)
254  subdetmax = maxTEChits_;
255  else
256  subdetmax = -9999;
257 
258  if (maxhits_ > -1) {
259  if (int(nhits_out) > maxhits_) {
260  prescfact_out = float(maxhits_) / float(nhits_out);
261  }
262  if (int(noverlaps_out) > maxhits_) {
263  prescfact_overlap_out = float(maxhits_) / float(noverlaps_out);
264  }
265  } else if (subdetmax > 0) { //calculate different prescaling factors for each subdet
266  if (int(nhits_out) > subdetmax) {
267  prescfact_out = float(subdetmax / nhits_out);
268  }
269  if (int(noverlaps_out) > subdetmax) {
270  prescfact_overlap_out = float(subdetmax) / float(noverlaps_out);
271  }
272  } else {
273  prescfact_out = 1.0;
274  prescfact_overlap_out = 1.0;
275  }
276  out_->Fill();
277 
278  } //end loop on mod - first tree modules
279 
280  myclock.Stop();
281  LogDebug("TkAlCaSkimTreeMerger") << "Finished endJob after " << myclock.RealTime() << " s (real time) / "
282  << myclock.CpuTime() << " s (cpu time)" << std::endl;
283  LogDebug("TkAlCaSkimTreeMerger") << "Ending the tree merging." << std::endl;
284  out_->Write();
285  LogDebug("TkAlCaSkimTreeMerger") << "Deleting..." << std::flush;
286  delete firstfile;
287  delete outfile;
288 } //end endJob
289 
292  desc.setComment("Merger of TkAlCaSkim Trees");
293  desc.add<std::string>("FileList", "DQMHitMapsList.txt");
294  desc.add<std::string>("TreeName", "AlignmentHitMaps");
295  desc.add<std::string>("OutputFile", "AlignmentHitMapsMerged.root");
296  desc.add<int>("NhitsMaxLimit", 0);
297 
298  edm::ParameterSetDescription tkAlCaSkimTreeParamsDesc;
299  std::vector<edm::ParameterSet> tkAlCaSkimDefaults(1);
300  tkAlCaSkimDefaults[0].addParameter("PXBmaxhits", -1);
301  tkAlCaSkimDefaults[0].addParameter("PXFmaxhits", -1);
302  tkAlCaSkimDefaults[0].addParameter("TIBmaxhits", -1);
303  tkAlCaSkimDefaults[0].addParameter("TIDmaxhits", -1);
304  tkAlCaSkimDefaults[0].addParameter("TOBmaxhits", -1);
305  tkAlCaSkimDefaults[0].addParameter("TECmaxhits", -1);
306  desc.addVPSet("NhitsMaxSet", tkAlCaSkimTreeParamsDesc, tkAlCaSkimDefaults);
307  descriptions.addWithDefaultLabel(desc);
308 }
309 
310 // ========= MODULE DEF ==============
void analyze(const edm::Event &, const edm::EventSetup &) override
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
TkAlCaSkimTreeMerger(const edm::ParameterSet &iConfig)
constexpr std::array< uint8_t, layerIndexSize > layer
static void fillDescriptions(edm::ConfigurationDescriptions &)
edm::ParameterSet maxhitsSet_
std::map< uint32_t, uint32_t > DetHitMap
HLT enums.
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
#define LogDebug(id)