CMS 3D CMS Logo

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