Class for doing model compression
[u/mrichter/AliRoot.git] / HLT / comp / AliL3Compress.cxx
1 //$Id$
2
3 // Author: Anders Vestbo <mailto:vestbo$fi.uib.no>
4 //*-- Copyright &copy ASV
5
6 #include <stdio.h>
7 #include <stream.h>
8 #include <stdlib.h>
9
10 #include "AliL3Compress.h"
11 #include "AliL3TrackArray.h"
12 #include "AliL3ModelTrack.h"
13 #include "bitio.h"
14
15 ClassImp(AliL3Compress)
16
17 AliL3Compress::AliL3Compress()
18 {
19   
20 }
21
22 AliL3Compress::~AliL3Compress()
23 {
24   
25 }
26
27 void AliL3Compress::Write2File(AliL3TrackArray *tracks)
28 {
29   FILE *file = fopen("data.raw","w");
30   Short_t ntracks = tracks->GetNTracks();
31   cout<<"Writing "<<ntracks<<" tracks to file"<<endl;
32   //Write the number of tracks at the beginning:
33   //fwrite(&ntracks,sizeof(Short_t),1,file);
34   
35   for(Int_t i=0; i<ntracks; i++)
36     {
37       AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
38       if(!track) continue;
39       AliL3TrackModel *model = track->GetModel();
40       cout<<"Writing "<<model->fFirstPointX<<endl;
41       if(fwrite(model,sizeof(AliL3TrackModel),1,file)!=1) break;
42       for(Int_t j=0; j<model->fNClusters; j++)
43         {
44           AliL3ClusterModel *cl = track->GetClusterModel(j);
45           fwrite(cl,sizeof(AliL3ClusterModel),1,file);
46         }
47     }
48   fclose(file);
49 }
50
51 void AliL3Compress::ReadFile()
52 {
53   FILE *file = fopen("data.raw","r");
54   
55   AliL3TrackArray *tracks = new AliL3TrackArray("AliL3ModelTrack");
56   Int_t ntracks=0;
57   
58   while(!feof(file))
59     {
60       AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->NextTrack();
61       track->Init(0,0);
62       AliL3TrackModel *model = track->GetModel();
63       AliL3ClusterModel *clusters = track->GetClusters();
64       if(fread(model,sizeof(AliL3TrackModel),1,file)!=1) break;
65       cout<<"Read model "<<model->fFirstPointX<<endl;
66       if(fread(clusters,(model->fNClusters)*sizeof(AliL3ClusterModel),1,file)!=1) break;
67       ntracks++;
68     }
69   
70   delete tracks;
71   cout<<"Read "<<ntracks<<" tracks from file"<<endl;
72   fclose(file);
73 }
74
75 void AliL3Compress::CompressFile()
76 {
77   
78   BIT_FILE *output = OpenOutputBitFile("test.raw");
79   FILE *input = fopen("data.raw","r");
80
81   AliL3TrackModel track;
82   AliL3ClusterModel cluster;
83   Int_t temp;
84
85   while(!feof(input))
86     {
87       if(fread(&track,sizeof(AliL3TrackModel),1,input)!=1) break;
88       cout<<"Writing "<<sizeof(AliL3TrackModel)<<endl;
89       fwrite(&track,sizeof(AliL3TrackModel),1,output->file);
90       Int_t bitcount=0;
91       for(Int_t i=0; i<track.fNClusters; i++)
92         {
93           if(fread(&cluster,sizeof(AliL3ClusterModel),1,input)!=1) break;
94           Int_t flag = cluster.fPresent;
95           OutputBit(output,flag);
96           bitcount++;
97           if(!flag) continue;
98           temp = (Int_t)cluster.fDTime;
99           if(temp<0)
100             OutputBit(output,0);
101           else
102             OutputBit(output,1);
103           OutputBits(output,abs(temp),8);
104           temp = (Int_t)cluster.fDPad;
105           if(temp<0)
106             OutputBit(output,0);
107           else
108             OutputBit(output,1);
109           OutputBits(output,abs(temp),8);
110           bitcount+=8;
111           temp = (Int_t)cluster.fDCharge;
112           OutputBits(output,temp,10);
113           bitcount+=10;
114           
115           //Short_t temp=(Short_t)cluster.fDTime;
116           cout<<"flag "<<(int)flag<<" dtime "<<(int)cluster.fDTime<<" dpad "<<(int)cluster.fDPad<<" charge "<<cluster.fDCharge<<endl;
117         }
118       
119     }
120
121   fclose(input);
122   CloseOutputBitFile(output);
123 }
124
125 void AliL3Compress::ExpandFile()
126 {
127   BIT_FILE *input = OpenInputBitFile("test.raw");
128   
129   AliL3TrackModel track;
130   AliL3ClusterModel cluster;
131   
132   fread(&track,sizeof(AliL3TrackModel),1,input->file);
133   for(Int_t i=0; i<track.fNClusters; i++)
134     {
135       Int_t temp,sign;
136       temp = InputBit(input);
137       if(!temp) break;
138       sign=InputBit(input);
139       temp = InputBits(input,8);
140       if(!sign)
141         temp*=-1;
142       cout<<"Dtime "<<temp;
143       sign=InputBit(input);
144       temp = InputBits(input,8);
145       if(!sign)
146         temp*=-1;
147       cout<<" DPad "<<temp;
148       temp=InputBits(input,10);
149       cout<<" Charge "<<temp<<endl;
150     }
151   CloseInputBitFile(input);
152 }