FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairMultiLinkedData.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 /*
9  * FairMultiLinkedData.cxx
10  *
11  * Created on: Dec 7, 2009
12  * Author: stockman
13  */
14 
15 #include "FairMultiLinkedData.h"
16 
17 #include "FairLinkManager.h" // for FairLinkManager
19 #include "FairRootManager.h" // for FairRootManager
20 
21 #include <TClonesArray.h> // for TClonesArray
22 #include <algorithm> // for find
23 #include <iterator> // for distance
24 
26 
28  : TObject()
29  , fLinks()
30  , fEntryNr()
31  , fPersistanceCheck(kTRUE)
32  , fInsertHistory(kTRUE)
33  , fVerbose(0)
34  , fDefaultType(0)
35 {}
36 
37 FairMultiLinkedData::FairMultiLinkedData(const std::set<FairLink>& links, Bool_t persistanceCheck)
38  : TObject()
39  , fLinks(links)
40  , fEntryNr()
41  , fPersistanceCheck(persistanceCheck)
42  , fInsertHistory(kTRUE)
43  , fVerbose(0)
44  , fDefaultType(0)
45 {}
46 
48  const std::vector<Int_t>& links,
49  Int_t fileId,
50  Int_t evtId,
51  Bool_t persistanceCheck,
52  Bool_t bypass,
53  Float_t mult)
54  : TObject()
55  , fLinks()
56  , fEntryNr()
57  , fPersistanceCheck(persistanceCheck)
58  , fInsertHistory(kTRUE)
59  , fVerbose(0)
60  , fDefaultType(0)
61 {
62  SimpleAddLinks(fileId, evtId, FairRootManager::Instance()->GetBranchId(dataType), links, bypass, mult);
63  // FairMultiLinkedData(ioman->GetBranchId(dataType), links, persistanceCheck, bypass, addup, mult);
64 }
65 
67  const std::vector<Int_t>& links,
68  Int_t fileId,
69  Int_t evtId,
70  Bool_t persistanceCheck,
71  Bool_t bypass,
72  Float_t mult)
73  : TObject()
74  , fLinks()
75  , fEntryNr()
76  , fPersistanceCheck(persistanceCheck)
77  , fInsertHistory(kTRUE)
78  , fVerbose(0)
79  , fDefaultType(0)
80 {
81  SimpleAddLinks(fileId, evtId, dataType, links, bypass, mult);
82 }
83 
85 {
86  if (pos < static_cast<Int_t>(fLinks.size())) {
87  // return value at iterator+pos
88  return *(std::next(fLinks.begin(), pos));
89  } else {
90  std::cout << "-E- FairMultiLinkedData:GetLink(pos) pos " << pos << " outside range " << fLinks.size()
91  << std::endl;
92  return FairLink();
93  }
94 }
95 
97 {
98  fLinks.clear();
99  AddLinks(links, mult);
100 }
101 
102 void FairMultiLinkedData::SetLink(FairLink link, Bool_t bypass, Float_t mult)
103 {
104  fLinks.clear();
105  Float_t weight = link.GetWeight() * mult;
106  link.SetWeight(weight);
107  AddLink(link, bypass);
108 }
109 
111 {
112  std::set<FairLink> myLinks = links.GetLinks();
113  for (auto link : myLinks) {
114  link.SetWeight(link.GetWeight() * mult);
115  AddLink(link);
116  }
117 }
118 
119 void FairMultiLinkedData::AddLink(FairLink link, Bool_t bypass, Float_t mult)
120 {
121  Float_t weight = link.GetWeight() * mult;
122  link.SetWeight(weight);
123  // std::cout << fVerbose << std::endl;
124 
126 
127  if (ioman == 0) {
128  std::cout << "-W- no IOManager present!" << std::endl;
129  fPersistanceCheck = kFALSE;
130  }
131  if (fVerbose > 1) {
132  std::cout << "Add FairLink: " << link << std::endl;
133  }
134 
135  if (fPersistanceCheck == kFALSE || link.GetIndex() < 0
136  || (link.GetType() != ioman->GetMCTrackBranchId()
137  && ioman->CheckBranch(ioman->GetBranchName(link.GetType())) == 0)) {
138  InsertLink(link);
139  if (fInsertHistory == kTRUE)
140  InsertHistory(link);
141  return;
142  }
143 
144  if (bypass == kFALSE) {
145  if (fVerbose > 1) {
146  std::cout << "BranchName " << ioman->GetBranchName(link.GetType())
147  << " checkStatus: " << ioman->CheckBranch(ioman->GetBranchName(link.GetType())) << std::endl;
148  }
149  if (link.GetType() > ioman->GetMCTrackBranchId()
150  && ioman->CheckBranch(ioman->GetBranchName(link.GetType())) != 1) {
151  if (fVerbose > 1) {
152  std::cout << "BYPASS!" << std::endl;
153  }
154  // bypass = kTRUE;
155  }
156  }
157 
158  if (bypass == kTRUE) {
159  // FairRootManager* ioman = FairRootManager::Instance();
160  if (link.GetType() > ioman->GetMCTrackBranchId()) {
161  TClonesArray* array = static_cast<TClonesArray*>(ioman->GetObject(ioman->GetBranchName(link.GetType())));
162  if (fVerbose > 1) {
163  std::cout << "Entries in " << ioman->GetBranchName(link.GetType()) << " Array: " << array->GetEntries()
164  << std::endl;
165  }
166  FairMultiLinkedData* links = static_cast<FairMultiLinkedData*>(array->At(link.GetIndex()));
167  if (fVerbose > 1) {
168  std::cout << "FairMultiLinkedData has " << links->GetNLinks() << " Entries: " << std::endl;
169  std::cout << *links << std::endl;
170  }
171  AddLinks(*links, mult);
172  return;
173  } else {
174  InsertLink(link);
175  if (fInsertHistory == kTRUE)
176  InsertHistory(link);
177  }
178  } else {
179  InsertLink(link);
180  if (fInsertHistory == kTRUE)
181  InsertHistory(link);
182  }
183 }
184 
186 {
187  if (FairLinkManager::Instance()->IsIgnoreType(link.GetType())) {
188  return;
189  }
190 
191  std::set<FairLink>::iterator it = fLinks.find(link);
192  if (it != fLinks.end()) {
193  FairLink myTempLink = *it;
194  myTempLink.AddWeight(link.GetWeight());
195  fLinks.erase(it);
196  fLinks.insert(myTempLink);
197  } else {
198  fLinks.insert(link);
199  }
200  return;
201 }
202 
204 {
206  FairMultiLinkedData* pointerToLinks = 0;
207 
208  if (fVerbose > 1) {
209  std::cout << "FairMultiLinkedData::InsertHistory for Link " << link
210  << " Type: " << ioman->GetBranchName(link.GetType()) << std::endl;
211  }
212 
213  if (link.GetType() < 0)
214  return;
215  if (link.GetType() == ioman->GetMCTrackBranchId())
216  return;
217  if (ioman->GetBranchName(link.GetType()).Contains("."))
218  return;
219  if (link.GetEntry() != -1 && link.GetEntry() != ioman->GetEntryNr())
220  return;
221 
222  if (link.GetIndex() < 0) { // if index is -1 then this is not a TClonesArray so only the Object is returned
223  TObject* myObject = ioman->GetObject(ioman->GetBranchName(link.GetType()));
224  if (myObject->InheritsFrom("FairMultiLinkedData_Interface")) {
225  FairMultiLinkedData_Interface* interface = static_cast<FairMultiLinkedData_Interface*>(myObject);
226  pointerToLinks = interface->GetPointerToLinks();
227  } else {
228  std::cout << "FairMultiLinkedData::InsertHistory Link to wrong Class: "
229  << ioman->GetBranchName(link.GetType()) << std::endl;
230  return;
231  }
232  } else {
233  TClonesArray* dataArray = static_cast<TClonesArray*>(ioman->GetObject(ioman->GetBranchName(link.GetType())));
234  if (dataArray != 0 && link.GetIndex() < dataArray->GetEntriesFast()) {
235  TObject* myObject = dataArray->At(link.GetIndex());
236  if (myObject->InheritsFrom("FairMultiLinkedData_Interface")) {
237  FairMultiLinkedData_Interface* interface = static_cast<FairMultiLinkedData_Interface*>(myObject);
238  pointerToLinks = interface->GetPointerToLinks();
239  } else {
240  std::cout << "FairMultiLinkedData::InsertHistory Link to wrong Class: "
241  << ioman->GetBranchName(link.GetType()) << std::endl;
242  return;
243  }
244  }
245  }
246  if (pointerToLinks != 0) {
247  std::set<FairLink> linkSet = pointerToLinks->GetLinks();
248  for (auto tlink : linkSet) {
249  if (fVerbose > 1)
250  std::cout << "FairMultiLinkedData::InsertHistory inserting " << tlink << std::endl;
251  InsertLink(tlink);
252  }
253  }
254 }
255 
256 Bool_t FairMultiLinkedData::IsLinkInList(Int_t type, Int_t index)
257 {
258  if (LinkPosInList(type, index) > -1) {
259  return kTRUE;
260  }
261  return kFALSE;
262 }
263 
264 Int_t FairMultiLinkedData::LinkPosInList(Int_t type, Int_t index)
265 {
266  std::set<FairLink>::iterator it = std::find(fLinks.begin(), fLinks.end(), FairLink(type, index));
267  if (it != fLinks.end()) {
268  return std::distance(fLinks.begin(), it);
269  }
270  return -1;
271 }
272 
273 void FairMultiLinkedData::DeleteLink(Int_t /*type*/, Int_t /*index*/)
274 {
275  /* Int_t pos = LinkPosInList(type, index);
276  if (pos < 0) return;
277  else{
278  fLinks.erase(fLinks.begin()+pos);
279  }*/
280 }
281 
283 {
284  FairMultiLinkedData result;
285  for (auto link : fLinks) {
286  if (link.GetType() == type) {
287  result.InsertLink(link);
288  }
289  }
290  return result;
291 }
292 
293 bool LargerWeight(FairLink val1, FairLink val2) { return val1.GetWeight() > val2.GetWeight(); }
294 
296 {
297  FairMultiLinkedData mcLinks = GetLinksWithType(FairRootManager::Instance()->GetMCTrackBranchId());
298  std::set<FairLink> mcSet = mcLinks.GetLinks();
299  std::vector<FairLink> mcVector(mcSet.begin(), mcSet.end());
300  // std::sort(begin(mcVector), end(mcVector), [](FairLink& val1, FairLink& val2){ return val1.GetWeight() >
301  // val2.GetWeight();});
302  std::sort(begin(mcVector), end(mcVector), LargerWeight);
303  return mcVector;
304 }
305 
307 {
309  TString branchName = ioman->GetBranchName(myLink.GetType());
310  if (ioman->CheckBranch(branchName) > 0) {
311  TClonesArray* myArray = static_cast<TClonesArray*>(ioman->GetObject(branchName));
312  if (myArray != 0) {
313  if (myArray->GetEntries() > myLink.GetIndex()) {
314  return myArray->At(myLink.GetIndex());
315  } else {
316  std::cout << "-E- FairMultiLinkedData::GetData index out of bounds" << std::endl;
317  }
318  } else {
319  std::cout << "-E- FairMultiLinkedData::GetData no TClonesArray" << std::endl;
320  }
321  } else {
322  std::cout << "-E- FairMultiLinkedData::GetData Branch does not exist in Memory" << std::endl;
323  }
324 
325  return 0;
326 }
327 
329 {
330  std::set<FairLink> tempLinks;
331  for (auto link : fLinks) {
332  link.SetWeight(weight);
333  tempLinks.insert(link);
334  }
335  fLinks = tempLinks;
336 }
337 
339 {
340  std::set<FairLink> tempLinks;
341  for (auto link : fLinks) {
342  link.SetWeight(weight + link.GetWeight());
343  tempLinks.insert(link);
344  }
345  fLinks = tempLinks;
346 }
347 
349 {
350  std::set<FairLink> tempLinks;
351  for (auto link : fLinks) {
352  link.SetWeight(weight * link.GetWeight());
353  tempLinks.insert(link);
354  }
355  fLinks = tempLinks;
356 }
virtual void SimpleAddLinks(Int_t fileId, Int_t evtId, Int_t dataType, const std::vector< Int_t > &links, Bool_t, Float_t)
bool LargerWeight(FairLink val1, FairLink val2)
virtual Bool_t IsLinkInList(FairLink link)
Test if a given link is in fLinks.
TObject * GetData(FairLink &myLink)
Get the TObject the Link is pointing to.
virtual void AddAllWeights(Double_t weight)
Adds weight to all Links.
virtual std::vector< FairLink > GetSortedMCTracks()
Gives you a list of all FairLinks pointing to a &quot;MCTrack&quot; sorted by their weight. ...
virtual void SetLinks(FairMultiLinkedData links, Float_t mult=1.0)
Sets the links as vector of FairLink.
std::set< FairLink > fLinks
static FairRootManager * Instance()
ClassImp(FairEventBuilder)
virtual Int_t LinkPosInList(FairLink link)
returns position of link in fLinks. If it is not in the list -1 is returned
virtual void SetAllWeights(Double_t weight)
Sets a common weight for Links.
TObject * GetObject(const char *BrName)
virtual void MultiplyAllWeights(Double_t weight)
Multiplies all Links with weight.
virtual void InsertHistory(FairLink link)
Adds the FairLinks of the inserted link to the set of links of this object.
virtual void InsertLink(FairLink link)
Inserts a link into the list of links without persistance checking.
static FairLinkManager * Instance()
Int_t GetMCTrackBranchId() const
virtual void AddLinks(FairMultiLinkedData links, Float_t mult=1.0)
Adds a List of FairLinks (FairMultiLinkedData) to fLinks.
FairMultiLinkedData()
Default constructor.
virtual void SetLink(FairLink link, Bool_t bypass=kFALSE, Float_t mult=1.0)
Sets the Links with a single FairLink.
virtual void DeleteLink(FairLink link)
Deletes a link ouf of fLinks.
TString GetBranchName(Int_t id)
virtual Int_t GetNLinks() const
returns the number of stored links
virtual FairLink GetLink(Int_t pos) const
returns the FairLink at the given position
Int_t CheckBranch(const char *BrName)
virtual void AddLink(FairLink link, Bool_t bypass=kFALSE, Float_t mult=1.0)
virtual std::set< FairLink > GetLinks() const
returns stored links as FairLinks
virtual FairMultiLinkedData GetLinksWithType(Int_t type) const
Gives you a list of links which contain the given type.