Heads.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file Heads.cxx
3 /// \brief Mapping from a subset of the library to their best matches
4 /// \author Christopher Backhouse - bckhouse@caltech.edu
5 ////////////////////////////////////////////////////////////////////////
6 
7 #include "LEM/func/Heads.h"
8 
9 #include "LEM/func/OnDemand.h"
10 #include "LEM/func/Util.h"
11 
12 #include <cassert>
13 #include <sys/mman.h>
14 
15 namespace lem
16 {
17  // Private to this file
18  namespace{
19  char* kHeadsAddr = (char*)0x00000300CAFE0000;
20  }
21 
22  //......................................................................
23  Heads* Heads::FromMMap(const std::string& libPath, bool touchAll)
24  {
25  Heads* heads = new Heads;
26 
27  heads->fMappingSize = MMapFileAtAddress(libPath+"/heads_map.bin", kHeadsAddr,
28  touchAll, "/tmp/lem_preload_heads.lock");
29 
30  heads->fNHeads = ((int*)kHeadsAddr)[0];
31  heads->fHeadSeqsLen = ((int*)kHeadsAddr)[1];
32 
33  heads->fHeadIdxs = ((int*)kHeadsAddr)+2;
34 
35  heads->fHeadSeqs = new int*[heads->fNHeads];
36  heads->fHeadSeqs[0] = ((int*)kHeadsAddr)+2+heads->fNHeads;
37  for(long i = 0; i < heads->fNHeads; ++i){
38  heads->fHeadSeqs[i] = heads->fHeadSeqs[0]+heads->fHeadSeqsLen*i;
39 
40  // If "i" is an int, the calculation overflows. Check nothing similar is
41  // reintroduced.
42  assert((char*)heads->fHeadSeqs[i] > kHeadsAddr);
43  }
44 
45  return heads;
46  }
47 
48  //......................................................................
50  {
51  CreateOnDemandMapping((libPath+"/heads_map.bin").c_str(), kHeadsAddr);
52 
53  Heads* heads = new Heads;
54  heads->fMappingSize = 0;
55 
56  heads->fNHeads = ((int*)kHeadsAddr)[0];
57  heads->fHeadSeqsLen = ((int*)kHeadsAddr)[1];
58 
59  heads->fHeadIdxs = ((int*)kHeadsAddr)+2;
60 
61  heads->fHeadSeqs = new int*[heads->fNHeads];
62  heads->fHeadSeqs[0] = ((int*)kHeadsAddr)+2+heads->fNHeads;
63  for(int i = 0; i < heads->fNHeads; ++i){
64  heads->fHeadSeqs[i] = heads->fHeadSeqs[0]+heads->fHeadSeqsLen*i;
65  }
66 
67  return heads;
68  }
69 
70  //......................................................................
72  {
73  // munmap(kHeadsAddr, fMappingSize);
74  }
75 
76 
77  //......................................................................
78  int Heads::ChildIdxFor(int head, int child) const
79  {
80  // Strip out flip information
81  return fHeadSeqs[head][child]>>2;
82  }
83 
84  //......................................................................
85  bool Heads::FlipEvenFor(int head, int child) const
86  {
87  // Extract information in this bit
88  return fHeadSeqs[head][child] & 1<<1;
89  }
90 
91  //......................................................................
92  bool Heads::FlipOddFor(int head, int child) const
93  {
94  // Extract information in this bit
95  return fHeadSeqs[head][child] & 1<<0;
96  }
97 
98  //......................................................................
100  {
101  HeadsTranspose* heads = new HeadsTranspose;
102 
103  heads->fMappingSize = MMapFileAtAddress(libPath+"/heads_map_transpose.bin", kHeadsAddr, false);
104 
105  heads->fNHeads = ((int*)kHeadsAddr)[0];
106  heads->fLibSize = ((int*)kHeadsAddr)[1];
107 
108  heads->fHeadIdxs = ((int*)kHeadsAddr)+2;
109 
110  heads->fPSeqs = (int*)((char*)heads->fHeadIdxs+sizeof(int)*heads->fNHeads);
111 
112  return heads;
113  }
114 
115  //......................................................................
117  {
118  CreateOnDemandMapping((libPath+"/heads_map_transpose.bin").c_str(), kHeadsAddr);
119 
120  HeadsTranspose* heads = new HeadsTranspose;
121  heads->fMappingSize = 0;
122 
123  heads->fNHeads = ((int*)kHeadsAddr)[0];
124  heads->fLibSize = ((int*)kHeadsAddr)[1];
125 
126  heads->fHeadIdxs = ((int*)kHeadsAddr)+2;
127 
128  heads->fPSeqs = (int*)((char*)heads->fHeadIdxs+sizeof(int)*heads->fNHeads);
129 
130  return heads;
131  }
132 
133  //......................................................................
135  {
136  munmap(kHeadsAddr, fMappingSize);
137  }
138 
139  //......................................................................
140  std::ostream& operator<<(std::ostream& os, const lem::Heads& hs)
141  {
142  os << hs.fNHeads << " heads, with " << hs.fHeadSeqsLen << " events each.";
143  return os;
144  }
145 
146  //......................................................................
147  std::ostream& operator<<(std::ostream& os, const lem::HeadsTranspose& hs)
148  {
149  os << hs.fNHeads << " heads, for a librar of " << hs.fLibSize << " events.";
150  return os;
151  }
152 
153 } // namespace
static Heads * FromMMapOnDemand(const std::string &libPath)
Definition: Heads.cxx:49
long fMappingSize
Definition: Heads.h:71
int ChildIdxFor(int head, int child) const
Definition: Heads.cxx:78
bool FlipEvenFor(int head, int child) const
Definition: Heads.cxx:85
Mapping from a subset of the library to their best matches.
int ** fHeadSeqs
Definition: Heads.h:41
Heads()
Definition: Heads.h:34
Eigen::Matrix< T, Eigen::Dynamic, 1 > head(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v, size_t n)
Definition: head.hpp:24
static HeadsTranspose * FromMMapOnDemand(const std::string &libPath)
Definition: Heads.cxx:116
PID
Definition: FillPIDs.h:13
~Heads()
Definition: Heads.cxx:71
long MMapFileAtAddress(const std::string &fname, void *addr, bool touchAll, const std::string &lockName)
Definition: Util.cxx:18
int * fHeadIdxs
Definition: Heads.h:68
int fHeadSeqsLen
Definition: Heads.h:40
bool FlipOddFor(int head, int child) const
Definition: Heads.cxx:92
Mapping from a subset of the library to their best matches.
Definition: Heads.h:16
Utilities for mapping in library files on-demand.
friend std::ostream & operator<<(std::ostream &, const lem::Heads &)
Definition: Heads.cxx:140
long fMappingSize
Definition: Heads.h:43
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
int fNHeads
Definition: Heads.h:38
int * fHeadIdxs
Definition: Heads.h:39
static HeadsTranspose * FromMMap(const std::string &libPath)
Definition: Heads.cxx:99
assert(nhit_max >=nhit_nbins)
static Heads * FromMMap(const std::string &libPath, bool touchAll)
Definition: Heads.cxx:23
void CreateOnDemandMapping(const std::string &fname, const char *base)
Definition: OnDemand.cxx:123