Skip to content

Commit

Permalink
updated the documentation (README) to include examples with iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
gavalian committed Dec 18, 2023
1 parent 0b4da65 commit 6b0c04c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,59 @@ Output:

<img src="https://github.com/gavalian/hipo/blob/master/documents/screenshots/histogram_vz.png" width="800">

## Iterators

Iterators can be used to read through bank. There are several ways to construct iterators, through expressions and
through lambda functions. Here is an example with expressions:

```c++
hipo::reader r(file);
hipo::banklist list = r.getBanks({"REC::Particle","REC::Event"});
while( r.next(list)){
hipo::iterator it = hipo::iterator::reduce(list[0],"charge!=0");
for(it.begin(); !it.end(); it.next()){
printf("\t pid [%d] = %d\n",it.index(), list[0].getInt(0,it.index()));
}
}
```
Here is an example with lambda function:
```c++
hipo::reader r(file);
hipo::banklist list = r.getBanks({"REC::Particle","REC::Event"});
std::function charged = [](hipo::bank &b, int row) { return b.getInt("charge",row)!=0 ? 1.0 : 0.0;};
while( r.next(list)&&counter<350){
hipo::iterator it = hipo::iterator::reduce(charged,list[0]);
for(it.begin(); !it.end(); it.next()){
printf("\t pid [%d] = %d\n",it.index(), list[0].getInt(0,it.index()));
}
}
```

Iterators can be used also to get indicies of the refernce bank. For example:

```c++
hipo::reader r(file);
hipo::banklist list = r.getBanks({"REC::Particle","REC::Calorimeter"});

while( r.next(list)&&counter<350){
int status = list[0].getInt("status",0);
if(list[0].getInt(0,0)==11&&abs(status)>=2000&&abs(status)<3000){
hipo::iterator it = hipo::iterator::link(list[1],0,1);
double energy = 0.0;
for(it.begin(); !it.end(); it.next()){
energy += list[1].getFloat("energy",it.index());
}
printf("total energy = %f\n",energy);
}
}
```
In the example above, the link function will return an iterator for the bank "REC::Calorimeter"
where the column number 1 (which is "pindex") has a value of 0. Then by itarating over the returned
indicies the total energy can be calculated.
## Analysis
Expand Down
12 changes: 3 additions & 9 deletions examples/histograms.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,17 @@ void example7(const char *file){
}

void example8(const char *file){

hipo::reader r(file);
hipo::banklist list = r.getBanks({"REC::Particle","REC::Event"});
int counter = 0;

std::function charged = [](hipo::bank &b, int row) {
int charge = b.getInt("charge",row);
if(charge!=0) return 1.0; return 0.0;
};
std::function charged = [](hipo::bank &b, int row) { return b.getInt("charge",row)!=0 ? 1.0 : 0.0;};

int counter = 0;
while( r.next(list)&&counter<350){
counter++;
hipo::iterator it = hipo::iterator::reduce(charged,list[0]);
list[0].show();
it.show();
//for(it.begin(); !it.end(); it.next()){
// printf("\t pid [%d] = %d\n",it.index(), list[0].getInt(0,it.index()));
//}
}
}

Expand Down

0 comments on commit 6b0c04c

Please sign in to comment.